在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算法优化工作室&20 分钟前
Qt平台+三维建模+动画演示+工业风展示
前端·qt
LoveXming42 分钟前
Qt零散知识点
开发语言·qt
alive9032 小时前
【QT】 进程
c++·qt·嵌入式·进程·qprocess
SunkingYang11 小时前
QT中怎么隐藏或显示最大化、最小化、关闭按钮
qt·关闭按钮·隐藏·显示·最大化按钮·最小化按钮
六bring个六11 小时前
QT上位机笔记
开发语言·笔记·qt
步木木11 小时前
Qt 5.14.2入门(一)写个Hello Qt!程序
开发语言·qt
FreeLikeTheWind.15 小时前
Qt 开发时可以在函数内引用的头文件
开发语言·c++·qt
binary001017 小时前
基于 Qt 的图片处理工具开发(一):拖拽加载与基础图像处理功能实现
图像处理·人工智能·qt
"_rainbow_"1 天前
Qt中的鼠标事件
开发语言·qt
Quz1 天前
QML输入控件: TextArea的应用(带行号的编辑器)
qt·ui