Calling functions in a DLL from C++

前端 未结 7 2454
清歌不尽
清歌不尽 2020-11-30 00:17

I have a solution in VS 2008 with 2 projects in it. One is a DLL written in C++ and the other is a simple C++ console application created from a blank project. I would like

7条回答
  •  我在风中等你
    2020-11-30 01:12

    The following are the 5 steps required:

    1. declare the function pointer
    2. Load the library
    3. Get the procedure address
    4. assign it to function pointer
    5. call the function using function pointer

    You can find the step by step VC++ IDE screen shot at http://www.softwareandfinance.com/Visual_CPP/DLLDynamicBinding.html

    Here is the code snippet:

    int main()
    {
    /***
    __declspec(dllimport) bool GetWelcomeMessage(char *buf, int len); // used for static binding
     ***/
        typedef bool (*GW)(char *buf, int len);
    
        HMODULE hModule = LoadLibrary(TEXT("TestServer.DLL"));
        GW GetWelcomeMessage = (GW) GetProcAddress(hModule, "GetWelcomeMessage");
    
        char buf[128];
        if(GetWelcomeMessage(buf, 128) == true)
            std::cout << buf;
            return 0;
    }
    

提交回复
热议问题