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

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

所以我想使用我创建的 DLL,但我有一个非常奇怪的警告,我没有看到任何人有这个。我检查了如果 LoadLibray returns "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;
}

Warning at the 8th line: "'dll_file' could be '0': this does not adhere to the specification for the function 'GetProcAddress'."

一切正常,当我 运行 它没有写任何错误。

如果 LoadLibrary 出现任何问题,您调用 failed() 打印错误代码和 returns.

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
}