MCode4GCC -- C/C++ to MCode Generator

Post your working scripts, libraries and tools for AHK v1.1 and older
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

08 Jun 2019, 07:49

or a function needs to be complex enough for MCode to be faster than regular AHK code?
Simply put, yes. The overhead of the dllcall outweighs any possible benefits for your example. Often, when you can replace a script loop with a compiled loop, then you can get speed benefits.

Any questions not related to joedf's app should probably go somewhere else.

Cheers.
serg
Posts: 56
Joined: 21 Mar 2015, 05:33

Re: MCode4GCC -- C/C++ to MCode Generator

08 Jun 2019, 08:17

@Helgef
Thanks for the answers!
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: MCode4GCC -- C/C++ to MCode Generator

08 Jun 2019, 08:43

dont call MCode on every loop iteration!! u do it once at the start
dont benchmark with too few iterations
dont benchmark with imprecise timers, use queryperformancecounter
dont benchmark without setbatchlines -1

for just multiplying 2 numbers * will probably be faster anyway
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

08 Jun 2019, 13:59

@Helgef @swagfag Thanks! :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
paulpma
Posts: 65
Joined: 08 Sep 2018, 22:05

Re: MCode4GCC -- C/C++ to MCode Generator

20 Nov 2019, 13:46

Hello,

Is there a way to set MCODE encoding to hex vs base64(default) in the generator?. Have been searching but without any luck. Thank you.
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

20 Nov 2019, 22:05

You have to change it under "Settings" here :thumbup: :
"Laszlo Style" for Hex
"Bentschi Style" for Base64
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
paulpma
Posts: 65
Joined: 08 Sep 2018, 22:05

Re: MCode4GCC -- C/C++ to MCode Generator

20 Nov 2019, 23:35

OMG.. thank you so much..
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

21 Nov 2019, 01:37

No worries :+1:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: MCode4GCC -- C/C++ to MCode Generator

23 Nov 2019, 23:16

bentschi's mcode:

Code: Select all

MCode(mcode)
{
  static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
  if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
    return
  if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
    return
  p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
  if (c="x64")
    DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
  if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
    return p
  DllCall("GlobalFree", "ptr", p)
}
are we sure VirtualProtect is only needed on AHKx64? and not 32?

Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 03:45

are we sure VirtualProtect is only needed on AHKx64? and not 32?
The function is known to work. However, I can't recall seeing any documentation suggesting the PAGE_EXECUTE_READWRITE flag is only for 64 bit, I've always set it for both 32 and 64 bit.

Cheers.
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 11:48

Helgef wrote:
24 Nov 2019, 03:45
are we sure VirtualProtect is only needed on AHKx64? and not 32?
The function is known to work. However, I can't recall seeing any documentation suggesting the PAGE_EXECUTE_READWRITE flag is only for 64 bit, I've always set it for both 32 and 64 bit.
i have a suspicion that VirtualProtect is necessary for 32bit as well, but i havent been able to confirm with my enduser yet

jeeswg wrote:
23 May 2018, 19:59
- [EDIT:] I noticed that the script is adding a trailing linefeed character when you click 'Copy to Clipboard'. And it adds them intermittently throughout long strings.
yeah and its only breaking up the Bentschi b64 mcode with linefeeds, its not breaking up the laszlo style hex

User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 12:01

Oops really? I must have missed this... :think:
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 12:17

looks like the CryptBinaryToString function appends newlines throughout the conversion after every 64th char. no idea why it does this

if we add the flag from msdn to the two calls, the newlines are removed:

Code: Select all

Base64enc( ByRef OutData, ByRef InData, InDataLen ) { ; by SKAN
	DllCall("Crypt32.dll\CryptBinaryToString" (A_IsUnicode?"W":"A")
		,UInt,&InData,UInt,InDataLen,UInt,0x40000001,UInt,0,UIntP,TChars,"CDECL Int")
	VarSetCapacity(OutData,Req:=TChars*(A_IsUnicode?2:1))
	DllCall("Crypt32.dll\CryptBinaryToString" (A_IsUnicode?"W":"A")
		,UInt,&InData,UInt,InDataLen,UInt,0x40000001,Str,OutData,UIntP,Req,"CDECL Int")
                msgbox %OutData%
	Return TChars
}
CRYPT_STRING_NOCRLF
0x40000000
Do not append any new line characters to the encoded string. The default behavior is to use a carriage return/line feed (CR/LF) pair (0x0D/0x0A) to represent a new line.
Windows Server 2003 and Windows XP: This value is not supported.

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 12:43

AFAIK for hex it adds CRLFs and spaces, for base64 it adds CRLFs.
So you might want to do something like this:

Code: Select all

vText := RTrim(vText, " `r`n")
vText := StrReplace(vText, "`r`n")
vText := StrReplace(vText, " ")
;vText .= "`r`n" ;add this line, if a trailing CRLF is wanted
Since the raw hex/base64 flags are not supported on Windows XP.
Either drop the flag, and always do StrReplace.
Or check the OS version number, conditionally set the flag, and do StrReplace if necessary.
E.g. something like:

Code: Select all

JEE_HexGet(vAddr, vSize, vCase:="U")
{
	local
	static vIsVistaPlus := (DllCall("kernel32\GetVersion", "UInt") & 0xFF) >= 6
	vCase := "U"
	;CRYPT_STRING_NOCRLF := 0x40000000 (not supported by Windows XP)
	;CRYPT_STRING_HEXRAW := 0xC ;to return raw hex but with a trailing enter (not supported by Windows XP)
	;CRYPT_STRING_HEX := 0x4 ;to return space/CRLF-separated text
	vFlags := vIsVistaPlus ? 0x4000000C : 0x4
	vChars := 0
	DllCall("crypt32\CryptBinaryToString", "Ptr",vAddr, "UInt",vSize, "UInt",vFlags, "Ptr",0, "UInt*",vChars)
	VarSetCapacity(vHex, vChars*2, 0)
	DllCall("crypt32\CryptBinaryToString", "Ptr",vAddr, "UInt",vSize, "UInt",vFlags, "Str",vHex, "UInt*",vChars)
	if !vIsVistaPlus
	{
		vHex := StrReplace(vHex, "`r`n")
		vHex := StrReplace(vHex, " ")
	}
	return (vCase = "L") ? vHex : Format("{:U}", vHex)
}

JEE_Base64Get(vAddr, oParams*) ;vAddr, vSize
{
	local
	static vIsVistaPlus := (DllCall("kernel32\GetVersion", "UInt") & 0xFF) >= 6
	if (oParams.Length() = 2)
		vSize := oParams.1
	if !JEE_StrIsType(oParams.1, "number")
		vSize := 0
	else if (oParams.1 = 0)
		return
	else if oParams.HasKey(1)
		vSize := oParams.1
	else
		vSize := 0
	if !JEE_StrIsType(vSize, "number")
		vSize := 0
	;CRYPT_STRING_NOCRLF := 0x40000000
	;CRYPT_STRING_BASE64 := 0x1
	vChars := 0
	vFlags := vIsVistaPlus ? 0x40000001 : 0x1
	DllCall("crypt32\CryptBinaryToString", "Ptr",vAddr, "UInt",vSize, "UInt",vFlags, "Ptr",0, "UInt*",vChars)
	VarSetCapacity(vBase64, vChars*2, 0)
	DllCall("crypt32\CryptBinaryToString", "Ptr",vAddr, "UInt",vSize, "UInt",vFlags, "Str",vBase64, "UInt*",vChars)
	return vIsVistaPlus ? vBase64 : StrReplace(vBase64, "`r`n")
}
It would be good if a few of you supported me in getting base64/hex functionality built-in.
Either my code below, or something similar. Cheers.
C++: AHK source code: Base64Get/Base64Put and HexGet/HexPut - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=75&t=64694
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 12:52

jeeswg wrote:
24 Nov 2019, 12:43
AFAIK for hex it adds CRLFs and spaces, for base64 it adds CRLFs.
for Hex i'm not seeing any extra CRLF nor spaces

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 13:00

StringToBinary is using fmt=12 by default:
CRYPT_STRING_HEXRAW := 0xC to return raw hex but with a trailing enter (not supported by Windows XP).
Anyhow, at least that explains the trailing enter.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
guest3456
Posts: 3454
Joined: 09 Oct 2013, 10:31

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 15:46

jeeswg wrote:
24 Nov 2019, 13:00
StringToBinary is using fmt=12 by default:
CRYPT_STRING_HEXRAW := 0xC to return raw hex but with a trailing enter (not supported by Windows XP).
Anyhow, at least that explains the trailing enter.
StringToBinary isn't used for Hex output in this script, its only used for b64. The Hex is parsed directly from the compiler output.

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: MCode4GCC -- C/C++ to MCode Generator

24 Nov 2019, 18:20

Thanks guest3456. OK, a summary:

Notes on how the hex/base64 is generated:
- Hex from: MCode_Generate (which uses the gcc exe) creates var x.
- Base64 from: Hex2Base64 (which uses StringToBinary, which uses the Winapi function CryptStringToBinary) applied to var x.

'Bentschi Style' option:
- Base64, 'Copy to Clipboard': there are CRLFs every 64 characters.
- Base64, copy directly from Edit4: there are CRLFs every 64 characters *and* a trailing CRLF.

'Laszlo Style' option (note: not 'Gangnam Style'):
- Hex, 'Copy to Clipboard': one long string with no spaces or CRs/LFs.
- Hex, copy directly from Edit4: the same.

I saw CRLFs only, fortunately no lone LFs (without CRs), which Notepad doesn't handle well.

The CopytoClipboard subroutine has this line:
Clipboard := Trim(StrReplace(x, "`n", "`r`n"), "`r`n")

So, recommendations would be:
- RTrim CRLFs before putting the base64 into the Edit control.
- Either split up the hex, or remove the splits (CRLFs) from the base64. I'm neutral re. this.

Btw can some recommend a C++ example, long enough to produce a long hex/base64 string? Thanks.
I used this (note: replace bool with _Bool before compiling with TDM-GCC):
case-insensitive searching in binary buffers - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=58103&p=255645#p255645
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
joedf
Posts: 8940
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

Re: MCode4GCC -- C/C++ to MCode Generator

25 Nov 2019, 10:43

Okay, I just committed a patch. Could anyone confirm it's fixed?
https://github.com/joedf/MCode4GCC/blob/master/MCode4GCC.ahk
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500, NVIDIA GTX 1060 6GB, 2x16GB Kingston FURY Beast - DDR4 3200 MHz | [About Me] | [About the AHK Foundation] | [Courses on AutoHotkey]
[ASPDM - StdLib Distribution] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library]

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: gwarble and 128 guests