Unable to load DLL using LoadLibrary in Qt

Qt series article directory

foreword

Recently, because the project needs to use qt for development, I used LoadLibrary to load the dll successfully before. Fortunately, when everything went smoothly, the test colleagues found that loading the dll on some computers on the windows platform would fail. Now loading returns a null pointer, and the powerful qt provides the QLibrary class to facilitate dynamic loading of dlls.

 DWORD iErrorCode;
    HINSTANCE hlib=LoadLibrary(TEXT("CartDll.dll"));
    if(!hlib)
    {
    
    
        iErrorCode = GetLastError();
        cout<<" last error code = "<<iErrorCode<<endl;
        cout<<"open cart dll error!"<<endl;
        return -1;
    }

Unable to load DLL using LoadLibrary in Qt
insert image description here

1. Problem analysis

1. The first thing that comes to mind is that the related dependency library of CartDll.dll is missing. The method to find the dll dependent files
is to use the tools that come with visual studio to find
insert image description here

Enter the directory where CartDll.dll is located,
enter the command: dumpbin -imports CartDll.dll
or use the redirection to the txt text: dumpbin -imports CartDll.dll > result.txt
insert image description here

Microsoft (R) COFF/PE Dumper Version 14.29.30138.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file CartDll.dll

File Type: DLL

  Section contains the following imports:

    libifcoremd.dll
             1800AE0C8 Import Address Table
             1800BBCF0 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         176 for_inquire
                         249 for_rewind
                         282 for_write_int_fmt_xmit
                         281 for_write_int_fmt
                         12B for_dealloc_allocatable
                         289 for_write_seq_lis_xmit
                          D2 for_concat
                         278 for_trim
                         248 for_read_seq_xmit
                         242 for_read_seq
                          DD for_cpystr
                          D1 for_close
                         1B2 for_pause
                         246 for_read_seq_lis_xmit
                         288 for_write_seq_lis
                         245 for_read_seq_lis
                         1B1 for_open
                         179 for_is_nan_s_
                         26F for_stop_core
                         287 for_write_seq_fmt_xmit
                         286 for_write_seq_fmt

    libifportMD.dll
             1800AE178 Import Address Table
             1800BBDA0 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         154 SYSTEM

    libmmd.dll
             1800AE188 Import Address Table
             1800BBDB0 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         1D8 cbrtf
                         336 sinf
                         2E2 logf
                         345 tanf
                         153 __libm_sse2_sincos
                         1EE cexpf
                         20B cos
                         20F cosf
                         173 __powr4i4
                         2D7 log10f
                         30C powf
                         154 __libm_sse2_sincosf
                         246 expf
                         19C acosf

    MSVCR110.dll
             1800AE040 Import Address Table
             1800BBC68 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         336 _lock
                         49B _unlock
                         5EB memmove
                         18D __dllonexit
                         3DD _onexit
                         17E __clean_type_info_names_internal
                         161 __CppXcptFilter
                         1E0 _amsg_exit
                         5A3 free
                         347 _malloc_crt
                         2C6 _initterm
                         2C7 _initterm_e
                         1F6 _calloc_crt
                         5E9 memcpy
                         5ED memset
                         160 __C_specific_handler

    svml_dispmd.dll
             1800AE200 Import Address Table
             1800BBE28 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         392 __svml_logf4
                         399 __svml_logf4_mask
                         3BB __svml_powf4_mask
                         3B6 __svml_powf4
                         316 __svml_irem4
                         482 __svml_tanf4
                         286 __svml_expf4
                          10 __svml_acosf4
                         1AE __svml_cosf4
                         28B __svml_expf4_mask

    KERNEL32.dll
             1800AE000 Import Address Table
             1800BBC28 Import Name Table
                     0 time date stamp
                     0 Index of first forwarder reference

                         318 GetTickCount64
                         2FB GetSystemTimeAsFileTime
                         22E GetCurrentThreadId
                         43F QueryPerformanceCounter
                         118 DecodePointer
                         140 EncodePointer
                         132 DisableThreadLibraryCalls

  Summary

    193FE000 .data
        3000 .pdata
        F000 .rdata
        3000 .reloc
        1000 .rsrc
       AD000 .text

2. Method 2 Use the Dependencies.exe tool to open CartDll.dll
insert image description here
3. Method 3: Modify the code

int main()
{
    
    
    DWORD iErrorCode;
//    HINSTANCE hlib=LoadLibrary(TEXT("CartDll.dll"));

//    QString strFilePath = "CartDll.dll";

    LPCWSTR lpLibFileName = TEXT("E:\\osg\\work\\CallCartDll_20230721\\CallCartDll\\CartDll.dll");
    HINSTANCE hlib= LoadLibraryEx(lpLibFileName,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
    if(!hlib)
    {
    
    
        iErrorCode = GetLastError();
        cout<<" last error code = "<<iErrorCode<<endl;
        cout<<"open cart dll error!"<<endl;
        return -1;
    }
    }

Guess you like

Origin blog.csdn.net/aoxuestudy/article/details/132232758