Use loadlibrary and getprocaddress functions to call the DLL Code If you have any questions, please kindly advise (Thank you )! Landlord
Wangzi163 (Oracle)
14:12:01 in VC/mfc/process/thread/DLL
Expert advice Program What's the problem? I made a DLL file and wanted to call it in the display mode. It was implemented using loadlibrary and getprocaddress functions, but the operation always failed.
The program code is as follows:
// Dll_test.cpp generates dll_test.dll
# Include "stdafx. H"
# Include "dll_test.h"
Int max (int A, int B)
{
If (A> B)
Return;
Else
Return B;
}
Bool apientry dllmain (handle hmodule,
DWORD ul_reason_for_call,
Lpvoid lpreserved
)
{
Switch (ul_reason_for_call)
{
Case dll_process_attach:
Case dll_thread_attach:
Case dll_thread_detach:
Case dll_process_detach:
Break;
}
Return true;
}
// Test_getmax.cpp
# Include "stdafx. H"
# Include <stdio. h>
# Include <afxwin. h>
# Include <afxext. h>
# Include <afxdisp. h>
# Include <afxdtctl. h>
Int main (INT argc, char * argv [])
{
Typedef int (callback * lpfndllfunc) (INT, INT );
Hinstance hdll;
Lpfndllfunc;
Hdll = loadlibrary ("dll_test.dll ");
If (hdll! = NULL)
{
Lpfndllfunc = (lpfndllfunc) getprocaddress (hdll, "Max ");
If (! Lpfndllfunc)
{
Freelibrary (hdll );
}
Else
{
Printf ("function found ");
Int;
A = lpfndllfunc (1, 2 );
Printf ("return straight: % d", );
}
}
Return 0;
}
Number of questions: 100, replies: 12
Top
1st Floor
Louifox)
The score is returned at 14:41:24 ,.
5
Note the folder in which dll_test.dll is located. Top
2nd floor
Oyljerry ([heartbleed] → tiramisu √ success)
The score is returned at 15:01:31 ,.
2
Ensure that the DLL is under the project directory
Error refers to what error? Top
Third floor
Legendhui (autumn leaves)
The score is returned at 15:27:24 ,.
2
Check whether your dll path is top
4th floor
Wenxinfly)
The score is returned at 15:36:54 ,.
2
The above makes sense.
But does your int max () function Add the top icon that marks the export function?
5th floor
Laiyiling (Stranger [MVP])
The score is returned at 20:31:20 ,.
50
Do not call back
Typedef int (sqrtproc) (INT, INT );
Sqrtproc * lpfndllfunc;
Lpfndllfunc = (sqrtproc *) getprocaddress (hdll, "Max"); top
6th Floor
Kugou123 (cool dog) (awesome life, do not need to explain www.xiaozhou.net)
The score is returned at 20:36:15 ,.
10
You are advised to use the. Def file for export.
Avoid the compiler changing the name of your export function.
In addition, if your function does not have a call Convention, do not add it when you use typedef to define the function pointer. Top
7 th floor
Wellin ()
The score is returned at 23:55:01 ,.
20
Typedef int (* AAA) (INT, INT );
AAA lpfndllfunc;
Hinstance hinst;
Hinst = loadlibrary ("demodll. dll ");
Lpfndllfunc = (AAA) getprocaddress (hinst, function name );
Lpfndllfunc (A, B );
Top
8 th floor
Tankbattle (tank wars)
The score is returned at 00:44:50 ,.
3
Use dumpbin.exe (available in VC) to check what function names exported from the dynamic library are changed. Top
9th floor
Redkit (chinayan)
The score is returned at 12:49:00 ,.
2
Hdll = loadlibrary ("dll_test.dll ");
In this case, you should use the absolute path!
Top
10th floor
Sambios)
The score is returned at 15:41:04 ,.
4
The code above has the following Disadvantages:
1) No description of the function to be exported.
2) The. Def file is missing.
The content of the corrected file is as follows:
Change the function in DLL to the following code:
Extern "C" _ declspec (dllexport) int _ stdcall max (int A, int B)
{
If (A> B)
Return;
Else
Return B;
}
// The content of the test. Def file is as follows:
Exports
Max
//:~
The main function does not need to be modified. So there is no problem.
Top
11 th floor
Wangzi163 (Oracle)
The score is 0 at 19:20:23.
I have already declared it, but I forgot to write it, as shown below:
Extern "C" _ declspec (dllimport) int max (int A, int B );
The last reason is to remove callback. Thank you! Top
12th floor
Robot1980 (past events)
The score is 0 at 14:47:36.
OK