2

Suppose that I have a dll called it MyDll.dll

  • It is in the d:\MyWorks\MyDll.dll [it is directshow dll]

  • I want to get path of its location from inside the MyDll code.

I used boost for this: FileSystem

string path = "";
boost::filesystem::path full_path( boost::filesystem::current_path() );
path =  full_path.string();

But this give me its execution path, which is C:\Windows\system32, and not its location path which is d:\MyWorks\MyDll.dll.

How can I get a dll's location inside the same dll?

Update: By Get Module:

TCHAR path[2048];
  GetModuleFileName( NULL, path, 2048 );
  ostringstream file;

  file << path ;

  string const pathString =file.str();

  cout << "Path: " << pathString << endl;

Gives me just hex-like string : 0049EA95....

Novalis
  • 2,265
  • 6
  • 39
  • 63
  • Please look at GetModuleFileName: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197%28v=vs.85%29.aspx – Lucian Dec 22 '11 at 15:02
  • I try the GetModuleFileName get some text strange...Can not properly assign it to string maybe @Lucian – Novalis Dec 22 '11 at 15:27

4 Answers4

7

In your DllMain you receive an HINSTANCE parameter; that is actually the HMODULE of your dll, that you can use with GetModuleFileName to retrieve the fully-qualified path of your dll. To get just the directory that contains it you just have to remove the file name (you can do that with boost::filesystem, with shell path functions as well as just with a strrchr).

Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
5

You can use GetModuleFileName in order to get the full path of a module.

The first argument is a handle to the required module. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process.

If you want the path to other module, you may use GetModuleHandle to get a handle. For example:

  TCHAR path[_MAX_PATH+1]; 
  GetModuleFileName(GetModuleHandle(_T("MyDll.dll")), path, sizeof(path)/sizeof(path[0])); 
J. Calleja
  • 4,855
  • 2
  • 33
  • 54
5

Your problem is trying to see a Unicode string on an Ansi console output window. If you really want to see the result, you need to cast your strings to Ansi (with some loss of course) or you can directly use;

char path[2048];
GetModuleFileNameA(NULL, path, 2048);
cout << path;

If you want to use Unicode, use TCHAR and GetModuleFileNameW (or GetModuleFileName since your application is in unicode mode) but don't try to output to console window without casting to Ansi.

Emir Akaydın
  • 5,708
  • 1
  • 29
  • 57
  • 4
    Passing NULL to `GetModuleFileName()` will return the path for the executable. You need to pass the DLL's hInstance to get the path of the DLL. In addition, casting to ANSI will not help. You need to actually convert the string to ANSI (possibly using `MultiByteToWideChar()` or the ATL conversion routines). Alternatively, you could just send it to `std::wcout` instead. – Ferruccio Dec 22 '11 at 17:13
4
TCHAR s[MAX_PATH+1];
GetModuleFileName(hInstance, s, _countof(s));

where hInstance is a parameter of DllMain. Despite the name, it returns the full path.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • 1
    I'd like to add to this... If your `DllMain` function definition looks like this: `BOOL WINAPI DllMain(HINSTANCE hInst, DWORD reason, LPVOID)` then you would be referencing the variable `hInst` in this particular example. Per this answer, check what the parameter passed in as `HINSTANCE` is named and use that :D – Shrout1 Jan 30 '20 at 16:49