2

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.

Patch
  • 694
  • 1
  • 10
  • 29

1 Answers1

3

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
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • Good point but still it prints "0" if I print getlasterror I checked – Patch Oct 14 '19 at 22:03
  • You are getting a warning from the compiler for things that _might_ happen. If you fail to `LoadLibrary` and still call `GetProcAddress` you may get problems. If you fix what I suggested, the warning will go away. – Ted Lyngmo Oct 14 '19 at 22:05
  • I see but it still my code doesn't work for some reason I do get what you are saying. – Patch Oct 14 '19 at 22:06
  • Did the warning go away? – Ted Lyngmo Oct 14 '19 at 22:07
  • Will the warning be fixed if i add "EXIT_FAILED" in failed()? And yeah it did fix it the problem is I need to say that encryption = to something if it's not good – Patch Oct 14 '19 at 22:10
  • Do you mean to terminate the program with `std::exit(EXIT_FAILED);`? Sure, that would work but one can usually handle it better. In my example above, `encryption` will be `nullptr` afterwards if `LoadLibrary` or `GetProcAddress` failed. – Ted Lyngmo Oct 14 '19 at 22:14
  • Yeah it fixed the warning, thanks but I still have an error in the code it's so weird – Patch Oct 14 '19 at 22:16
  • @Bor ok, that's some other problem then. Back to bug-hunting :-) – Ted Lyngmo Oct 14 '19 at 22:17
  • 1
    [why is using exit() considered bad](https://stackoverflow.com/questions/25141737/) – Remy Lebeau Oct 14 '19 at 22:32