The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Artoo on February 20, 2005, 08:21:11 AM

Title: Static Funbtion pointers
Post by: Artoo on February 20, 2005, 08:21:11 AM
Hi,

In a C project i have a static funtion defined as:

static LRESULT CALLBACK WndProc(  HWND hwnd,        UINT uMsg,    WPARAM wParam,   LPARAM lParam )

and get the address of this function like so:

BYTE * pWndProc = (BYTE *)&WndProc;

Now trying to convert this to assembler code Ive tried this:

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
.
.
.
WndProc endp

AnotherFunction proc
      MOV      EAX, WndProc
AnotherFunction endp

But this doesnt work. As it assmbles to this:

mov         eax,dword ptr [@ILT+20(_WndProc@16) (401019h)]  

where the instruction at 401019h is:

jmp         WndProc (40128Ah)  

So my question is, how do I get rid of this ILT (table lookup) and use the proper address of WndProc.  I guess the"static" in the C code tells the compiler to do something different. What do I have to do in my MASM code for the same effect?

Your help is much appreciated!


Title: Re: Static Funbtion pointers
Post by: hutch-- on February 20, 2005, 08:53:08 AM
You can build a different set of prototypes using the L2EXTIA.EXE tool in masm32 which will produce the address directly in code rather than the jump table at the end but there is no gain doing it.

In MASM as with any normal API based code you set the address of the WndProc in the WNDCLASSEX structure using OFFSET WndProc.
Title: Re: Static Funbtion pointers
Post by: AeroASM on February 20, 2005, 09:02:11 AM
To get the address of a procedure you can use mov eax,offset WndProc as hutch said, because offset is like * in C.

An alternative is lea eax,WndProcFunctionally they are equivalent, but the first one is faster.

You can use exactly the same syntax for variables as well.
Title: Re: Static Funbtion pointers
Post by: Artoo on February 20, 2005, 09:54:46 AM
I tired:

MOV      EAX, OFFSET ClockWndProc

but that gaves me the same address of 0x00401019, which is in the ILT.  What am I doing wrong?
Title: Re: Static Funbtion pointers
Post by: Artoo on February 20, 2005, 10:58:57 AM
Ok, Ive trracked it down to it being a debug, which creates the ILT ("Incremental Link Table").  In release builds no ILT is created and the real address of WndProc is used.

So my question now is must a ILT be used in my debug EXE?

Title: Re: Static Funbtion pointers
Post by: AeroASM on February 20, 2005, 08:47:55 PM
You are doing nothing wrong. 0x00401??? is a normal address for something in the code section, and Windows will happily receive it.

With Incremental Link, only the procedures which have been changed are recompiled and relinked. The ILT helps the linker keep track. You can disable it by adding the switch /INCREMENTAL:NO when you link.
Title: Re: Static Funbtion pointers
Post by: Artoo on February 21, 2005, 06:45:14 AM
Thanks everyone for your help.

The problem was with debug builds creating a ILT.  The /INCREMENTAL:NO  fixed it.