0

I have a C++ program with a depedency on external Dll [lets say dependent.dll], now If I keep..

  • dependent.dll in executable folder, it works fine, but if I keep
  • dependent.dll in somne custom Folder, it doesn't.

I have tried

LPCTSTR deplibFullPath = "C:\Program Files\My_Program\Dlls\dependent.dll";
HMODULE hMod = LoadLibraryEx(deplibFullPath , NULL, LOAD_WITH_ALTERED_SEARCH_PATH);

It returns not null but whenever I access any function in dependent.dll, it doesnt works fine (crashes)

Any idea where I am doing wrong or what extra for i need to do.

Thanks

-Pankaj

Pankaj
  • 2,618
  • 3
  • 25
  • 47

3 Answers3

3

Your path is wrong. It should be

LPCTSTR deplibFullPath = "C:\\Program Files\\My_Program\\Dlls\\dependent.dll";

since \ is used to escape special commands in a string.

SinisterMJ
  • 3,425
  • 2
  • 33
  • 53
2

Maybe your dll requires other dll's (dependencies) from that same folder. Try calling AddDllDirectory before loading that one.

And I assume the single "\"s in your path is just a copy and paste error...

001
  • 13,291
  • 5
  • 35
  • 66
  • Note: That is a Windows 8 function. – Sion Sheevok Sep 26 '12 at 15:32
  • Thanks Johnny but I am currently working on Windows 7 and AddDllDirectory is available minimum on Windows 8 :( and yes, single "\s" were just typing error. – Pankaj Sep 26 '12 at 15:35
  • Ok. This might help: http://stackoverflow.com/questions/1919125/programmatically-adding-a-directory-to-windows-path-environment-variable – 001 Sep 26 '12 at 15:37
0

I got the answer, the problem was not in my LoadLibraryEx function but I was not using the GetProcAddress correctly. :(

Pankaj
  • 2,618
  • 3
  • 25
  • 47