News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Correct way to set up a TYPEDEF from C/C++

Started by Lonewolff, March 16, 2016, 01:06:23 PM

Previous topic - Next topic

Lonewolff

Hi Guys,

Just wondering if this is the right way to bring over a TYPEDEF from a C/C++ header.


;typedef enum D3D_DRIVER_TYPE
;    {
;        D3D_DRIVER_TYPE_UNKNOWN = 0,
;        D3D_DRIVER_TYPE_HARDWARE = ( D3D_DRIVER_TYPE_UNKNOWN + 1 ) ,
;        D3D_DRIVER_TYPE_REFERENCE = ( D3D_DRIVER_TYPE_HARDWARE + 1 ) ,
;        D3D_DRIVER_TYPE_NULL = ( D3D_DRIVER_TYPE_REFERENCE + 1 ) ,
;        D3D_DRIVER_TYPE_SOFTWARE = ( D3D_DRIVER_TYPE_NULL + 1 ) ,
;        D3D_DRIVER_TYPE_WARP = ( D3D_DRIVER_TYPE_SOFTWARE + 1 )
;    } D3D_DRIVER_TYPE;

D3D_DRIVER_TYPE STRUCT
D3D_DRIVER_TYPE_UNKNOWN WORD 0
D3D_DRIVER_TYPE_HARDWARE WORD 1
D3D_DRIVER_TYPE_REFERENCE WORD 2
D3D_DRIVER_TYPE_NULL WORD 3
D3D_DRIVER_TYPE_SOFTWARE WORD 4
D3D_DRIVER_TYPE_WARP WORD 5
D3D_DRIVER_TYPE ENDS


Thanks in advance :)

jj2007

typedef != struct

For examples, see
\Masm32\include\COMHelper.INC
\Masm32\include\windows.inc

TouEnMasm

enum in c++ are DWORD not word.
There is no enum in masm syntax,so you must choose a translate who give the correct result.
here one
d3dcommon.sdk
http://masm32.com/board/index.php?topic=563.msg4563#msg4563

Quote
D3D_DRIVER_TYPE TYPEDEF DWORD

;D3D_DRIVER_TYPE   UNION DEFALIGNMASM
   D3D_DRIVER_TYPE_UNKNOWN   EQU <0 >
   D3D_DRIVER_TYPE_HARDWARE   EQU <(D3D_DRIVER_TYPE_UNKNOWN    + 1 )>
   D3D_DRIVER_TYPE_REFERENCE   EQU <(D3D_DRIVER_TYPE_HARDWARE    + 1 )>
   D3D_DRIVER_TYPE_NULL   EQU <(D3D_DRIVER_TYPE_REFERENCE    + 1 )>
   D3D_DRIVER_TYPE_SOFTWARE   EQU <(D3D_DRIVER_TYPE_NULL    + 1 )>
   D3D_DRIVER_TYPE_WARP   EQU <(D3D_DRIVER_TYPE_SOFTWARE    + 1 )>
;D3D_DRIVER_TYPE      ENDS

Fa is a musical note to play with CL

Lonewolff


jj2007

Quote from: ToutEnMasm on March 16, 2016, 05:40:28 PMThere is no enum in masm syntax

Enum

include \masm32\MasmBasic\MasmBasic.inc
  Init
  Enum 0:D3D_DRIVER_TYPE_UNKNOWN, D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_REFERENCE
  Enum D3D_DRIVER_TYPE_NULL, D3D_DRIVER_TYPE_SOFTWARE, D3D_DRIVER_TYPE_WARP
  Print Str$("Example A: D3D_DRIVER_TYPE_SOFTWARE=%i\n", D3D_DRIVER_TYPE_SOFTWARE)

  ; same with standard Masm syntax:
  D3D_DRIVER_TYPE_UNKNOWN        = 0
  D3D_DRIVER_TYPE_HARDWARE       = D3D_DRIVER_TYPE_UNKNOWN + 1
  D3D_DRIVER_TYPE_REFERENCE      = D3D_DRIVER_TYPE_HARDWARE + 1
  D3D_DRIVER_TYPE_NULL           = D3D_DRIVER_TYPE_REFERENCE + 1
  D3D_DRIVER_TYPE_SOFTWARE       = D3D_DRIVER_TYPE_NULL + 1
  D3D_DRIVER_TYPE_WARP           = D3D_DRIVER_TYPE_SOFTWARE + 1
  Inkey Str$("Example B: D3D_DRIVER_TYPE_SOFTWARE=%i", D3D_DRIVER_TYPE_SOFTWARE)
EndOfCode


Output:
Example A: D3D_DRIVER_TYPE_SOFTWARE=4
Example B: D3D_DRIVER_TYPE_SOFTWARE=4

BolterVi

Here is also a small enum macro:

ENUM MACRO prefix:REQ,data_type:REQ,increaser:REQ, args:VARARG
LOCAL _cnt, _inc, _pos, _var
IFNB <args>
_var TEXTEQU <0>
_pos = 0
_cnt = 0
FOR _arg, <args>
_pos INSTR <_arg>, <=>
IF _pos NE 0
_var SUBSTR <_arg>, _pos+1
IFDIF <prefix>,<0>
@CatStr(<prefix>, @SubStr(<_arg>, 1, _pos-1)) data_type _var
ELSE
@SubStr(<_arg>, 1, _pos-1) data_type _var
ENDIF
_cnt = 1
ELSE
IFDIF <prefix>,<0>
@CatStr(<prefix>, _arg) data_type _var + _cnt
ELSE
_arg data_type _var + _cnt
ENDIF
_cnt = _cnt + increaser
ENDIF

ENDM
ENDIF
ENDM


usage:

ENUM D3D_DIRVER_TYPE_, EQU, 1, HARDWARE, REFERENCE, NULL, SOFTWARE, WARP


then you can access them just straight like:

mov eax, D3D_DRIVER_TYPE_HARDWARE

or such.

this was based on a macro I've found some time ago (could also be posted here somewhere?)
My version could be improved imo (atleast it works for me so far).

Regards

jj2007

Quote from: BolterVi on March 17, 2016, 04:40:39 AM
Here is also a small enum macro:

ENUM   MACRO   prefix:REQ,data_type:REQ,increaser:REQ, args:VARARG

You are giving me ideas :biggrin:
  Enum #D3D_DRIVER_TYPE_, 0:UNKNOWN, HARDWARE, REFERENCE, NULL, SOFTWARE, WARP
  Enum #, 100:bla1, bla2, bla3


Could you give an example where data_type would be different from EQU, or increaser different from 1?

BolterVi

Well the data_type could be removed i guess, was just an idea if you want an enumeration of variables for example so you can set it to DWORD or WORD or such. ;) But yeah EQU would be enough.
Well in my case it was for working with offsets. I had about 5 offsets for BYTE variables and  ~2-3 for WORDS. Was just to make it possible to access them easier ;) But yeah also something which can be removed or redefined so the default is 1 and you can change it with an % or something after a name


ENUM TEST_, A=0%5, B, C, D, E ;Increase value = 5
ENUM TEST2_,A=0,B,C,D,E ;Increase value = 1 (default)

^ w/o data_type and increaser argument.

Edit: Maybe i rewrite the whole enum macro again, cleaner with more features and such :)

Regards :)

fearless

I found this enum macro somewhere (not sure where, but it seems to work) that i use, allows you to specify the start integer

; enum macro is used to enumerate constants - similar to how enum works in c.
enum macro _param_:VARARG
    _segname_ textequ @CurSeg
    _val_=0
    % forc _chr_,_segname_
        _val_=_val_+'&_chr_'
    endm
    IFNB <_param_>
        _count_=0
        _temp_ textequ <0>
        _ldata_ textequ <_DATA>
        for _arg_,<_param_>
            _pos_ instr <_arg_>,<=>
            if _pos_ ne 0
                _temp_ SubStr <_arg_>,_pos_+1
                if _val_ ne 179H ;;179H ='_' + 'D' + 'A' + 'T' + 'A'
                    @SubStr(<_arg_>,1,_pos_-1) equ _temp_
                else
                    @SubStr(<_arg_>,1,_pos_-1) dd _temp_
                endif
                _count_=1
            else
                if _val_ ne 179H ;;179H ='_' + 'D' + 'A' + 'T' + 'A'
                    _arg_ equ _temp_+_count_
                else
                    _arg_ dd _temp_+_count_
                endif
                _count_=_count_+1
            endif
        endm
    ENDIF
endm


enum CB_INITDEBUG,CB_STOPDEBUG,CB_CREATEPROCESS,CB_EXITPROCESS,CB_CREATETHREAD,CB_EXITTHREAD,CB_SYSTEMBREAKPOINT,CB_LOADDLL,CB_UNLOADDLL,CB_OUTPUTDEBUGSTRING,CB_EXCEPTION
enum CB_BREAKPOINT=11d,CB_PAUSEDEBUG,CB_RESUMEDEBUG,CB_STEPPED,CB_ATTACH,CB_DETACH,CB_DEBUGEVENT,CB_MENUENTRY,CB_WINEVENT,CB_WINEVENTGLOBAL


due to line length limits sometimes you have to split up the enums, first enum start at 0 for CB_INITDEBUG, 1 for CB_STOPDEBUG and so on. CB_BREAKPOINT starts at 11, CB_PAUSEDEBUG continues with 12, CB_RESUMEDEBUG is 13 and so on