CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    LoadLibrary() strangeness with VS2019

    Using VS2019, I'm trying to build a project as 64-bit. Many years ago I'd built it as 32-bit using VS2005. The project consists of an exe file and around 30 x DLL's. Some of the DLL's are behaving strangely - e.g. crashing when there's apparently nothing wrong with the code. I tried a small experiment to check the values returned from LoadLibrary()

    Code:
    HINSTANCE handle;
    DWORD res;
    
    handle = LoadLibrary( <try to load a 32-bit DLL> );
    res = GetLastError(); <--- 'handle' == NULL and 'res' == 193 (ERROR_BAD_EXE_FORMAT)
    
    SetLastError(0);
    handle = LoadLibrary( <load somebody else's 64-bit DLL> );
    res = GetLastError(); <--- 'handle' seems valid and 'res' == 0 (ERROR_SUCCESS)
    
    SetLastError(0);
    handle = LoadLibrary( <load my own 64-bit DLL> );
    res = GetLastError(); <--- 'handle' seems valid but 'res' == 126 (ERROR_MOD_NOT_FOUND)
    The first two results are what I'd expected - but when trying to load one of my own-built DLL's I get ERR_MOD_NOT_FOUND (even though Loadlibrary() itself returns a valid handle). I assume this means that my DLL's are incomplete somehow - e.g. some expected section wasn't present. Is there some tool somewhere that I can use to test a DLL and tell me if it finds anything wrong?
    "A problem well stated is a problem half solved.” - Charles F. Kettering

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

    Re: LoadLibrary() strangeness with VS2019

    From MSDN:
    GetLastError function (errhandlingapi.h)
    ...

    Return value
    The return value is the calling thread's last-error code.

    The Return Value section of the documentation for each function that sets the last-error code notes the conditions under which the function sets the last-error code. Most functions that set the thread's last-error code set it when they fail. However, some functions also set the last-error code when they succeed. If the function is not documented to set the last-error code, the value returned by this function is simply the most recent last-error code to have been set; some functions set the last-error code to 0 on success and others do not.
    So never trust the GetLastError return value if the API call succeeds!
    Victor Nijegorodov

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: LoadLibrary() strangeness with VS2019

    Thanks Victor. I went back to my VS2005 build and tried the same thing. What was happening (for my DLL's) was simply that they need to link to other DLLs. But conveniently, as well as giving me the error code, VS2005 popped up a message box to tell me that some particular symbol (i.e. from one of the other DLL's) hadn't been found. Sadly, VS2019 just leaves me to guess at what the problem might be

    When testing with other people's DLL's, I think I just happened to choose DLL's which don't need to link to other DLL's.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

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

    Re: LoadLibrary() strangeness with VS2019

    I'm not sure it could help, but try to play with the SetErrorMode function
    Victor Nijegorodov

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: LoadLibrary() strangeness with VS2019

    You can't mix 32 bit and 64 bit libraries/exe. They must all be compiled as either 32 bit or 64 bit. See https://docs.microsoft.com/en-us/win...teroperability
    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)

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: LoadLibrary() strangeness with VS2019

    Thanks 2kaud, I did realise that - I just tried a mixture to make sure I'd get the expected response from LoadLibrary()
    "A problem well stated is a problem half solved.” - Charles F. Kettering

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