You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Ward Willats <wa...@wardco.com> on 2000/04/06 18:38:54 UTC

Win32 Message Loader Hack

Hello Everyone.

For our Win32 deployment at work I changed the MSVC6 project to 
rename the output DLL (we use differently named debug and release 
versions). This broke the message loader since it needs to know its 
own name to find its Windows' module handle. Examining the code 
reveals the development version has the same problem when the loader 
and messages are out in a separate DLL from the parser.

The code below can recover a Win32 module handle without knowing the 
module name. It relies on the fact that a module handle in Win32 is 
the base address of the image, and that the PE image loader maps 
.text sections in a contiguous block from the base. Someone sent it 
into "Windows Developer's Journal" back in 1998.

This approach obviously has risks, and violates one of the few design 
esthetics Windows has (handles) :-) , but in real-life Microsoft is 
unlikely to change either of the things this trick relies on for the 
foreseable future. It works on 95,98,NT and 2K. Probably not on 
Win32s or Alpha, but I haven't tried it.

Anyway, this is probably not something for the general source base, 
but might be useful in oddball individual situations. (And my work is 
about as oddball as you can get!)

-- Ward

static void ward_foo( void ) {}

Win32MsgLoader::Win32MsgLoader(const XMLCh* const msgDomain) :

     fDomainOfs(0)
     , fModHandle(0)
     , fMsgDomain(0)
{

     // BEGIN HACK
     // This trick works w/out knowing our own name 05Apr00
     MEMORY_BASIC_INFORMATION mbi;
     if ( ::VirtualQuery( ward_foo, &mbi, sizeof(mbi) ) )
         fModHandle = (HMODULE) mbi.AllocationBase;
     else
     {
     // END HACK

         fModHandle = ::GetModuleHandleA(XML4C_DLLName);
         if (!fModHandle)
         {



RE: Win32 Message Loader Hack

Posted by Linda Derezinski <li...@interfacecontrol.com>.
We do the same here.  adding _d for the debug versions.  Also we have to
change all filenames with - to _ since dashes are not compatable with our
source control.

Linda Derezinski
Interface & Control Systems Inc.

-----Original Message-----
From: Ward Willats [mailto:ward@wardco.com]
Sent: Thursday, April 06, 2000 12:39 PM
To: xerces-c-dev@xml.apache.org
Subject: Win32 Message Loader Hack


Hello Everyone.

For our Win32 deployment at work I changed the MSVC6 project to
rename the output DLL (we use differently named debug and release
versions).