0

i want to write a program in C++ which takes a dll file and prints the exports and imports of it . i use loadLibrary function like the code below . but it always go to the first if and return error , it looks like that the file is not loading or it is empty . does anybody know what is the problem or knows a better way ?

#include <windows.h>

int main() {
    HINSTANCE hDLL = LoadLibrary("mylibrary.dll");

    if (hDLL == NULL) {
        // Handle error here
        return 1;
    }

    // Now that the DLL is loaded, you can call functions from it using GetProcAddress
    typedef void (*MyFunction)();
    MyFunction myFunction = (MyFunction)GetProcAddress(hDLL, "myFunction");

    if (myFunction == NULL) {
        // Handle error here
        return 1;
    }

    // Call the function
    myFunction();

    // Free the DLL when you're done using it
    FreeLibrary(hDLL);

    return 0;
}

i have tried some ways but none of them take me to the right point

  • Related, if not duplicate: https://stackoverflow.com/questions/11657968/how-to-get-list-of-functions-inside-a-dll-managed-and-unmanaged – Aconcagua Jun 13 '23 at 08:28
  • 1
    Basic check-list: 1) ensure the DLL exists [in a location where it can be found](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order); 2) ensure that, if you rely on the [current working directory](https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getcurrentdirectory), that it's _actually_ what you expect; 3) ensure your DLL is the correct architecture: _e.g._ if your DLL is 64-bits, make sure your program is too; 4) check the result of `GetLastError`, as detailed in the docs for `LoadLibrary`. – paddy Jun 13 '23 at 08:29
  • Did you export your function in the dll code, and marked it as `extern C` ? Look up Name Mangling if you have not. There are tools to see all export of a DLL, check https://stackoverflow.com/questions/4438900/how-to-view-dll-functions – nick Jun 13 '23 at 09:03

0 Answers0