c++ - “dll_file”可能是 '0' : This does not adhere to the specification for the function 'GetProcAddress'

标签 c++ dll getprocaddress

所以我想使用我创建的 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;
}

Warning at the 8th line: "'dll_file' could be '0': this does not adhere to the specification for the function '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
}

关于c++ - “dll_file”可能是 '0' : This does not adhere to the specification for the function 'GetProcAddress' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58384491/

相关文章:

c++ - 将整数数组传递给函数时计算元素数量时得到不同的结果

.net - 调试与发布 dll 大小

c++ - 使用 MATLAB 中的系数在 C++ 中设计 FIR 滤波器,滤波器未给出正确的结果

c++ - 未初始化 auto_ptr 时 get() 是否可靠?

Java Native Interface - 在 Java 中使用第三方 dll 文件

c++ - 在 C++ 中以编程方式从 DLL 中获取 DLL 版本 - 再次

c++ - LoadLibrary 的 STATUS_STACK_BUFFER_OVERRUN

java - 如何在 JNA 中使用 GetProcAddress?

c++ - 如何使用 WinDbg 查找 `GetProcAddress` 函数的过程名称

c++ - 生成器模式 : making sure the object is fully built