c++dllgetprocaddress

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


So I would like to use the DLL that I created, and I have this really weird warning I didn't see anyone has this one. I checked if LoadLibray returns "NULL", and this is not the case.

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'."

Everything works, it doesn't write any errors when I run it.


Solution

  • If anything goes wrong in LoadLibrary you call failed() that prints the error code and 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");
    

    If LoadLibrary fails, you shold not use that dll_file to call 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
    }