Qt系列文章目录
文章目录
- Qt系列文章目录
- 前言
- 一、问题分析
前言
最近因项目需要使用qt做开发,之前使用LoadLibrary加载dll成功,很庆幸,当一切都那么顺风顺水的时候,测试同事却发现,在windows平台上个别电脑上加载dll会失败。现在加载返回空指针,强大的qt提供了QLibrary类,方便dll动态加载。
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
Microsoft (R) COFF/PE Dumper Version 14.29.30138.0
Copyright (C) Microsoft Corporation. All rights reserved.Dump of file CartDll.dllFile Type: DLLSection contains the following imports:libifcoremd.dll1800AE0C8 Import Address Table1800BBCF0 Import Name Table0 time date stamp0 Index of first forwarder reference176 for_inquire249 for_rewind282 for_write_int_fmt_xmit281 for_write_int_fmt12B for_dealloc_allocatable289 for_write_seq_lis_xmitD2 for_concat278 for_trim248 for_read_seq_xmit242 for_read_seqDD for_cpystrD1 for_close1B2 for_pause246 for_read_seq_lis_xmit288 for_write_seq_lis245 for_read_seq_lis1B1 for_open179 for_is_nan_s_26F for_stop_core287 for_write_seq_fmt_xmit286 for_write_seq_fmtlibifportMD.dll1800AE178 Import Address Table1800BBDA0 Import Name Table0 time date stamp0 Index of first forwarder reference154 SYSTEMlibmmd.dll1800AE188 Import Address Table1800BBDB0 Import Name Table0 time date stamp0 Index of first forwarder reference1D8 cbrtf336 sinf2E2 logf345 tanf153 __libm_sse2_sincos1EE cexpf20B cos20F cosf173 __powr4i42D7 log10f30C powf154 __libm_sse2_sincosf246 expf19C acosfMSVCR110.dll1800AE040 Import Address Table1800BBC68 Import Name Table0 time date stamp0 Index of first forwarder reference336 _lock49B _unlock5EB memmove18D __dllonexit3DD _onexit17E __clean_type_info_names_internal161 __CppXcptFilter1E0 _amsg_exit5A3 free347 _malloc_crt2C6 _initterm2C7 _initterm_e1F6 _calloc_crt5E9 memcpy5ED memset160 __C_specific_handlersvml_dispmd.dll1800AE200 Import Address Table1800BBE28 Import Name Table0 time date stamp0 Index of first forwarder reference392 __svml_logf4399 __svml_logf4_mask3BB __svml_powf4_mask3B6 __svml_powf4316 __svml_irem4482 __svml_tanf4286 __svml_expf410 __svml_acosf41AE __svml_cosf428B __svml_expf4_maskKERNEL32.dll1800AE000 Import Address Table1800BBC28 Import Name Table0 time date stamp0 Index of first forwarder reference318 GetTickCount642FB GetSystemTimeAsFileTime22E GetCurrentThreadId43F QueryPerformanceCounter118 DecodePointer140 EncodePointer132 DisableThreadLibraryCallsSummary193FE000 .data3000 .pdataF000 .rdata3000 .reloc1000 .rsrcAD000 .text
2.方法二使用Dependencies.exe工具打开 CartDll.dll
3.方法3:修改代码
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;}}