SomeOtherFunction(void)
{
int x = 1, y = 2;
float fpv1 = 1.2, fpv2 = 3.4;
BarFunction( x, fpv1, fpv2, y );
}
void BarFunction( int a, float b, float c, int d )
{
•••
}
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
#ifndef _M_IA64
#error "This program only runs on an IA64 system\n" );
#endif
PVOID GetGlobalPointerFromHMODULE( HMODULE hModule );
int main()
{
// Get the "address" of function main(). This address is really
// a PLABEL_DESCRIPTOR, defined in WINNT.H
PLABEL_DESCRIPTOR * pLabelDesc = (PLABEL_DESCRIPTOR *)&main;
// Print out the PLABEL_DESCRIPTOR fields
printf( "EntryPoint: %p\n", pLabelDesc->EntryPoint );
printf( "GlobalPointer: %p\n", pLabelDesc->GlobalPointer );
// Just for fun, find out and display the GlobalPointer of this EXE,
// using the RVA stored in the PE header. This GlobalPointer should
// match the GlobalPointer from above.
HMODULE hModule = GetModuleHandle( 0 ); // Get HMODULE of this
// program
PVOID globalPointer = GetGlobalPointerFromHMODULE( hModule );
printf( "GlobalPointer from PE header: %p\n", globalPointer );
return 0;
}
// MakePtr is A handy macro function for creating pointers of the
// appropriate type. It adds the two values as integral types, and cast
// the result to the desired pointer type
#define MakePtr(cast, ptr, addValue) \
(cast)((DWORD_PTR)(ptr) + (DWORD_PTR)(addValue))
PVOID GetGlobalPointerFromHMODULE( HMODULE hModule )
{
// Point to the "DOS" header so that we can locate the "PE" header
PIMAGE_DOS_HEADER pDosHdr = (PIMAGE_DOS_HEADER)hModule;
// Make a pointer to the PE32+ header
PIMAGE_NT_HEADERS pNTHdr = MakePtr( PIMAGE_NT_HEADERS,
hModule,
pDosHdr->e_lfanew );
// Get the RVA of the GlobalPointer from the PE's DataDirectory
DWORD gpRVA = pNTHdr->OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_GLOBALPTR].VirtualAddress;
// Add the RVA to the module load address to form the GlobalPointer
// address
return MakePtr( PVOID, hModule, gpRVA );
}