Currently I have a C++ program that is using a small ASM helper routine just because I dont know how to make it in C. This routine modifies 2 entries in IAT (HeapAlloc and HeapFree). How would a C version look like?

Heres the code:



.386
.model flat,stdcall
option casemap:none

externdef _imp__HeapAlloc@12:dword
externdef _imp__HeapFree@12:dword

.code

InstallHeapDebug proc pNewAllocProc, pOldAllocProc, pNewFreeProc, pOldFreeProc

mov ecx,_imp__HeapAlloc@12
mov eax,pOldAllocProc
mov [eax],ecx
mov eax,pNewAllocProc
mov _imp__HeapAlloc@12,eax

mov ecx,_imp__HeapFree@12
mov eax,pOldFreeProc
mov [eax],ecx
mov eax,pNewFreeProc
mov _imp__HeapFree@12,eax
ret
InstallHeapDebug endp

end



and the C part is:



#if ALLOCDEBUG
typedef HANDLE (WINAPI* LPHEAPALLOC)(HANDLE, DWORD, UINT);
typedef BOOL (WINAPI* LPHEAPFREE)(HANDLE, DWORD, void *);
static LPHEAPALLOC oldHeapAlloc;
static LPHEAPFREE oldHeapFree;
static dwHeapCount = 0;
extern "C" HANDLE _heapid;
extern "C" void WINAPI InstallHeapDebug(void *, LPHEAPALLOC *, void *, LPHEAPFREE *);

static void * WINAPI myHeapAlloc(HANDLE heapid, DWORD dwFlags, UINT dwSize)
{
void * pMem;
char szStr[128];

pMem = oldHeapAlloc(heapid,dwFlags,dwSize);
if (pMem)
dwHeapCount++;
wsprintf(szStr,"%u. HeapAlloc(%u)=%X\r\n", dwHeapCount, dwSize, pMem);
OutputDebugString(szStr);
return pMem;
}
static BOOL WINAPI myHeapFree(HANDLE heapid, DWORD dwFlags, void * pMem)
{
BOOL rc;
char szStr[128];

if (rc = oldHeapFree(heapid, dwFlags, pMem))
dwHeapCount--;
wsprintf(szStr,"%u. HeapFree(%X)=%u\r\n",dwHeapCount, pMem, rc);
OutputDebugString(szStr);
return rc;
}

#endif

.... and to initialize

#if ALLOCDEBUG
InstallHeapDebug(myHeapAlloc, &oldHeapAlloc, myHeapFree, &oldHeapFree);
#endif



Problem in C is that I cannot define



extern "C" LPHEAPALLOC _imp__HeapAlloc@12
extern "C" LPHEAPALLOC _imp__HeapFree@12


because the compiler doesnt like the "@"s.


japheth
Posted on 2002-04-04 02:13:00 by japheth
Maybe you can build a LIB file from the assembly version and link it after to any C, C++ or assembly program ?
Posted on 2002-04-04 03:17:31 by JCP