Getting a dll name (not the name of the exe running the process)

Posted on 2005-04-05
Medium Priority
Last Modified: 2008-01-09

I am writing an C++ ISAPI wildcard extension and I want to use the Event Log.  I have ripped off some code which is doing this quite nicely.  However when I use GetModuleFileName with parameter 1 (hModule) set to NULL I get the IIS worker process address instead of the DLL (i.e. the exe name (as documented!)).  I would quite like to install the event source into the registry every time the dll calls GetExtensionVersion even though this is a little pointless - it just seems to keep manual config and possible errors at bay.  To do this I want to be able to get the [FILEPATH]\[FILENAME] of the DLL.  

Could anyone tell me how to do this ?  If the answer is to get a hold of the hModule handle could you give me a fairly clear clue as to how to do this please as I'm not much cop with C++.  


Question by:gringogordo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Assisted Solution

AlexFM earned 240 total points
ID: 13705571
You need to use Dll handle passed as parameter to DllMain function.

HINSTANCE g_hDll;   // Dll handle

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
    if ( fdwReason == DLL_PROCESS_ATTACH )
        g_hDll = hinstDLL;


Having Dll handle in global g_hDll variable you can use it as parameter of GetModuleFileName.

Author Comment

ID: 13705909
As this is an ISAPI extension the entry point is


and so I can't pass a parameter in and DLL Main is not the entry point.  I think this means that I can't use this solution.  Is there another way (or have I misunderstood ...!)


LVL 86

Assisted Solution

jkr earned 255 total points
ID: 13708057
>> As this is an ISAPI extension the entry point is [...]

No. This is just the 1st function that is called from IIS. The entry point still is 'DllMain()' (unless specified differently in your linker settings).

An alternative method would be to

LPCSTR GetCurrentModuleName () {

    HMODULE hMod;
    static char acModule [ MAX_PATH];
    CONTEXT ctx;

    GetThreadContext( GetCurrentThread(), &ctx);
    VirtualQuery( ctx.Eip, &mbi, sizeof ( mbi));
    hMod = ( HMODULE) mbi.AllocationBase;
    GetModuleFileName ( hMod, acModule, sizeof ( acModule));

    return acModule;
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 13717455
I have used this practically verbatum (I added some casting as I had a couple of complie errors)


GetThreadContext( GetCurrentThread(), &ctx);
VirtualQuery( TESTVAR, &mbi, sizeof ( mbi)); changed param1

But I still get the exe name, in this case


Any ideas?



LVL 39

Accepted Solution

itsmeandnobodyelse earned 255 total points
ID: 13727294
>>>> VirtualQuery( ctx.Eip, &mbi, sizeof ( mbi));

VirtualQuery needs a pointer to the base address of the region of pages to be queried. So I would try to call it

        VirtualQuery( &ctx.Eip, &mbi, sizeof ( mbi));

That also should solve the casting problem.

Are you sure that the current thread runs in the DLL?

>>>> The entry point still is 'DllMain()'

I thing jkr is right. Did you search for DllMain in your project files?

Regards, Alex

Author Comment

ID: 13770912
Sorry I will get back to this and allocate the points if its successful, I have been sidetracked.  I had a quick go but I seemd to get nothing back by passing the reference.  I didn't have time to have a proper look though so I will hopefully have another look soon.


Author Comment

ID: 13814080
I looked for DLLMain but it doesn't have one (although I have based my app on the Wildcardmap ISAPI wildcard extrension SDK sample.  If you create a virgin project using the Visual Studio template there is one).

Using the original with the casting I get the IIS process exe address and using the reference I get a blank.  I assume that this exe uis running the dll in the thread called but I don't know this for sure.  And as even if I hardcode the dll path and name in the registry I can't get it to 'locate' the event message ID's (i.e. I get unkonwn messages in the event log) - although I have a simple console app where I have done this succesfully.  I'm beginning to think its an ISAPI issue.  




Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question