none
Return code of LoadLibrary = 0x80000000 on Windows 7 (64bit) RRS feed

  • Frage

  • Hello,

    i'm using Microsoft Visual Studio 2005 (Visual Basic) and i want to load a DLL file using the API function LoadLibrary. The return code (handle) is -2147483648 (0x80000000) and the error code delivered by the API function GetLastError is 0. In the following code the API function GetProcAddress fails wit GetLastError=126 (The specified module could not be found.).

    In the MSDN help article for LoadLibrary the return code -2147483648 is not explained. The statement there is that the return value NULL means that LoadLibrary failed and all other values are assumed to be a valid DLL handle for following functions. The special value 0x80000000 seems to be an undocumented error message.

    Of course i have checked that the DLL name is correct and that the function name in GetProcAddress is correct too.

    And i did tests using the Ansi and the Unicode version of LoadLibrary and nothing worked.


    My question is, does anybody know what the meaning of the return value 0x80000000 of LoadLibrary is?



    Here is the VB 2005 code I have used:

        ' API Declarations:
        Declare Ansi Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" ( _
            ByVal lpLibFileName As String) As Integer

        Declare Ansi Function LoadLibraryEx Lib "kernel32" Alias "LoadLibraryExA" ( _
            ByVal lpLibFileName As String, _
            ByVal hFile As Integer, _
            ByVal dwFlags As Integer) As Integer

        ' function calls:
        Private Function loadCasApiClientDLL(ByRef errmsg As String, ByRef DllInstance As Integer) As Integer
            ' Rückgabewerte:
            '  -2: Die Bitbreite des Betriebssystems (32 oder 64) konnte nicht festgestellt werden (sollte nie vorkommen)
            '  -1: Die DLL konnte nicht geladen werden
            '   0: laden OK und alle COMMAND_CAS_...-Konstanten konnten einer Funktion zugeordnet werden
            '  >0: laden OK, Rückgabewert enthält die Anzahl der Funktionen, die nicht zugeordnet werden konnten.
            '      (z.B. Schreibfehler in COMMAND_CAS_..., auch die Groß-/Kleinschreibung muss stimmen!)
            '

            Dim CasApiClientName As String
            Dim osBit As Integer

            Dim dllFunctName As String = ""
            Dim dllFunctAddr As Integer = 0

            Dim resParams(1) As String


            ' check Windows version (32bit or 64bit)
            osBit = System.IntPtr.Size * 8
            If osBit = 32 Then
                ' 32bit Windows
                CasApiClientName = "CasApiClient.DLL"
            ElseIf osBit = 64 Then
                ' 64bit Windows
                CasApiClientName = "CasApiClient64.DLL"
            Else
                ' unknown OS (should never occur...)
                errmsg = "unknown OS"
                loadCasApiClientDLL = -2
                Exit Function
            End If

            DllInstance = LoadLibrary(CasApiClientName) ' "CasApiClient.DLL" or "CasApiClient64.DLL"
            If DllInstance = 0 Then
                errmsg = "could not load " & CasApiClientName
                loadCasApiClientDLL = -1
            Else
                ' LoadLibrary was successful:
                errmsg = ""
                loadCasApiClientDLL = 0

                ' Set Delegates with DLL entry points (addresses):
                dllFunctName = "InitCasRunF"
                dllFunctAddr = GetProcAddress(DllInstance, dllFunctName)
                If dllFunctAddr = 0 Then
                    ' function name doesn't exist:
                    loadCasApiClientDLL += 1 ' error counter for each function that wasn't found
                    errmsg &= vbNewLine & vbTab & dllFunctName
                Else
                    ' Create a Delegate für this DLL function
                    methodInitCasRunF = CType(System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(CType(dllFunctAddr, IntPtr), GetType(dInitCasRunF)), dInitCasRunF)
                End If

                ' Do this for other DLL functions
            End If
        End Function

    Best regards

    schlizbaeda



    Freitag, 19. April 2013 15:00

Alle Antworten

  • Hello,

    there are two things I forgot to mention:

    The described problem only occurs on the 64bit version of Windows. On a 32bit version this problem doesn't occur.

    And the "better" thing:
    In a previous version of my software the same code worked! After that I added a multi-language feature using the resource file technology. And since that version it desn't work any longer.

    Has anybody got an idea about this undocumented issue?

    with best regards

    schlizbaeda

    Montag, 22. April 2013 07:39
  • Hello,

    now i know why i didn't get any response.

    The error was that the definition of the WinAPI functions LoadLibrary etc. was wrong. The return value is a handle to the loaded DLL and its size depends on the bit width of the OS. That means on 32bit Windows the handle size is 32 bit and on 64bit Windows the size is 64bit.

    To respect this don't use the data type Integer. Use the data type IntPtr instead.

    Here is the corrected code:

    ' API Declarations:
        Declare Ansi Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" ( _
            ByVal lpLibFileName As String) As IntPtr ' Integer is wrong here!

        Declare Ansi Function LoadLibraryEx Lib "kernel32" Alias "LoadLibraryExA" ( _
            ByVal lpLibFileName As String, _
            ByVal hFile As Integer, _
            ByVal dwFlags As Integer) As IntPtr ' Integer is wrong here

        Declare Ansi Function FreeLibrary Lib "kernel32" Alias "FreeLibrary" ( _
            ByVal hLibModule As IntPtr) As Integer ' here the handle is referenced

        Declare Ansi Function GetProcAddress Lib "kernel32" Alias "GetProcAddress" ( _
            ByVal hModule As IntPtr, _
            ByVal lpProcName As String) As IntPtr ' here the handle is referenced

    best regards

    schlizbaeda

    Donnerstag, 2. Mai 2013 15:18