Method for calling DLL (verification pass)

xiaoxiao2021-03-06  189

Sender: Appleii (Dry Yumi), Word Area: Programming Title: Method for calling DLL (Verification Pass) Send Station: Sun and Moonlight (August 04, 2004 13:22:49 Wednesday), station letter

Many of the code on the Internet are actually verified. In this article, how to call 10 I have verified on VC6, so it is posted, help the big rookie like me.

Http://www.ipx.cn/html/c/20040304/index/200434134531.html

Creation and calling method of DLL in VC

This is only actually operation, without related theory 2 DLL, first, "New" in the VC integration development interface, new project. Whether it is VC6.0 or VC.NET, there is an option to establish a DLL project. Just some slightly, such as the ISAPI DLL in VC.NET, the expansion stored procedure DLL, etc., these are not discussed. For example, we have established a DLL project with static connection MFC libraries, namely MYDLL, edit the MyDLL.cpp file, join our own function void Go (). Note that there is no need to declare it in MyDLL.H, and you need to turn the function head as the following: Extern "c" __declspec (dllexport) Void Go () {// code .....} DLLEXPORT indicates that this function is called by external call. . Because of whether or not the parameters are parameters, we have to declare a function with parameters: Extern "C" __declspec (dllexport) void wrent (cstring str) {// code .....} OK, compiled below Form a MyDLL.dll file. 2 DLL calls, let's write a program with VC to call it. In the called function, you must first get the handle of the DLL, there is a statement: Hinstance Dllinstance; Dllinstance = :: loadLibrary (strdllurl); if (Dllinstance == Null) AFXMessageBox ("can't open dll file"); StrDllURL It is a string of mydll.dll path, which can find it. :: LoadLibrary gets the handle of the DLL file identified by the parameter. After getting your handle, you will get the function address below to perform it. There is a statement: FarProc Proc; PROC = GetProcaddress (Dllinstance, "Go"); if (proc == null) AFXMessageBox ("can't find function"); else proc (); FarProc is a remote process pointer, via getProcaddress Get the address of the function. Its two parameters are the name of the DLL file handle and function. Then FarProc can use the same as Go, it is GO, Go is it. For functions in DLL with parameters, the calling method is different. Because the call to the function is made by reference to its address, the incoming parameter is not correct, and the correctness cannot be known during the compilation and coupling of the function call program.

Therefore, to make a declaration in the call to the DLL in the calling program, such as Went in MyDLL, we have to make a declaration as follows: typedef void (fal __cdecl * mywent) (cstring); then declared variables with type MYWENT Call, as follows: MyWent MyProc; MyProc = (MyWent) GetProcaddress (Dllinstance, "Go"); if (myProc == Null) AFXMessageBox ("can't find function"); Else MyProc ("o ----- YEAH --------- "); When notice the declaration, because the definition of WENT in the DLL is a C language call specification, you must use __cdecl before MYWENT, and __stdcall is PASCAL calls in the VC. Specification, no. Be sure to pay attention. --Appleii once is the most widely used computer, using 6502 CPU, there are hundreds of K levels. Usually no hard disk, 1 to 2 5 "floppy drives, green or color displays, no mobs. I used, 15 years Before. Another low-grade computer I have used is Laser310. Of course, it is 15 years ago. ※ Source: · Sun Moonlight Bbs.fudan.edu.cn · [from: 10.100.178.160] ※ Modification: · Appleii in June 04, 13:23:05 revision this article · [from: 10.100.178.160]

转载请注明原文地址:https://www.9cbs.com/read-128543.html

New Post(0)