在Qt中使用LoadLibrary无法加载DLL

Qt系列文章目录

文章目录

前言

最近因项目需要使用qt做开发,之前使用LoadLibrary加载dll成功,很庆幸,当一切都那么顺风顺水的时候,测试同事却发现,在windows平台上个别电脑上加载dll会失败。现在加载返回空指针,强大的qt提供了QLibrary类,方便dll动态加载。

cpp 复制代码
 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;
    }

在Qt中使用LoadLibrary无法加载DLL

一、问题分析

1.首先想到的是CartDll.dll相关依赖库缺失,查找dll依赖文件的方法

使用visual studio自带的工具查找

进入CartDll.dll所在目录,

输入命令:dumpbin -imports CartDll.dll

或者使用从定向到txt文本中:dumpbin -imports CartDll.dll > result.txt

bash 复制代码
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.方法二使用Dependencies.exe工具打开 CartDll.dll

3.方法3:修改代码

cpp 复制代码
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;
    }
    }
相关推荐
7年老菜鸡15 分钟前
策略模式(C++)三分钟读懂
c++·qt·策略模式
huanggang9824 小时前
在Ubuntu22.04上使用Qt Creator开发ROS2项目
qt·ros2
老秦包你会4 小时前
Qt第三课 ----------容器类控件
开发语言·qt
spygg6 小时前
Qt低版本多网卡组播bug
qt·组播·多网卡组播·qt5.7.0
码农客栈6 小时前
qt QWebSocketServer详解
qt
plmm烟酒僧7 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
Black_Friend8 小时前
关于在VS中使用Qt不同版本报错的问题
开发语言·qt
CSUC8 小时前
【Qt】QTreeView 和 QStandardItemModel的关系
qt
冷凝女子9 小时前
【QT】海康视频及openCv抓拍正脸接口
qt·opencv·音视频·海康
苏三有春11 小时前
PyQt5实战——UTF-8编码器功能的实现(六)
开发语言·qt