因此,我想使用自己创建的DLL,并且我收到了一个非常奇怪的警告,我没有看到有人拥有这个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;
}



一切正常,运行程序时不显示任何错误消息。

最佳答案

如果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
}

关于c++ - 'dll_file'可能是 '0':这不符合 'GetProcAddress'函数的规范,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58384491/

10-09 03:03