LoadLibrary method loads and runs the DLL library

Recently, I contacted another company and asked to use the test program provided by the other company to test the DLL we made.

After receiving the test program of the other party, it is found that it is not the same as the way we used to call the DLL. But I read the code for a while and understood its meaning, and I got it in one day.

But there are also some small confusions, record it.

 

The following is a small Demo I wrote experimentally :

my_dll.h:

#ifdef MY_DLL_EXPORTS
#define MY_DLL_API __declspec(dllexport)
#else
#define MY_DLL_API __declspec(dllimport)
#endif


extern "C" MY_DLL_API int WINAPI fnmy_dll(void);

my_dll.cpp:

#include "stdafx.h"
#include "my_dll.h"

extern "C"
{
// This is an example of an exported function. 
    MY_DLL_API int WINAPI fnmy_dll( void )
    {
        MessageBox(NULL,L"ddd",L"xxx",NULL);
        return 42;
    }
}

test_dll.cpp:

#include "stdafx.h"

typedef int (WINAPI *MY_FUNC)(void);
MY_FUNC func;


int _tmain(int argc, _TCHAR* argv[]) 
{
    HMODULE  g_hDll = LoadLibrary(L"my_dll.dll");
    if (g_hDll)    
        func = (MY_FUNC)GetProcAddress(g_hDll, "fnmy_dll");
    func();
    return 0;
}

The structure of two projects (a dll, a test project that calls the dll):

LoadLibrary is to load the dll library file GetProcAddress function is to find the function address, here to declare the function pointer, point to the found function address, and then use the function through the function pointer.

It is worth noting that in the dll file, extern "C" should be added to the export function of the dll project. The function exported in this way is the name of the C language way, otherwise it is the name of the C++ way. If it is the latter, when using dll, the original name of the function will not be found.

 

 WINAPI identifies the way the function is called. If the exported function in the dll is declared as WINAPI, the function pointer declared by the project calling the DLL should also declare WINAPI.

The netizen gave the answer:
There are the following definitions in the windef.h header file
#define WINAPI __stdcall
#define APIENTRY WINAPI
VC has two function calling methods: one is __stdcall, and the other is __cdecl
function. There are two ways to call one. One is the PASCAL calling method, the other is the C calling method
using the PASCAL calling method, the function deletes the parameters from the stack before returning to the caller
. Yes
, the Windows system stipulates that the functions called by the system comply with the PASCAL calling method,
but the default calling method of the function in VC is __cdecl, which is the C calling method,
so the declaration displayed before WinMain.
In Windows programming, you will encounter many declaration modifiers, such as CALLBACK, WINAPI, PASCAL, which are all __stdcall on Intel CPU computers.
For detailed declaration details, please see the windef.h file

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324894536&siteId=291194637