“dll_file”可能是“0”:这不符合函数“GetProcAddress”的规范

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

提问人:Patch 提问时间:10/15/2019 最后编辑:Patch 更新时间:11/29/2020 访问量:5494

问:

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

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' 的规范。

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

C++ DLL getProcAddress

评论

0赞 Patch 10/15/2019
不过,它确实打印了“Library loaded!”和“Worked!”,
0赞 Patch 10/15/2019
@TedLyngmo它打印 GetLastError 函数,我将其添加到代码中。
0赞 Ayjay 10/15/2019
这篇文章中没有问题。
0赞 Patch 10/15/2019
@Ayjay问题是如何修复该警告消息,以便我的代码能够正常工作

答:

3赞 Ted Lyngmo 10/15/2019 #1

如果调用中出现任何问题,则会打印错误代码并返回。LoadLibraryfailed()

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");

如果失败,则不应使用它来调用 。LoadLibrarydll_fileGetProcAddress

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
}

评论

0赞 Patch 10/15/2019
好点子,但如果我打印我检查的 getlasterror,它仍然打印“0”
0赞 Ted Lyngmo 10/15/2019
您从编译器收到有关可能发生的情况的警告。如果你没有打电话,仍然打电话,你可能会遇到问题。如果您修复了我的建议,警告就会消失。LoadLibraryGetProcAddress
0赞 Patch 10/15/2019
我明白了,但由于某种原因,我的代码仍然不起作用,我确实明白你在说什么。
0赞 Ted Lyngmo 10/15/2019
警告消失了吗?