CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Dec 2017
    Posts
    41

    DLL not loaded with LoadLibrary() function

    LoadLibray returns 0 when I'm trying to load a dll file. I'm using Visual Studio 2017

    Code:
    #include "App.h"
    #include <windows.h>
    #include <iostream>
    using namespace std;
    typedef int(__stdcall *f_funci)(unsigned char, unsigned int);
    int main(int argc, char **argv) {
        SetDllDirectory((LPCWSTR)L"C:\\xmrig-master\\build\\Debug");
        HINSTANCE hGetProcIDDLL = LoadLibrary(L"asm.dll");
        if (!hGetProcIDDLL) {
            cout << "could not load the dynamic library" << endl;
            //return EXIT_FAILURE;
        }else{
            cout << "cy" << endl;
        }
        /*
        f_funci assemblerMode = (f_funci)GetProcAddress(hGetProcIDDLL, "asm");
        if (!assemblerMode) {
            cout << "could not locate the function " << assemblerMode << GetLastError() << endl;
            //return EXIT_FAILURE;
        }
        cout << "assemblerMode() returned " << assemblerMode(1,0) << endl;
        //*/
        App app(argc, argv);
        return app.exec();
    }
    Last edited by 2kaud; December 28th, 2017 at 09:51 AM.

  2. #2
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: DLL not loaded with LoadLibrary() function

    Are you compiling as multi-byte or unicode? Your code seems a mixture. LoadLibrary() takes a parameter of LPCTSTR which means either LPCSTR if compiling as multi-byte or LPCWSTR if compiling as unicode. Try using ansi char strings instead of wide ones for SetDllDirectory() and LoadLibrary().
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: DLL not loaded with LoadLibrary() function

    Quote Originally Posted by binary2 View Post
    LoadLibray returns 0 when I'm trying to load a dll file. I'm using Visual Studio 2017
    And what does the GetLastError return in this case?
    Victor Nijegorodov

  4. #4
    Join Date
    Dec 2017
    Posts
    41

    Re: DLL not loaded with LoadLibrary() function

    Quote Originally Posted by VictorN View Post
    And what does the GetLastError return in this case?
    Error code 126 (0x7E) The specified module could not be found.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,398

    Re: DLL not loaded with LoadLibrary() function

    Quote Originally Posted by binary2 View Post
    Error code 126 (0x7E) The specified module could not be found.
    Well, could you answer the question 2kaud asked you in the post#2?

    And, BTW, what does the SetDllDirectory(...) call return?

    PS: I presume that your "asm.dll" does exist in the "C:\\xmrig-master\\build\\Debug" directory...
    Victor Nijegorodov

  6. #6
    Join Date
    Dec 2017
    Posts
    41

    Re: DLL not loaded with LoadLibrary() function

    Quote Originally Posted by 2kaud View Post
    Are you compiling as multi-byte or unicode? Your code seems a mixture. LoadLibrary() takes a parameter of LPCTSTR which means either LPCSTR if compiling as multi-byte or LPCWSTR if compiling as unicode. Try using ansi char strings instead of wide ones for SetDllDirectory() and LoadLibrary().
    I tried , still the same, dll cold not be found.

    SetDllDirectory(...) returns 1. The dll is in the path

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: DLL not loaded with LoadLibrary() function

    What happens if you try

    Code:
    //SetDllDirectory((LPCWSTR)L"C:\\xmrig-master\\build\\Debug");
    HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\xmrig-master\\build\\Debug\\asm.dll");
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Join Date
    Dec 2017
    Posts
    41

    Re: DLL not loaded with LoadLibrary() function

    Quote Originally Posted by 2kaud View Post
    What happens if you try

    Code:
    //SetDllDirectory((LPCWSTR)L"C:\\xmrig-master\\build\\Debug");
    HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\xmrig-master\\build\\Debug\\asm.dll");

    I recieved the error:

    Severity Code Description Project File Line Suppression State
    Error C2664 'HMODULE LoadLibraryW(LPCWSTR)': cannot convert argument 1 from 'const char [36]' to 'LPCWSTR' xmrig C:\xmrig-master\src\xmrig.cpp 45

    I added "L" to the address string

    Code:
    HINSTANCE hGetProcIDDLL = LoadLibrary(L"C:\\xmrig-master\\build\\Debug\\asm.dll");
    I also tried:

    Code:
    HINSTANCE hGetProcIDDLL = LoadLibrary((LPCTSTR)"C:\\xmrig-master\\build\\Debug\\asm.dll");
    In both cases the dll is not loaded

  9. #9
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: DLL not loaded with LoadLibrary() function

    cannot convert argument 1 from 'const char [36]' to 'LPCWSTR'
    then your program is using unicode - and the whole program should be written as unicode. If
    Code:
    HINSTANCE hGetProcIDDLL = LoadLibrary(L"C:\\xmrig-master\\build\\Debug\\asm.dll");
    which is unicode, still produces the same extended error (from GetLastError()), then it looks like the file asm.dll in the specified folder isn't present. Error 126 means that the specified module could not be found as in post #4. If this was a permission issue then a different error message would be produced. How was asm.dll produced? From another program, copied from somewhere else, downloaded?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Dec 2017
    Posts
    41

    Re: DLL not loaded with LoadLibrary() function

    asm.dll is coded in x86 assembly and compiled with Fasm

  11. #11
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: DLL not loaded with LoadLibrary() function

    Have you used dumpbin to show exports to verify that the .dll file is correct? From a VS2017 developer command prompt,

    Code:
    dumpbin /exports C:\xmrig-master\build\Debug\asm.dll
    This should show something like

    Code:
    C:\MyProgs>dumpbin /exports sclcomm.dll
    Microsoft (R) COFF/PE Dumper Version 7.10.6030
    Copyright (C) Microsoft Corporation.  All rights reserved.
    
    
    Dump of file sclcomm.dll
    
    File Type: DLL
    
      Section contains the following exports for SclComm.dll
    
        00000000 characteristics
        58FB5F59 time date stamp Sat Apr 22 14:49:13 2017
            0.00 version
               1 ordinal base
              50 number of functions
              50 number of names
    
        ordinal hint RVA      name
    
              1    0 00013610 DictAct
              2    1 0000C210 ShowErrMsg
              3    2 0000C2D0 WGenMaint
    {...}
    
      Summary
    
            1000 .data
            1000 .gfids
            5000 .rdata
            4000 .reloc
            1000 .rsrc
           1B000 .text
    This is a sample output from dumpbin for the file sclcomm.dll

    If this command doesn't produce something like above then there is an issue with asm.dll.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    Dec 2017
    Posts
    41

    Re: DLL not loaded with LoadLibrary() function

    I checked with dumpbin and it shows something like you mentioned.
    I checked GetLastError and I'm getting an error 193 (0xC1) %1 is not a valid Win32 application

  13. #13
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: DLL not loaded with LoadLibrary() function

    Attach asm.dll as a file to a post and I'll have a look at it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    Dec 2017
    Posts
    41

    Re: DLL not loaded with LoadLibrary() function

    Thank you

    asm.zip

  15. #15
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: DLL not loaded with LoadLibrary() function

    This test code loads asm.dll OK on my system using VS2017.

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	HMODULE ll = LoadLibrary("asm.dll");
    
    	if (ll == NULL) {
    		cout << "asm.dll not loaded. Error code is " << GetLastError() << endl;
    		return 1;
    	}
    
    	cout << "asm.dll loaded ok\n";
    }
    In this test case, asm.dll is in the same folder as the test .exe. Note that this is for multi-byte, not unicode build.

    This is the unicode version

    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <Windows.h>
    
    #include <iostream>
    using namespace std;
    
    int wmain()
    {
    	HMODULE ll = LoadLibrary(L"asm.dll");
    
    	if (ll == NULL) {
    		wcout << L"asm.dll not loaded. Error code is " << GetLastError() << endl;
    		return 1;
    	}
    
    	wcout << L"asm.dll loaded ok\n";
    }
    In your build, you are building for x86 solution platform?
    Last edited by 2kaud; December 29th, 2017 at 12:51 PM. Reason: Added unicode version
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured