|
-
January 27th, 2006, 09:08 AM
#1
Getting DLL filename
Is it possible to get the filename of the DLL the current code is in? Of course I could hardcode it but somebody could later change the output filename from "a.dll" to "b.dll" - is it possible to obtain it programatically? Hope the question is clear... I could use the GetModuleFileName function but I then need to get the module handle. Any suggestion? Thanks in advance.
-
January 27th, 2006, 09:50 AM
#2
Re: Getting DLL filename
Check out the Toolhelp32 functions. Module32First, ...
sulu
Yay 100+ rep! Thnx all for Rating my Posts and deeming them worthy 
-
January 27th, 2006, 11:36 AM
#3
Re: Getting DLL filename
 Originally Posted by Pavel Kotrc
I could use the GetModuleFileName function but I then need to get the module handle. Any suggestion? Thanks in advance.
The instance handle is being available in 'DllMain()'...simply store this instance so that you can later use it with 'GetModuleFileName()'....
-
January 27th, 2006, 12:07 PM
#4
Re: Getting DLL filename
 Originally Posted by Pavel Kotrc
Is it possible to get the filename of the DLL the current code is in? Of course I could hardcode it but somebody could later change the output filename from "a.dll" to "b.dll" - is it possible to obtain it programatically? Hope the question is clear... I could use the GetModuleFileName function but I then need to get the module handle. Any suggestion? Thanks in advance.
Just wanted to add that if you want to extract only file name, then you can use ::PathFindFileName(..) from the result you getting from ::GetModuleHandle(..)
Cheers
-
January 27th, 2006, 01:02 PM
#5
Re: Getting DLL filename
 Originally Posted by Andreas Masur
...simply store this instance so that you can later use it...
... or use GetModuleHandle(NULL);
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
January 27th, 2006, 01:08 PM
#6
Re: Getting DLL filename
 Originally Posted by VladimirF
... or use GetModuleHandle(NULL);
Passing NULL will always return you handle to the process ( exe ) and not the dll ..
-
January 27th, 2006, 01:17 PM
#7
Re: Getting DLL filename
 Originally Posted by kirants
Passing NULL will always return you handle to the process ( exe ) and not the dll ..
Right, of course. Sorry...
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
January 27th, 2006, 02:10 PM
#8
Re: Getting DLL filename
 Originally Posted by Andreas Masur
The instance handle is being available in 'DllMain()'...simply store this instance so that you can later use it with 'GetModuleFileName()'.... 
Quite rare thing (but not impossible) when you cannot rely on DllMain. For example, its code is placed in static library which can be used for dll build. There's no opportunity to obtain module hinstance from outer world.
For this case a man can use one nifty trick:
Code:
HINSTANCE GetMyInstance()
{
MEMORY_BASIC_INFORMATION mbi = {0};
if( VirtualQuery(GetMyInstance, &mbi, sizeof(mbi)) )
{
return (HINSTANCE)mbi.AllocationBase;
}
return NULL;
}
Best regards,
Igor
-
January 27th, 2006, 04:39 PM
#9
Re: Getting DLL filename
 Originally Posted by Igor Vartanov
Quite rare thing (but not impossible) when you cannot rely on DllMain. For example, its code is placed in static library which can be used for dll build. There's no opportunity to obtain module hinstance from outer world.
For this case a man can use one nifty trick:
Code:
HINSTANCE GetMyInstance()
{
MEMORY_BASIC_INFORMATION mbi = {0};
if( VirtualQuery(GetMyInstance, &mbi, sizeof(mbi)) )
{
return (HINSTANCE)mbi.AllocationBase;
}
return NULL;
}
Browsing through the web, I found this trick too - in a similar fashion:
Code:
MEMORY_BASIC_INFORMATION mbi;
static int dummy;
TCHAR szFileName[ MAX_PATH ];
VirtualQuery( &dummy, &mbi, sizeof(mbi) );
HMODULE hMod = (HMODULE) mbi.AllocationBase;
GetModuleFileName( hMod, szFileName, sizeof(szFileName) );
Anyway, thanks a lot!
-
January 30th, 2006, 04:01 AM
#10
Re: Getting DLL filename
Actually it doesn't matter who gave you the idea, me or anybody else - the main thing is you've got it. And you're welcome.
Best regards,
Igor
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|