Creation and calling method of DLL in VC

zhaozj2021-02-11  157

The creation of DLL in the VC is only actually operational, without related theories.

2 DLL creation

First, use the "New" in the development interface to create a 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 a static connection MFC library, named MYDLL

Then, edit mydll.cpp files, join our own function void Go (). Note that there is no need to declare it in MyDLL.H, but it needs to turn the function head as the following:

Extern "C" __declspec (dllexport) void Go ()

{

// Code ...

}

DLLEXPORT indicates that this function is called by external.

Because of whether or not parameters, we must affect the way external calls, so we declare a function with parameters:

EXTERN "C" __DECLSPEC (DLLEXPORT) Void Went (CSTRING STR)

{

// Code ...

}

OK, the next compilation connection forms mydll.dll file.

2 DLL call

Ok, let's call it with a VC written by VC. In the called function, 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 is a string of the MyDLL.dll path, so the program 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 to get the address of the function via getProcAddress. 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 must do a statement as follows:

Typedef void (far __cdecl * mywent) (CSTRING);

Then be called by type myWent declared variables, as follows:

MYWENT myproc;

MyProc = (MyWent) GetProcaddress (Dllinstance, "Go");

IF (MyProc == Null) AFXMessageBox ("Can't Find Function);

Else MyProc ("o ----- Yeah ---------");

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

New Post(0)