'dll_file'可以是"0":这不符合函数的规范'GetProcAddress'

'dll_file' could be '0': This does not adhere to the specification for the function 'GetProcAddress'

本文关键字:函数 不符合 GetProcAddress file dll      更新时间:2023-10-16

所以我想使用我创建的DLL,我有一个非常奇怪的警告,我没有看到有人有这个。我检查了LoadLibray是否返回"NULL",但事实并非如此。

typedef DATA_BLOB(*encryption_decryption)(DATA_BLOB, bool*);
HINSTANCE dll_file = LoadLibrary(L"dllForEncryptionNDecryptionn.dll");
if (dll_file != NULL) {
cout << "Library loaded!" << endl;
}
else {
failed();
}
encryption_decryption encryption = (encryption_decryption)GetProcAddress(dll_file,"encryption");
if(encryption != NULL)
{
cout << "Workded!" << endl;
}
else
{
failed();
}
void failed() {
cout << GetLastError() << endl;
cout << "Faild!" << endl;
}

第 8 行的警告:"'dll_file'可能是'0':这不符合函数'GetProcAddress'的规范。

一切正常,当我运行它时它不会写入任何错误。

如果LoadLibrary出现任何问题,请调用打印错误代码并返回的failed()

HINSTANCE dll_file = LoadLibrary(L"dllForEncryptionNDecryptionn.dll");
if (dll_file != NULL) {
cout << "Library loaded!" << endl;
}
else {
failed(); // returns even when dll_file is NULL
}
// so, here you don't know if it's NULL or a valid handle which is why you get the warning
encryption_decryption encryption = (encryption_decryption)GetProcAddress(dll_file,"encryption");

如果LoadLibrary失败,则不应使用该dll_file调用GetProcAddress

encryption_decryption encryption = nullptr;
HINSTANCE dll_file = LoadLibrary(L"dllForEncryptionNDecryptionn.dll");
if(dll_file) {
encryption_decryption encryption = 
(encryption_decryption)GetProcAddress(dll_file,"encryption");
} else {
// do NOT call GetProcAddress
}
if(encryption) {
// function successfully loaded
}