Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set an entrypoint for a dll

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.

like image 695
method Avatar asked Oct 04 '11 17:10

method


People also ask

How do I add an entry point to a DLL?

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). Use __stdcall for the C declaration or CallingConvention.

Does a DLL have an entry point?

A DLL can have a single entry-point function. The system calls this entry-point function at various times, which I'll discuss shortly. These calls are informational and are usually used by a DLL to perform any per-process or per-thread initialization and cleanup.

Does a DLL need DllMain?

DllMain is not mandatory. If you have some initialization code required to run when loading the dll, you should create a DllMain function, and treat the initialization there. Otherwise it's not required.


1 Answers

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).

like image 193
mah Avatar answered Sep 28 '22 16:09

mah