0

So, I'm trying to use a function from DLL file and for some reason I can't open the DLL file: when I'm using the LoadLibrary function it returns as NULL I'm freaking out that I can't find this bug

typedef unsigned int (WINAPI* AvVersion)(void);
void secret()
{
    HMODULE dll = LoadLibrary("Secret.dll");

    if (dll == NULL)
    {
        std::cout << "coudlnt open ""secret.dll""\nError Code:" << GetLastError() << std::endl;
        return;
    }

    AvVersion function = (AvVersion)GetProcAddress(dll, "TheAnswerToLifeTheUniverseAndEverything");
    std::cout << "result:" << function() << std::endl;
}

The code number is 126 and the name of the function is TheAnswerToLifeTheUniverseAndEverything

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Where is the dll? Is it in the working directory? – tkausl Apr 30 '18 at 20:51
  • @tkausl yes it is –  Apr 30 '18 at 20:52
  • 1
    So what did your code print out ? the value GetLastError() should tell why LoadLibrary() fails. – nos Apr 30 '18 at 20:54
  • Yes, what's the error code? You took the time to create a question on Stackoverflow but you didn't check the error code? I bet that it's "file not found". – BJovke Apr 30 '18 at 20:56
  • I searched on the MSDN and it says " The specified module could not be found. " –  Apr 30 '18 at 20:57
  • @BJovke yes it is –  Apr 30 '18 at 20:57
  • its in the directory https://ibb.co/n6rdTc –  Apr 30 '18 at 20:59
  • It has to be in the same directory as your .EXE or in the directory you're starting your EXE from, not in this one. – BJovke Apr 30 '18 at 21:00
  • Or in path, system32, etc. There's a MSDN doc stating in which order the DLL is being searched for. – BJovke Apr 30 '18 at 21:01
  • still not working :| –  Apr 30 '18 at 21:02
  • Does it matter that I use visual studio? –  Apr 30 '18 at 21:03
  • Are you launching your program from Visual studio IDE? If yes then working directory is up one level from the one in your picture, copy the DLL there. – BJovke Apr 30 '18 at 21:04
  • well, it still not working –  Apr 30 '18 at 21:06
  • I copy the dll to the upper directory where the solution is –  Apr 30 '18 at 21:07
  • 3
    `ERROR_MOD_NOT_FOUND` does not mean the DLL itself is not found. It means the DLL is found, but one of its dependent DLLs is not being found. Subtle but important difference. If your DLL has dependencies, make sure they are all in the same folder, or on the system search path. – Remy Lebeau Apr 30 '18 at 21:07
  • Copy the DLL to the same directory where your EXE is. Then open command prompt, CHDIR to that directory and start the application. If this doesn't work then DLL's dependencies are not met or you have a mismatch between the DLL and your app. For example, DLL is 32 bit and app 64 bit, DLL is in debug version and app in release (this is rare), etc. – BJovke Apr 30 '18 at 21:11

0 Answers0