1
votes

I have a C++ project that has more dependencies to .dll files. How can I build the executable so, that the created .exe file finds the .dll files in a given folder relative to the .exe? I am using Visual Studio.

2

2 Answers

1
votes

You can use this code to load a dll and call a function exported in the dll:

#include <windows.h>
#include <iostream>

/* Define a function pointer for our imported
 * function.
 * This reads as "introduce the new type f_funci as the type: 
 *                pointer to a function returning an int and 
 *                taking no arguments.
 */
typedef int (*f_funci)();

int main()
{
  HINSTANCE hGetProcIDDLL = LoadLibrary("C:\\Documents and Settings\\User\\Desktop  \\fgfdg\\dgdg\\test.dll");

  if (hGetProcIDDLL == NULL) {
    std::cout << "could not load the dynamic library" << std::endl;
    return EXIT_FAILURE;
  }

  # resolve function address here
  f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "funci");
  if (!funci) {
    std::cout << "could not locate the function" << std::endl;
    return EXIT_FAILURE;
  }

  std::cout << "funci() returned " << funci() << std::endl;

  return EXIT_SUCCESS;
}
1
votes

To load a dll from an exe relative directory all you have to do is specify a path in the format of "\\mydlldir\\dllnamehere.dll" as opposed to the fully qualified path of "driveletter:\\dir\\dir2\\dirwithexeinit\\mydlldir\\dllnamehere.dll".

The first method will always look in the directory specified from where the exe exists, where the second will always look in the exact directory specified.