CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    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.

  2. #2
    Join Date
    Nov 2002
    Location
    Washington, D.C.
    Posts
    264

    Re: Getting DLL filename

    Check out the Toolhelp32 functions. Module32First, ...

    sulu
    Yay 100+ rep! Thnx all for Rating my Posts and deeming them worthy

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: Getting DLL filename

    Quote 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()'....

  4. #4
    Join Date
    May 2005
    Posts
    4,954

    Re: Getting DLL filename

    Quote 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
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Getting DLL filename

    Quote 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...

  6. #6
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Getting DLL filename

    Quote Originally Posted by VladimirF
    ... or use GetModuleHandle(NULL);
    Passing NULL will always return you handle to the process ( exe ) and not the dll ..

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Getting DLL filename

    Quote 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...

  8. #8
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: Getting DLL filename

    Quote 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

  9. #9
    Join Date
    Mar 2001
    Location
    Czech Republic
    Posts
    78

    Resolved Re: Getting DLL filename

    Quote 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!

  10. #10
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    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
  •  





Click Here to Expand Forum to Full Width

Featured