News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

TYPEDEF PTR

Started by mabdelouahab, December 14, 2014, 01:05:03 AM

Previous topic - Next topic

mabdelouahab

Hi there guys
How can I identify a specific type of procedures to pass through variable.
I tried to use (TYPEDEF PROTO  / TYPEDEF PTR), but it accept any type of Procedure, do not show any error message
Why
Example

I want to pass the procedure of the type:

My_TYPEDEFPROTO TYPEDEF PROTO :DWORD,:BYTE
My__TYPEDEFPTR TYPEDEF PTR My_TYPEDEFPROTO


...
prc_03 proc a:My__TYPEDEFPTR
invoke My__TYPEDEFPTR ptr a,0,0
ret
prc_03 endp

I want to receive an error message when you pass another type of procedure

prc_00 proc 
.....
ret
prc_00 endp
prc_01 proc a:dword
.....
ret
prc_01 endp
prc_02 proc a:dword,b:byte
.....
ret
prc_02 endp
...
invoke prc_03,offset prc_00
invoke prc_03,offset prc_01
invoke prc_03,offset prc_02


But he receives all types of procedures,
What is the correct way?

dedndave

first, let me say that a BYTE type is not a good idea
in 32-bit code, you want to keep the stack 4-aligned
so, to pass a byte, use only the low byte of a DWORD type

TYPEDEF Ptr is used when the process addess is a variable

;################################################################

FUNC08  TYPEDEF PROTO :DWORD,:DWORD
PFUNC08 TYPEDEF Ptr FUNC08

;################################################################

        .DATA?
        ALIGN   4

lpfnMyFunc PFUNC08 ?

;################################################################


now, you can place the address of a PROC in lpfnMyFunc and...

        .CODE

        mov     lpfnMyFunc,MyFunc1   ;"offset" not required - the assembler knows it's an address

        INVOKE  lpfnMyFunc,dwArg2,dwArg1
;
;
;
        INVOKE  ExitProcess,0


notice that the PROC's should all have 2 DWORD arguments

MyFunc1 PROC dwArg02:DWORD,dwArg01:DWORD

    mov     eax,dwArg02
    mov     dl,byte ptr dwArg01
;
;
;
    ret

MyFunc1 ENDP

qWord

Quote from: mabdelouahab on December 14, 2014, 01:05:03 AM
How can I identify a specific type of procedures to pass through variable.
I tried to use (TYPEDEF PROTO  / TYPEDEF PTR), but it accept any type of Procedure, do not show any error message
Why
Because MASM does not support this kind of type checking.
However, you might be able to get some more control over that by setting up an sophisticated macro system, which does record the declarations.

(Be warned, the INVOKE directive in earlier MASM versions (6~9) had a bug that produce bad code for BYTE parameters.)
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

Quote from: qWord on December 14, 2014, 05:32:20 AMBecause MASM does not support this kind of type checking.
However, you might be able to get some more control over that by setting up an sophisticated macro system, which does record the declarations.

CreateInvoke does this kind of checking:

include \masm32\MasmBasic\MasmBasic.inc
  Init
  push rv(LoadLibrary, "user32")
  .if eax
      mov CreateInvoke(MyMsgBox, 4*dword), rv(GetProcAddress, eax, "MessageBoxA")
      invoke MyMsgBox, 0, Chr$("Text"), Chr$("Title"), MB_OK
  .endif
  call FreeLibrary
  Exit
end start


Note that e.g.
   invoke MessageBox, ax, chr$("text"), chr$("Title"), MB_OK
is (wrongly) accepted by all MASM versions and JWasm, despite of its PROTO specifying a DWORD:
   MessageBoxA PROTO STDCALL :DWORD,:DWORD,:DWORD,:DWORD

qWord

Quote from: jj2007 on December 14, 2014, 05:37:40 AMNote that e.g.
   invoke MessageBox, ax, chr$("text"), chr$("Title"), MB_OK
is (wrongly) accepted by all MASM versions and JWasm
no, that is an well documented feature for integer types. The problem is that earlier MASM version produce bad code for the conversion.
MREAL macros - when you need floating point arithmetic while assembling!