Search code examples
c++windowsdllname-decoration

Using functions in a dll in c++?


I have created a dll in c++ using __declspec(dllexport) before class name. Now when i try to use it in another c++ program it crashes in between. When i debugged it i found that the function pointer is not initialized at all. help me plz.

using namespace std;

typedef void (*func)();

int main()

{

    func funcpointer;
    HINSTANCE xyz = LoadLibrary(TEXT("C:\\extra\\dll\\dlls\\debug\\random.dll"));
    funcpointer = (func)GetProcAddress(xyz,"get it");
    funcpointer();
    return 0;
}

Thanks in advance.


Solution

  • First of all use DUMPBIN /EXPORTS yourdll.dll to see if the function you expect to be exported is actually exported and its exact name. If you find the name "mangled" you probably need to declare the function as extern "C". Once you have determined the name the way you go is correct. Check also the HINSTANCE xyz became not null after loading the library. If null you robably don't reach the dll ( not in the search path ) or for some reason it can't load for example because some dependencies are missing.