Online MCode Generator(Beta, Tool)

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
Plorence
Posts: 9
Joined: 28 Feb 2020, 04:28
Contact:

Online MCode Generator(Beta, Tool)

29 Feb 2020, 00:08

mcode-generator.com [Domain mcode-generator.com has expired, so online services are no longer available.]
MCode Generator for AHK
2020-03-01, Created Github Repository! :arrow: Here
Features
-Using GCC
-Use Bentschi Style
-Create easy-to-see MCODE
-Select X86 or X64 to support compilation
-Code Copy Button Support
-Create example script
-Optimization Level Options Support

Online generator made by Benschi are not accessed.
Online is convenient, and simpler!

Lang: C#, ASP.net Core 2.1 MVC(Support docker)
Last edited by BoBo on 23 Feb 2022, 01:53, edited 3 times in total.
Reason: Added disclaimer about outdated 'mcode-generator.com' link.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Online MCode Generator(Beta, Tool)

29 Feb 2020, 00:47

great to have one of these again. benschi used to have one and it went offline. hopefully you will put yours on github open source so other people can continue if yours goes down

User avatar
Plorence
Posts: 9
Joined: 28 Feb 2020, 04:28
Contact:

Re: Online MCode Generator(Beta, Tool)

29 Feb 2020, 01:50

guest3456 wrote:
29 Feb 2020, 00:47
great to have one of these again. benschi used to have one and it went offline. hopefully you will put yours on github open source so other people can continue if yours goes down
I will try to upload to Github.
Thank you for your reply.
User avatar
boiler
Posts: 16965
Joined: 21 Dec 2014, 02:44

Re: Online MCode Generator(Beta, Tool)

01 Mar 2020, 20:52

Thanks for creating this. Having trouble making it work even with a very simple function. Fixed one of the issues where it shows the following in the AHK code:

Code: Select all

if (!regexmatch(mcode, " ^ ([0 - 9] +),(" c ":|.*?," c ":)([^,] +)", m))
Has a bunch of extra spaces and doesn't match anything. Matches as it should when changed to:

Code: Select all

if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
Is there something else needing correcting? Getting it to return an address, but when performing a DLL call to that address, not getting a return value.
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Online MCode Generator(Beta, Tool)

02 Mar 2020, 05:49

Since Bentchi released his ahk-code to convert c/c++ code to machinecode with his online converter I post it here for reference

Code: Select all

/*! Adapted by TheGood
http://www.autohotkey.com/forum/viewtopic.php?p=364922
Last updated: August 17th, 2010
Rewrite for AHK 1.1+ Bentschi 2013
*/

installdir := "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\"
;HKCU\HKEY_CURRENT_USER\Software\Microsoft\VCExpress\11.0_Config\Setup\VC
;ProductDir

if (!FileExist(vcvarsall := installdir "vcvarsall.bat"))
	ExitApp
opt := { opt: 1, lang: 1, comp: 1, enc: 1, warn: 1 }
pcount = %0%
if (!FileExist(file := AbsolutePath("tmp\test.c"))) ;AbsolutePath(%pcount%)))
	ExitApp
fileinfo := SplitPath(file)
Loop % pcount
{
	p := %A_Index%
	if (p = "-minsize")
		opt.opt := 1
	else if (p = "-maxspeed")
		opt.opt := 2
	else if (p = "-c")
		opt.lang := 1
	else if (p = "-cpp")
		opt.lang := 2
	else if (p = "-x86")
		opt.comp := 1
	else if (p = "-x64")
		opt.comp := 2
	else if (p = "-x86x64")
		opt.comp := 3
	else if (p = "-hex")
		opt.enc := 1
	else if (p = "-base64")
		opt.enc := 2
	else if (RegExMatch(p, "^-warn([1-4])$", m))
		opt.warn := m1
}

bat := "@echo off`r`n"
outfiles := {}
for k, v in {1: "x86", 2: "x86_amd64" }
{
	if (opt.comp & k)
	{
		outfiles[v] := fileinfo.dir fileinfo.filename "_" v ".cod"
		FileDelete, % fileinfo.dir fileinfo.filename "_" v ".cod"
		bat .= "call """ vcvarsall """ " v "`r`n"
		bat .= "cl " ((opt.lang=1) ? "/TC" : "/TP") " /c /FAc /Facode.cod /O" opt.opt " /W" opt.warn " """ file """`r`n"
		bat .= "echo ERRORLEVEL: %ERRORLEVEL%`r`n"
		bat .= "move """ fileinfo.dir "code.cod"" """ fileinfo.dir fileinfo.filename "_" v ".cod""`r`n"
	}
}
SetWorkingDir, % fileinfo.dir
batfile := fileinfo.dir fileinfo.filename ".bat"
logfile := fileinfo.dir fileinfo.filename ".log"
FileDelete, % batfile
FileAppend, % bat, % batfile
FileDelete, % logfile
RunWait, % batfile " >> " logfile,, Hide
FileDelete, % fileinfo.dir "code.cod"
err := 0
Loop, read, % logfile
{
	if (RegExMatch(A_LoopReadLine, "ERRORLEVEL: (.*)", m))
	err += m1
}
if (err)
	ExitApp
functions := {}
for k, v in outfiles
{
	FileRead, code, % v
	f := {}
	while (RegExMatch(code, "ms)_?([^\s]+?)\s+PROC\s*(.*?)\s*[^\s]+\s*ENDP(.*)", m))
	{
		f.name := m1
		f.code := RegExReplace(RegExReplace(m2, "m)^[^\t]*\t([^\t]+)[^\r\n]*", "$1"), "\s+")
		if (opt.enc != 1)
		{
			binlength := StringToBinary(4, f.code, bin)
			if (opt.enc = 2)
				f.code := BinaryToString(1, &bin, binlength)
		}
		code := m3
	}
	functions[f.name, k] := f.code
}
out := ""
width := 100
for name, info in functions
{
	c := "MCode(" name ", """
	d := opt.enc
	for comp, code in info
	{
		if (comp = "x86")
			d .= ",x86:"
		else if (comp = "x86_amd64")
			d .= ",x64:"
		else
			continue
		d .= code
	}
	if (StrLen(c) + StrLen(d) + 2 < width)
		out .= c d """)"
	else
	{
		out .= c "`r`n(LTrim Join`r`n"
		while (StrLen(d) > width)
		{
			out .= SubStr(d, 1, width) "`r`n"
			d := SubStr(d, width + 1)
		}
		if (d)
			out .= d "`r`n"
		out .= ")"")"
	}
}
outfile := fileinfo.dir fileinfo.filename ".mcode"
FileDelete, % outfile
FileAppend, % out, % outfile
ExitApp


SplitPath(filename)
{
	if (RegExMatch(filename, "^(.*\\)?([^.\\]*)\.?([^\\]*)$", m))
		return { filename: m2, dir: AbsolutePath(m1), ext: m3 }
}

AbsolutePath(filename)
{
	if (!DllCall("shlwapi\PathIsRelativeW", "wstr", filename))
		return filename
	s := DllCall("GetFullPathNameW", "wstr", filename, "uint", 0, "ptr", 0, "ptr", 0)
	VarSetCapacity(buf, s * 2, 0)
	DllCall("GetFullPathNameW", "wstr", filename, "uint", s, "ptr", &buf, "ptr", 0)
	return StrGet(&buf, s, "utf-16")
}

StringToBinary(type, str, byref buf)
{
	if (!DllCall("crypt32\CryptStringToBinary", "str", str, "uint", 0, "uint", type, "ptr", 0, "uint*", sout, "ptr", 0, "ptr", 0))
		return ""
	VarSetCapacity(buf, sout, 0)
	if (!DllCall("crypt32\CryptStringToBinary", "str", str, "uint", 0, "uint", type, "ptr", &buf, "uint*", sout, "ptr", 0, "ptr", 0))
		return ""
	return sout
}

BinaryToString(type, ptr, size)
{
	if (!DllCall("crypt32\CryptBinaryToStringW", "ptr", ptr, "uint", size, "uint", type, "ptr", 0, "uint*", sout))
		return
	VarSetCapacity(bout, sout << 1, 0)
	if (!DllCall("crypt32\CryptBinaryToStringW", "ptr", ptr, "uint", size, "uint", type, "ptr", &bout, "uint*", sout))
		return
	return RegExReplace(StrGet(&bout, sout << 1, "utf-16"), "\s+")
}
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
Plorence
Posts: 9
Joined: 28 Feb 2020, 04:28
Contact:

Re: Online MCode Generator(Beta, Tool)

03 Mar 2020, 06:50

@boiler
boiler wrote:
01 Mar 2020, 20:52
Thanks for creating this. Having trouble making it work even with a very simple function. Fixed one of the issues where it shows the following in the AHK code:

Code: Select all

if (!regexmatch(mcode, " ^ ([0 - 9] +),(" c ":|.*?," c ":)([^,] +)", m))
Has a bunch of extra spaces and doesn't match anything. Matches as it should when changed to:

Code: Select all

if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
Is there something else needing correcting? Getting it to return an address, but when performing a DLL call to that address, not getting a return value.
No, the code you modified is correct.
There was a problem creating the example script, and so did MCode.
This is a bug, and thank you for letting me know.
Try again.
(Tested in X86, X64.)
Please let me know Github id and I will register as a contributor.
Change log here
User avatar
Plorence
Posts: 9
Joined: 28 Feb 2020, 04:28
Contact:

Re: Online MCode Generator(Beta, Tool)

03 Mar 2020, 06:53

jNizM wrote:
02 Mar 2020, 05:49
Since Bentchi released his ahk-code to convert c/c++ code to machinecode with his online converter I post it here for reference

Code: Select all

/*! Adapted by TheGood
http://www.autohotkey.com/forum/viewtopic.php?p=364922
Last updated: August 17th, 2010
Rewrite for AHK 1.1+ Bentschi 2013
*/

installdir := "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\"
;HKCU\HKEY_CURRENT_USER\Software\Microsoft\VCExpress\11.0_Config\Setup\VC
;ProductDir

if (!FileExist(vcvarsall := installdir "vcvarsall.bat"))
	ExitApp
opt := { opt: 1, lang: 1, comp: 1, enc: 1, warn: 1 }
pcount = %0%
if (!FileExist(file := AbsolutePath("tmp\test.c"))) ;AbsolutePath(%pcount%)))
	ExitApp
fileinfo := SplitPath(file)
Loop % pcount
{
	p := %A_Index%
	if (p = "-minsize")
		opt.opt := 1
	else if (p = "-maxspeed")
		opt.opt := 2
	else if (p = "-c")
		opt.lang := 1
	else if (p = "-cpp")
		opt.lang := 2
	else if (p = "-x86")
		opt.comp := 1
	else if (p = "-x64")
		opt.comp := 2
	else if (p = "-x86x64")
		opt.comp := 3
	else if (p = "-hex")
		opt.enc := 1
	else if (p = "-base64")
		opt.enc := 2
	else if (RegExMatch(p, "^-warn([1-4])$", m))
		opt.warn := m1
}

bat := "@echo off`r`n"
outfiles := {}
for k, v in {1: "x86", 2: "x86_amd64" }
{
	if (opt.comp & k)
	{
		outfiles[v] := fileinfo.dir fileinfo.filename "_" v ".cod"
		FileDelete, % fileinfo.dir fileinfo.filename "_" v ".cod"
		bat .= "call """ vcvarsall """ " v "`r`n"
		bat .= "cl " ((opt.lang=1) ? "/TC" : "/TP") " /c /FAc /Facode.cod /O" opt.opt " /W" opt.warn " """ file """`r`n"
		bat .= "echo ERRORLEVEL: %ERRORLEVEL%`r`n"
		bat .= "move """ fileinfo.dir "code.cod"" """ fileinfo.dir fileinfo.filename "_" v ".cod""`r`n"
	}
}
SetWorkingDir, % fileinfo.dir
batfile := fileinfo.dir fileinfo.filename ".bat"
logfile := fileinfo.dir fileinfo.filename ".log"
FileDelete, % batfile
FileAppend, % bat, % batfile
FileDelete, % logfile
RunWait, % batfile " >> " logfile,, Hide
FileDelete, % fileinfo.dir "code.cod"
err := 0
Loop, read, % logfile
{
	if (RegExMatch(A_LoopReadLine, "ERRORLEVEL: (.*)", m))
	err += m1
}
if (err)
	ExitApp
functions := {}
for k, v in outfiles
{
	FileRead, code, % v
	f := {}
	while (RegExMatch(code, "ms)_?([^\s]+?)\s+PROC\s*(.*?)\s*[^\s]+\s*ENDP(.*)", m))
	{
		f.name := m1
		f.code := RegExReplace(RegExReplace(m2, "m)^[^\t]*\t([^\t]+)[^\r\n]*", "$1"), "\s+")
		if (opt.enc != 1)
		{
			binlength := StringToBinary(4, f.code, bin)
			if (opt.enc = 2)
				f.code := BinaryToString(1, &bin, binlength)
		}
		code := m3
	}
	functions[f.name, k] := f.code
}
out := ""
width := 100
for name, info in functions
{
	c := "MCode(" name ", """
	d := opt.enc
	for comp, code in info
	{
		if (comp = "x86")
			d .= ",x86:"
		else if (comp = "x86_amd64")
			d .= ",x64:"
		else
			continue
		d .= code
	}
	if (StrLen(c) + StrLen(d) + 2 < width)
		out .= c d """)"
	else
	{
		out .= c "`r`n(LTrim Join`r`n"
		while (StrLen(d) > width)
		{
			out .= SubStr(d, 1, width) "`r`n"
			d := SubStr(d, width + 1)
		}
		if (d)
			out .= d "`r`n"
		out .= ")"")"
	}
}
outfile := fileinfo.dir fileinfo.filename ".mcode"
FileDelete, % outfile
FileAppend, % out, % outfile
ExitApp


SplitPath(filename)
{
	if (RegExMatch(filename, "^(.*\\)?([^.\\]*)\.?([^\\]*)$", m))
		return { filename: m2, dir: AbsolutePath(m1), ext: m3 }
}

AbsolutePath(filename)
{
	if (!DllCall("shlwapi\PathIsRelativeW", "wstr", filename))
		return filename
	s := DllCall("GetFullPathNameW", "wstr", filename, "uint", 0, "ptr", 0, "ptr", 0)
	VarSetCapacity(buf, s * 2, 0)
	DllCall("GetFullPathNameW", "wstr", filename, "uint", s, "ptr", &buf, "ptr", 0)
	return StrGet(&buf, s, "utf-16")
}

StringToBinary(type, str, byref buf)
{
	if (!DllCall("crypt32\CryptStringToBinary", "str", str, "uint", 0, "uint", type, "ptr", 0, "uint*", sout, "ptr", 0, "ptr", 0))
		return ""
	VarSetCapacity(buf, sout, 0)
	if (!DllCall("crypt32\CryptStringToBinary", "str", str, "uint", 0, "uint", type, "ptr", &buf, "uint*", sout, "ptr", 0, "ptr", 0))
		return ""
	return sout
}

BinaryToString(type, ptr, size)
{
	if (!DllCall("crypt32\CryptBinaryToStringW", "ptr", ptr, "uint", size, "uint", type, "ptr", 0, "uint*", sout))
		return
	VarSetCapacity(bout, sout << 1, 0)
	if (!DllCall("crypt32\CryptBinaryToStringW", "ptr", ptr, "uint", size, "uint", type, "ptr", &bout, "uint*", sout))
		return
	return RegExReplace(StrGet(&bout, sout << 1, "utf-16"), "\s+")
}
Thank you for your comments!
User avatar
boiler
Posts: 16965
Joined: 21 Dec 2014, 02:44

Re: Online MCode Generator(Beta, Tool)

03 Mar 2020, 10:05

Plorence wrote: There was a problem creating the example script, and so did MCode.
This is a bug, and thank you for letting me know.
Try again.
(Tested in X86, X64.)
Please let me know Github id and I will register as a contributor.
Tried again, and it works. Thanks!

My Github id is boilercode, but there is no need to list me as a contributor. I appreciate you creating and sharing this tool. :clap:
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Online MCode Generator(Beta, Tool)

03 Mar 2020, 10:24

Plorence wrote:
03 Mar 2020, 06:50
Change log here
Change from gcc to mingw-w64(gcc is for linux)
i use tdm-gcc-32 and it works with joedf's script

i think its good to have an alternative that uses mingw so we can have two different compiler options

User avatar
Plorence
Posts: 9
Joined: 28 Feb 2020, 04:28
Contact:

Re: Online MCode Generator(Beta, Tool)

04 Mar 2020, 00:35

guest3456 wrote:
03 Mar 2020, 10:24
i think its good to have an alternative that uses mingw so we can have two different compiler options
Why do you think so?
The current server is running on Linux (Debian) and will be installed for Linux when gcc is installed.
So the machine code that compiles with gcc cannot be used in Windows.
Am I wrong?
I will try again at gcc (not mingw x64) and write a reply.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Online MCode Generator(Beta, Tool)

04 Mar 2020, 01:17

Plorence wrote:
04 Mar 2020, 00:35
guest3456 wrote:
03 Mar 2020, 10:24
i think its good to have an alternative that uses mingw so we can have two different compiler options
Why do you think so?
The current server is running on Linux (Debian) and will be installed for Linux when gcc is installed.
So the machine code that compiles with gcc cannot be used in Windows.
Am I wrong?
I will try again at gcc (not mingw x64) and write a reply.
sorry i wasn't clear. i was saying its good that your online generator uses mingw, because i use tdm-gcc locally on my machine with joedf's script, and so i can compare both mcodes and choose the smaller one

User avatar
Plorence
Posts: 9
Joined: 28 Feb 2020, 04:28
Contact:

Re: Online MCode Generator(Beta, Tool)

04 Mar 2020, 10:47

guest3456 wrote:
04 Mar 2020, 01:17
sorry i wasn't clear. i was saying its good that your online generator uses mingw, because i use tdm-gcc locally on my machine with joedf's script, and so i can compare both mcodes and choose the smaller one
I see. From what I tested, there is little difference between mingw and tdm-gcc.
Sooner or later, I will make it possible to choose a compilation option.
Thank you for your reply.
Ifeeldownnow
Posts: 15
Joined: 14 Mar 2020, 16:36

Re: Online MCode Generator(Beta, Tool)

18 Mar 2020, 08:09

Hi folk... Id apreciate if you help me
I can't put mcode-generator.com code to work

is it good still?

========================================================================================================
-------------------------------------------C func---------------------------------------------------------
========================================================================================================

Code: Select all

int funcsoma(int Width, int Height)
{
    return 3;
}
========================================================================================================
-------------------------------------------mcode generator x64 output for C func (funcsoma above)------------------------------
========================================================================================================

Code: Select all

MCode := "2,x64:VUiJ5YlNEIlVGLgDAAAAXcOQkJCQ"
_Function := BentschiMCode(MCode)
return
BentschiMCode(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)
}
========================================================================================================
----------------------------------------------- my script -----------------------------------------------
========================================================================================================

Code: Select all

msgbox % somasoma(1, 3)

return

BentschiMCode(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)
}


somasoma(a,b)
{
 MCode := "2,x64:VUiJ5YlNEIlVGLgDAAAAXcOQkJCQ"
;x86 MCode := "2,x86:VYnluAMAAABdww=="
_Function := BentschiMCode(MCode)
E := DllCall( &_Function, "int*", a, "int*", b, "cdecl int")
return E
}
[Mod edit: [code][/code] tags added]
feiyue
Posts: 349
Joined: 08 Aug 2014, 04:08

Re: Online MCode Generator(Beta, Tool)

18 Mar 2020, 10:58

Code: Select all

somasoma(a,b)
{
    static _Function:=""
    if (!_Function)
    {
        MCode := "2,x64:VUiJ5YlNEIlVGLgDAAAAXcOQkJCQ,x86:VYnluAMAAABdww=="
        _Function := BentschiMCode(MCode)
    }
    E := DllCall( _Function, "int", a, "int", b, "cdecl int")  ; Don't use &_Function
    return E
}
User avatar
Onimuru
Posts: 107
Joined: 08 Sep 2018, 18:35
Contact:

Re: Online MCode Generator(Beta, Tool)

25 Mar 2020, 16:07

Do you know of a way to include math.h?
User avatar
Plorence
Posts: 9
Joined: 28 Feb 2020, 04:28
Contact:

Re: Online MCode Generator(Beta, Tool)

31 Mar 2020, 05:53

Onimuru wrote:
25 Mar 2020, 16:07
Do you know of a way to include math.h?
I'm not sure either.
Please refer to this post.
See: https://www.autohotkey.com/boards/viewtopic.php?t=55647
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Online MCode Generator(Beta, Tool)

31 Mar 2020, 06:15

https://www.autohotkey.com/boards/viewtopic.php?f=7&t=32
I explained this in the basic MCode tutorial
Recommends AHK Studio
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: Online MCode Generator(Beta, Tool)

08 Apr 2020, 03:18

Is there Any OFFLINE solution? I am in middle of coding.. :(

Spoiler
Edit 08-Apr-2020: Works now
My Scripts and Functions: V1  V2
BNOLI
Posts: 548
Joined: 23 Mar 2020, 03:55

Re: Online MCode Generator(Beta, Tool)

08 Apr 2020, 03:33

SKAN wrote:
08 Apr 2020, 03:18
Is there Any OFFLINE solution? I am in middle of coding.. :(
I can't access it as well. Would this help: https://github.com/zxc010613/mcode-generator :?:
Remember to use [code]CODE[/code]-tags for your multi-line scripts. Stay safe, stay inside, and remember washing your hands for 20 sec !
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: Online MCode Generator(Beta, Tool)

08 Apr 2020, 09:23

SKAN wrote:
08 Apr 2020, 03:18
Is there Any OFFLINE solution?
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=4642


Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Bubo_Bubo, metallizer and 116 guests