Jump to content


Photo

[Ahk_H+Hv2] MCodeH based on DynaCall


  • Please log in to reply
No replies to this topic

#1 HotKeyIt

HotKeyIt
  • Fellows
  • 6129 posts

Posted 04 December 2011 - 10:05 PM

This is equivalent function to MCode, many thanks Laszlo.

Reqires latest AutoHotkey_H

Advantages using DynaCallare:
- Default parameters
- Parameters shifting, move parameters around
- Faster, expecially when using default parameters

Example
VarSetCapacity(hex,9),   VarSetCapacity(bin,12),   StrPut("123",&bin,3,"CP0")

; [color=red]["tti",2][/color] means that when function is called, first passed parameter will be used to set second parameter
;    this means automatically that second passed parameter will be used for first and third passed for third parameter.
Bin2Hex:=MCodeH("8B44240C568B742408578B7C24108D14073BFA7332538A07478AC8C0E904B3093AD91ADB80E30780C3"
               . "3002D9240F881E46B1093AC81AC980E10780C13002C8880E463BFA72D05B5FC606005EC3",[color=red]["tti",2][/color]
			   , [color=orange]&hex, &bin,4[/color])

[color=indigo]Bin2Hex[][/color] ; call Bin2Hex using default parameters
MsgBox % StrGet(&hex,"CP0")           ; 31323300

VarSetCapacity(bin2,12),   StrPut("456",&bin2,3,"CP0")
Bin2Hex[&bin2] ; call Bin2Hex changing only second parameter
MsgBox % StrGet(&hex,"CP0")           ; 34353600

Bin2Hex[&bin,&hex,4] ; of course all parameters can be used.
MsgBox % StrGet(&hex,"CP0")           ; 31323300
Function:
MCodeH(h,def,p*) { ; allocate memory in static object and write Machine Code there, return DynaCall (callable object)
   static f:={}
   f.Insert(h),f.SetCapacity(f.MaxIndex(),StrLen(h)//2)
   Loop % StrLen(h)//2
      NumPut("0x" . SubStr(h,2*A_Index-1,2), f.GetAddress(f.MaxIndex()), A_Index-1, "Char")
	Return DynaCall(f.GetAddress(f.MaxIndex()),def,p*)
}