2

所以我想使用我创建的 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' 的规范。”

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

4

1 回答 1

2

如果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
}
于 2019-10-14T22:00:47.103 回答