The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: jag on February 13, 2007, 05:03:56 AM

Title: way to get module base address
Post by: jag on February 13, 2007, 05:03:56 AM
I see here ( http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx ) that in C++ there is a way to get the pe header offset of your module.

EXTERN_C IMAGE_DOS_HEADER __ImageBase;
#define HINST_THISCOMPONENT ((HINSTANCE)&__ImageBase)


I wish to do this in masm but I cannot figure out for the life of me how to.
I was thinking of something like:
mov eax, [0400000h]

but how do I get that constant (0400000h) to become relocatable and what if my module doesn't have a preferred base address of 400000h ?

I hope some bright fellow here has the solution.

Thanks in advance
Title: Re: way to get module base address
Post by: sinsi on February 13, 2007, 06:17:12 AM
If by module you mean a DLL, DLLMain is passed an HINSTANCE - from the SDK
Quote
hinstDLL
[in] Handle to the DLL module. The value is the base address of the DLL. The HINSTANCE of a DLL is the same as the HMODULE of the DLL, so hinstDLL can be used in calls to functions that require a module handle.

If you mean your EXE, GetModuleHandle *should* point to the PE header of your EXE file - it is almost always 00400000h.
Title: Re: way to get module base address
Post by: jag on February 13, 2007, 07:15:38 AM
Aye stupid me. I forgot that DllMain receives the hInstance. Thanks!
Title: Re: way to get module base address
Post by: drizz on February 13, 2007, 12:04:30 PM
thanks for the info jag, i knew about _IMPORT_DESCRIPTOR_dll stuff but didn't know about this!

EXTERNDEF C __ImageBase:IMAGE_DOS_HEADER
mov eax,offset __ImageBase

EXTERNDEF C _IMPORT_DESCRIPTOR_KERNEL32:IMAGE_IMPORT_DESCRIPTOR
add eax,_IMPORT_DESCRIPTOR_KERNEL32.Name1