Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

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

Hi,

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

Thanks,

Ben.
0
gringogordo
Asked:
gringogordo
3 Solutions
 
AlexFMCommented:
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.
0
 
gringogordoAuthor Commented:
As this is an ISAPI extension the entry point is

WORD WINAPI HttpExtensionProc( EXTENSION_CONTROL_BLOCK *   pecb )

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 ...!)

Thanks,

Ben.
0
 
jkrCommented:
>> 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

inline
LPCSTR GetCurrentModuleName () {

    MEMORY_BASIC_INFORMATION mbi;
    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;
}
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

...
CONTEXT ctx;
LPCVOID TESTVAR; //new line

GetThreadContext( GetCurrentThread(), &ctx);
TESTVAR = (LPCVOID) ctx.Eip; //NEWLINE
VirtualQuery( TESTVAR, &mbi, sizeof ( mbi)); changed param1
...

But I still get the exe name, in this case

c:\windows\system32\inetsrv\w3wp.exe.  

Any ideas?

Thanks,

Ben.




0
 
itsmeandnobodyelseCommented:
>>>> 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
 
0
 
gringogordoAuthor Commented:
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.

Ben.
0
 
gringogordoAuthor Commented:
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.  

Ben.

 



0

Featured Post

Industry Leaders: 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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now