How can i set an entrypoint for a dll

2020-07-06 01:37发布

问题:

First i thought entry point in dlls DLLMain but then when i try to import it in C# i get an error that entrypoint wasn't found Here is my code:

#include <Windows.h>

int Test(int x,int y)
{
    return x+y;
}

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        MessageBox(0,L"Test",L"From unmanaged dll",0);
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
} 

How can i set an entry point for my dll? And if you dont mind can you give me little explanation about entry point?

Like do i have to set import the same dll again and changing the entry point so i can use other functions in same dll? thanks in advance.

回答1:

In your example, it seems you intend Test() to be an entry point however you aren't exporting it. Even if you begin exporting it, it might not work properly with C++ name "decoration" (mangling). I'd suggest redefining your function as:

extern "C" __declspec(dllexport) int Test(int x,int y)

The extern "C" component will remove C++ name mangling. The __declspec(dllexport) component exports the symbol.

See http://zone.ni.com/devzone/cda/tut/p/id/3056 for more detail.

Edit: You can add as many entry points as you like in this manner. Calling code merely must know the name of the symbol to retrieve (and if you're creating a static .lib, that takes care of it for you).