DrakeMcCain
DrakeMcCain

Reputation: 159

How to show Windows' "DLL not found" error in my program in C++?

I have searched what I am looking for in this website; but I don't understand.

So, I have written this question.

I wrote a .dll file and a program in C++ (Code Blocks IDE). If dll file and program are in same directory, my program will work.

But if I delete dll file and execute my program, Windows shows me this error:

"xxx.exe has stopped working."

I want to "The program can't start because xxx.dll is missing from your computer." message instead of this.

What should I do?

Upvotes: 0

Views: 1375

Answers (1)

Tiago Cunha
Tiago Cunha

Reputation: 169

There are three kinds of linking you can do.

  • Load-Time linking is when a DLL is loaded automatically when your program starts up. Windows finds this DLL usually in the same folder as the executable.
  • Run-Time linking is when you specifically load a DLL by calling LoadLibrary in your code.

When the application calls the LoadLibrary or LoadLibraryEx functions, the system attempts to locate the DLL (for details, see Dynamic-Link Library Search Order). If the search succeeds, the system maps the DLL module into the virtual address space of the process and increments the reference count. If the call to LoadLibrary or LoadLibraryEx specifies a DLL whose code is already mapped into the virtual address space of the calling process, the function simply returns a handle to the DLL and increments the DLL reference count. ~ taken from here

What you want to do is a run-time link to your dll and test the result instead of what you are currently doing.

  • Visual Studio offers a third option, delay-loaded DLL's.~ MSalters

which handles the loadLibrary calls for you.

Upvotes: 1

Related Questions