Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loadlibrary fails to load dll

Tags:

dll

visual-c++

    *******************UseDll1.cpp*********************

        #include <windows.h>

typedef int (*function1_ptr) ();

function1_ptr function1=NULL;

int APIENTRY WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { 

    HMODULE myDll = LoadLibrary("Dll1.dll"); 

    if(myDll!=NULL) {  
        function1 = (function1_ptr) GetProcAddress(myDll,"function1");  

        if(function1!=NULL)  
            function1();
        else
            exit(4);

        FreeLibrary(myDll);
    }
    else
        exit(6);

    return 0;
}

This is done in order to call Dll1.dll which was created with the functionality to send mail to my own mail server. The above code runs and exits,and no mail is sent.

And i also placed the Dll1.dll in the same folder as UseDll1.

EDIT: I added the Dll1.dll into the system32 folder.

like image 478
Vinod K Avatar asked Jan 16 '11 19:01

Vinod K


2 Answers

Have you verify that you have all external dependencies for "DLL1.dll" ?

LoadLibrary will fail even if any of the indirect linked-library is not available.

like image 132
YeenFei Avatar answered Oct 20 '22 21:10

YeenFei


In my dll calling experience, I had same problem. I did everything for giving path rightly but my library can not be loaded. Firstly I thought my dll had error, but nothing solved my problem at start. I advice doing below step for friends who think their project written rightly but still having same problem:

  1. Go to your DLL project and assure that you selected Multi-threaded Debug instead of Multi-thread(Debug) DLL(don't use dll because it uses some function from dll instead of embedding them inside) as runtime library(specially for debug mode). You can see runtime library selection under Property>Configuration Property>C/C++>Code Generation.
  2. After our DLL file is ok, we must ensure that our project uses right platform. If our dll uses x64, our project have to use x64. You can control it from configuration manager and you can select right one from platform box.

After building project with right configuration, it is working now.

like image 41
Mustafa Kemal Avatar answered Oct 20 '22 22:10

Mustafa Kemal