News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

structures and function pointers

Started by alphis, July 20, 2006, 01:33:21 PM

Previous topic - Next topic

alphis

Hey all. I'd like to first say this community looks very friendly and dedicated to masm. I've been meaning to post here before but couldn't until now. Well, onto the question.

I've defined a struct:

mystruct struct
something dd ?
.
.
mystruct ends

which has uninitialized data in it and everything is ok. However I'd like to also have function pointers in there. Since they are also dwords I figured I could simply have a member in it such as:

somefunction dd ?

and then later store the result of a getprocaddress() into this somefunction. However calling it using invoke requires a prototype. Now here is where two choices come in. I could either define some prototype or use call instead. I've tried both but cannot get the proper syntax.

prototype version - I define this before hand:

isomefunction PROTO STDCALL :DWORD

then try to make the "somefunction" member of the struct of this type which I cannot do!

somefunction: isomefunction <--- this fails completely

My problem here is that I don't know how to make somefunction of type isomefunction. in C/C++ I would use a standard function pointer and define its ret/params and then just make a var of that type with no problem. How can I do this in masm?

call version
push ..
push ..
call mystruct.somefunction

strangely the problem with this is that my pushes will not push members of my struct! IE: push mystruct.somedword fails during asm time.

Any help is appreciated.

CaptBobo

#1
alphis:

I'm pretty new to MASM myself, but I think you can use your function pointer and prototype in an INVOKE call by doing something like the following:


SomeFunctionPrototype PROTO STDCALL :DWORD

mov edx, SomeFuntionPointer
invoke (SomeFunctionPrototype PTR [edx]), param1


This works something like a C/C++ "typecast", if you're familiar with that.

I think you can declare an identifier to be a pointer to a particular function as follows:


SomeProto PROTO :DWORD, :DWORD

pSomeProto TYPEDEF PTR SomeProto

MyStruct STRUCT
...
pFunc pSomeProto ?

...
MyStruct ENDS


Hopefully one of the more experienced folks here can confirm this!

:bg

-Sean

Ratch

alphis,

    In MASM, a STRUCT is simply an address overlay of a section of memory.  MASM does not generate code to dynamically manipulate STRUCTs like C does.  The code below assembles correctly and  should work.  Ratch


mystruct STRUCT
 data1    DWORD ?
 data2    DWORD ?
 functadr DWORD ?
mystruct ENDS

.DATA?

myarea BYTE mystruct DUP (?)

.CODE
NOP
NOP
NOP
function:
;...
RET

START:
NOP
NOP
NOP
LEA EAX,function
MOV [myarea.mystruct.functadr],EAX
NOP
NOP
NOP
MOV EAX,[myarea.mystruct.functadr]
CALL EAX
INVOKE ExitProcess,0




drizz

lets not listen to "hard coded member"  its too hardcoded :bg,
CaptBobo is correct. This is how to do it:

;lets say you have couple of your funcs here
.code
myfunc1 proc stdcall oneparam:DWORD
ret
myfunc1 endp
myfunc2 proc c oneparam:DWORD,otherparams:VARARG
ret
myfunc2 endp
;and you want to call them trough struc
.data
myfunc1_PROTO TYPEDEF PROTO STDCALL :DWORD
PTR_myfunc1_PROTO TYPEDEF PTR myfunc1_PROTO
myfunc2_PROTO TYPEDEF PROTO C :DWORD,:VARARG
PTR_myfunc2_PROTO TYPEDEF PTR myfunc2_PROTO
MYSTRUCT struct
myf1 PTR_myfunc1_PROTO ?
myf2 PTR_myfunc2_PROTO ?
dat1 dd ?
dat2 dd ?
MYSTRUCT ends
;i'll use initialized data, otherwise you have to fill
;structure variable with offsets on runtime
mystuff MYSTRUCT <offset myfunc1,offset myfunc2,0DEADh,0BEEFh>

.code
invoke mystuff.myf1,0DEADC0DEh
invoke mystuff.myf2,31337h,1,2,3,4,5,6,7


all clear?? :)

for some more call examples see this thread
http://www.masm32.com/board/index.php?topic=5299.0
The truth cannot be learned ... it can only be recognized.