在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;
    }
    }
相关推荐
利哥AI实例探险11 分钟前
建筑工地AI安全检测系统:YOLO11数据标注训练与PyQt5界面设计应用开发
人工智能·qt·安全
c1s2d3n4cs4 小时前
Qt使用QZipWriter和QZipReader来解压、压缩文件
开发语言·qt
qq_430583977 小时前
QT笔记- QTreeView + QFileSystemModel 当前位置的保存与恢复 #选中 #保存当前索引
开发语言·笔记·qt
小王爱吃月亮糖8 小时前
QT-QVariant类应用
开发语言·c++·笔记·qt·visual studio
阿松のblog8 小时前
pyQt5实现目标检测可视化001
开发语言·qt·目标检测
沐泽Mu17 小时前
嵌入式学习-QT-Day05
开发语言·c++·qt·学习
StringerChen19 小时前
Qt ui提升窗口的头文件找不到
开发语言·qt
沐泽Mu21 小时前
嵌入式学习-QT-Day07
c++·qt·学习·命令模式
沐泽Mu21 小时前
嵌入式学习-QT-Day09
开发语言·qt·学习
林浔09061 天前
QT信号槽
开发语言·qt