How to Use DLL Functions for dynamic loading-Static Loading
<! --
Google_ad_client = "pub-8432098110340913 ";
/* Busfly_ss _ 250x250 on the top left of the content area, created in 08-2-2 */
Google_ad_slot = "2731136837 ";
Google_ad_width = 250;
Google _ad_height = 250;
// -->
Encryption and decryption are used in company projects, and the dll library specified by the customer is used for encryption and decryption.
At first, I used the dll library according to the previous method. Here I will introduce it too. Although there are many online methods, the steps for loading DLL dynamically are as follows:
Hinstance dll library Instance name = loadlibrary (_ T ("dll library name. dll"); // load the decryption function library
Typedef bool (callback * function type name) (function parameter 1, function parameter 2...); // define the function type, Format 1
// Typedef bool (_ cdecl * function type name) (function parameter 1, function parameter 2 ......); // defines the function type, Format 2
Function name = (function type name) getprocaddress (dll library Instance name, _ T ("dll library original function name"); // define a function
Well, it's so simple. Then you can use the "function name in use", and use it as a general function.
I
In general, I use "define function type, Format 1" to define the function type, and online methods also provide this method. I did not expect that when I used it in a project, there are always errors.
Try {} catch {} cannot catch the error. The error is: the value of ESP was not properly saved
Callback ss a function call. This is usually a result of calling a function
Declared with one calling convention with a funtion pointer declared
With a different calling convention.
I found it online, and now this problem is frequently encountered by others. Unfortunately, few people have given it to me.
A good answer is provided. The method provided by a person is // typedef bool (_ cdecl
* Function type name) (function parameter 1, function parameter 2 ......
); // Define the function type, Format 2. this format is okay. It seems that the call conventions do not match. therefore, when using DLL, you must be familiar with the function Format in this DLL.
I may not be able to understand it.Hinstance mydll = loadlibrary (_ T ("C: \ test. dll"); // load the decryption function library
Typedef bool (callback * Add) (int A, int B); // defines the function type, in the format of 1
Add myadd = (ADD) getprocaddress (mydll, _ T ("add"); // define a function
Use Time:
Int x = 2;
Int y = 3;
Int r = 0;
R = myadd (x, y );
Result r = 5;
Of course, the premise is that the DLL has the Add function, and the parameter is (int A, int B)
if you do not use dynamic DLL loading, you can use static loading. However, you need to obtain the Lib file and. h file. If all three files are available, you can easily load the DLL as follows: how to load the DLL
1 (configure the header file path): select project-> properties-> C/C ++-> General-> Add a directory: set the path of the reference header file
2 (configure the DLL and Lib file paths ): select project> Properties> connector> General> additional library Directory: set the dll path
3 (load the Lib file ): select project> Properties> connector> input> additional dependency: Enter the Lib file name to be loaded (when loading multiple lib files, press enter as the separator)
4 (the main application Program . EXE Directory, which is in the same directory as the DLL and lib to be loaded): select project> Properties> connector> General> output file