dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26

Thread: DLL not loaded with LoadLibrary() function

  1. #16
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,511

    Re: DLL not loaded with LoadLibrary() function

    Looking at the output from dumpbin, it doesn't seem quite right.

    Code:
    Dump of file asm.dll
    
    File Type: DLL
    
      Section contains the following exports for ERRORMSG.DLL
    Why is it referring to ERRORMSG.DLL ? Has the .dll been renamed?
    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++17 Compiler: Microsoft VS2017 (15.9.3)

  2. #17
    Join Date
    Dec 2017
    Posts
    38

    Re: DLL not loaded with LoadLibrary() function

    When testing on a new console app, the code works, on my other project GetyLastError outputs error 193 (0xC1) %1 is not a valid Win32 application. In Build -> Configuration Manager -> Active Solution Platform x64 is selected. In Project -> Properties -> Configuration Properties -> Linker -> Command Line -> Aditional Options "%(AdditionalOptions) /machine:x64" is specified.

    In Active Solution Platform when changing to X86 and on Aditional Options "%(AdditionalOptions) /machine:x86" I'm getting these errors:


    Severity Code Description Project File Line Suppression State
    Error A2013 .MODEL must precede this directive cpuid C:\xmrig-master\src\3rdparty\libcpuid\masm-x64.asm 2
    Error A2034 must be in segment block : exec_cpuid cpuid C:\xmrig-master\src\3rdparty\libcpuid\masm-x64.asm 5
    Error A2034 must be in segment block cpuid C:\xmrig-master\src\3rdparty\libcpuid\masm-x64.asm 6
    Last edited by binary2; December 30th, 2017 at 02:35 AM.

  3. #18
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,511

    Re: DLL not loaded with LoadLibrary() function

    You can't mix x64 .exe and x86 dll. They have to be the same. My test was for a x86 exe which worked with asm.dll so asm.dll is also x86.

    For x86, Active Solution Platform should be WIN32. You don't need to specify any linker additional options. Setting the Active Solution Platform in Configuration Manager sets the required solution properties. Note that if you change the Active Solution Configuration (release/debug), you'll need to ensure that the Active Solution Platform is correct and that the solution properties are also correct as these are stored separately per solution configuration.

    The errors shown are assembler errors. I thought you were using fasm as the assembler - not the VS masm assembler? Have you included your asm code as part of the solution? Also note that x64 asm is different in many ways to x86 asm and usually a x86 asm file cannot be just assembled as x64 asm without source changes.
    Last edited by 2kaud; December 30th, 2017 at 05:42 AM.
    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++17 Compiler: Microsoft VS2017 (15.9.3)

  4. #19
    Join Date
    Dec 2017
    Posts
    38

    Re: DLL not loaded with LoadLibrary() function

    I'm using Fasm for dll compilation and VS2017 for the c++ code

    I made a 64bit Dll with fasm, when running the program I'm getting this error:

    "asm.dll is either not designed to run on Windows or it contains an error"

  5. #20
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,511

    Re: DLL not loaded with LoadLibrary() function

    Quote Originally Posted by binary2 View Post
    I'm using Fasm for dll compilation and VS2017 for the c++ code

    I made a 64bit Dll with fasm, when running the program I'm getting this error:

    "asm.dll is either not designed to run on Windows or it contains an error"
    There's a problem with the 64bit dll. As fasm has a message board, I'd ask there. https://board.flatassembler.net/
    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++17 Compiler: Microsoft VS2017 (15.9.3)

  6. #21
    Join Date
    Dec 2017
    Posts
    38

    Re: DLL not loaded with LoadLibrary() function

    Thank you, I solved the issue with the dll file. I have one more question, how do I reference in a header file a function from the loaded dll to be used somewhere else

    Code:
    #include "App.h"
    #include <windows.h>
    #include <iostream>
    #include "a.h"
    
    using namespace std;
    
    typedef int(__stdcall *f_funci)(unsigned int, unsigned int);
    
    int main(int argc, char **argv) {
        
    	HMODULE ll = LoadLibrary(L"asm.dll");
    
    	if (ll == NULL) {
    		cout << "asm.dll not loaded. Error code is " << GetLastError() << endl;
    	}
    	cout << "asm.dll loaded ok\n";
    
    	f_funci assemblerMode = (f_funci)GetProcAddress(ll, "asm");
    
    	if (!assemblerMode) {
    		cout << "could not locate the function " << GetLastError() << endl;
    	}
    	
    	App app(argc, argv);
        return app.exec();
    }

    "a.h" is my header

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

    Re: DLL not loaded with LoadLibrary() function

    In the header file, you just specify the function declaration as _declspec(dllimport). See https://docs.microsoft.com/en-us/cpp...port-dllimport You also need to inform the linker into which .dll this function resides - so you need to specify the name of the appropriate .lib file in the linker configuration. Linker/General/Additional Library Directories/ specifies where the .lib file(s) reside(s) and Linker/Input/Additional Dependencies/ specifies the name(s) of the lib file(s). Or use a #pragma statement in the program. See lib within https://msdn.microsoft.com/en-us/library/7f0aews7.aspx

    If there is no generated .lib file when the .dll file is created, then you can't reference the exported .dll function as a 'normal' function, but will have to continue to use loadlibrary/getprocaddress or use the 'old fashioned way' of creating a .def file and specifying the .def file as a resource file in the solution. If when creating the .dll you don't get a .def file then you can create one manually. It's just a text file. See https://docs.microsoft.com/en-us/cpp...sing-def-files .

    PS You'll probably also need to specify extern "c" as well to avoid 'name-mangling' issues.See https://docs.microsoft.com/en-us/cpp...pecify-linkage

    So to specify a function, say int myfunc(int a) as import from a .dll,

    Code:
    extern "C" __declspec(dllimport) int myfunc(int a);
    Note that usually in c++, the same header file is used for the .dll source and the .exe that uses the .dll. Thus the header file often has the structure

    Code:
    #pragma once
    
    #ifdef INDLL
       #define DLL __declspec(dllexport)
    #else
       #define DLL __declspec(dllimport)
    #endif
    
    #ifdef __cplusplus
        extern "c" {
    #endif
    
    DLL int myfunc(int a);
    // More declarations here
    
    #ifdef __cplusplus
       }
    #endif
    If the header is used in a .exe source then it imports the functions. If used in the .dll source then INDLL needs to be defined before the #include so that the functions are exported.
    Last edited by 2kaud; January 1st, 2018 at 04:44 PM. Reason: PS
    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++17 Compiler: Microsoft VS2017 (15.9.3)

  8. #23
    Join Date
    Dec 2017
    Posts
    38

    Re: DLL not loaded with LoadLibrary() function

    I added the def file to my project "asm.def" with "asm" as the function name specified in the dll


    Code:
    LIBRARY   asm  
    EXPORTS  
       asm   @1
    The header file

    Code:
    #pragma once
    
    #ifdef INDLL
    #define DLL __declspec(dllexport)
    #else
    #define DLL __declspec(dllimport)
    #endif
    
    #ifdef __cplusplus
    extern "c" __declspec(dllimport) int assemblerMode(unsigned int, unsigned int);
    
    #endif
    Code:
    #pragma once
    
    #ifdef INDLL
    #define DLL __declspec(dllexport)
    #else
    #define DLL __declspec(dllimport)
    #endif
    
    #ifdef __cplusplus
    extern "c" {
    #endif
    
    	DLL int assemblerMode(unsigned int, unsigned int);
    	// More declarations here
    
    #ifdef __cplusplus
    }
    #endif
    In both cases when compiling I'm getting the error:

    error C2537: 'c': illegal linkage specification

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

    Re: DLL not loaded with LoadLibrary() function

    Sorry. My bad. Finger trouble. extern "c" should be extern "C" (capital C).
    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++17 Compiler: Microsoft VS2017 (15.9.3)

  10. #25
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,511

    Re: DLL not loaded with LoadLibrary() function

    It looks like with VS2017 you can't just use a .def with a .dll to import - you actually do need the .lib file.

    To create the .lib file from the .def file from a VS developer command prompt, chnage to the directory in which the .def is located then
    Code:
    lib /def:asm.def
    [or other file name instead of asm.def]

    This will produce a 32bit .lib file (asm.lib in this case) which can be used to import the functions from the .dll. To produce a 64 bit lib
    Code:
    lib /def:asm.def /machine:x64

    Note that asm is not allowed as a name. My revised .def file is

    Code:
    LIBRARY   asm
    EXPORTS
       asm1=asm   @1
       ShowErrorMessage @2
       ShowLastError @3
    The c++ code that compiles OK is
    Code:
    #pragma comment (lib, "asm")
    
    extern "C" __declspec(dllimport) int asm1(unsigned int, unsigned int);
    
    int main()
    {
    	asm1(1, 2);
    }
    Last edited by 2kaud; January 2nd, 2018 at 06:19 AM.
    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++17 Compiler: Microsoft VS2017 (15.9.3)

  11. #26
    Join Date
    Dec 2017
    Posts
    38

    Re: DLL not loaded with LoadLibrary() function

    Thank you

Page 2 of 2 FirstFirst 12

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)