MCode4GCC -- C/C++ to MCode Generator

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

16 Aug 2017, 08:10

This really cool. I didnt know godbolt had an api. This is a great resource then! :+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]
sancarn
Posts: 224
Joined: 01 Mar 2016, 14:52

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

17 Aug 2017, 12:32

joedf wrote:I didnt know godbolt had an api.
Granted... I didn't even know the API was official/documented (sparsely)... xD While at work I spend a lot of time figuring out how websites work to automate them... Spending a lot of time in the chrome network tab :D

Anyway, I'm glad people here will find a use for it (when it's eventually implemented ;))
User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

17 Aug 2017, 13:13

Yes, definitely! haha :+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]
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

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

23 May 2018, 19:59

tl;dr x32 MCode works but is overly long, x64 no MCode output

- I installed tdm64-gcc-5.1.0-2.exe to C:\TDM-GCC-64.
TDM-GCC : Download
http://tdm-gcc.tdragon.net/download
- I used 'TheAnswerToEverything.c' as the target file. The function returns 42.
- I used an x32 version of AutoHotkey.
- I based my settings on the image in the OP, i.e. I used the Overall option.

- If I specify 'gcc64' I get: [Empty]
- If I specify 'gcc' I get: 2,x86:uCoAAADDkJCQkJCQkJCQkA==

- I was hoping to get this: 2,x86:aipYww==,x64:uCoAAADD
- Or at least this: 2,x86:aipYww==
- The x32 (x86) machine code that I got was different to and longer than what I was hoping for, but it did work:

Code: Select all

q:: ;test machine code
;MyFunction := MCode("2,x86:aipYww==")
MyFunction := MCode("2,x86:aipYww==,x64:uCoAAADD")
MsgBox % DllCall(MyFunction,"cdecl") ;42
MyFunction := MCode("2,x86:uCoAAADDkJCQkJCQkJCQkA==")
MsgBox % DllCall(MyFunction,"cdecl") ;42
return

;==================================================

;GitHub - joedf/MCode4GCC: MCode4GCC is an MCode generator using the GCC Compiler.
;https://github.com/joedf/MCode4GCC

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)
}
- Any ideas on how I can get the same x32 machine code as in the example, and on how to get any x64 machine code? Thanks.

- [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.

Code: Select all

;I replaced this:
Clipboard:=x
;with this:
Clipboard:=Trim(x,"`r`n")
;or this:
Clipboard := Trim(StrReplace(x, "`n", "`r`n"), "`r`n")
- [EDIT:] Thanks so much for this script. It's been great writing C++ code and testing out the functions in AHK.
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
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

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

24 May 2018, 00:25

I think those are Bentschis examples.
He used Visual C++ to compile the code with some optimisation flags.
Recommends AHK Studio
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

24 May 2018, 05:19

I installed tdm64-gcc-5.1.0-2.exe to C:\TDM-GCC-64.
You can specify -m32 for 32 bit code and -m64 for 64 bit code for gcc tdm. It looks like joedf's script determines the x86/x64 prefix based on the bitness of the compiler executable, this will not be correct for tdm gcc if you use the mentioned flags.

Code: Select all

MyFunction := MCode("2,x86:uCoAAADDkJCQkJCQkJCQkA==,2,x64:uCoAAADDkJCQkJCQkJCQkA==")
It works for me on both 32 and 64, I got the same result with -m32 -O3 and -m64 -O3.

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

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

24 May 2018, 12:57

- Hmm, so I used gcc and it produced machine code that works with both x64 and x32. Cf. the examples which were about half as long, and were either x64 or x32 only.
- Do you mean to replace 'gcc' with 'gcc -m64'/'gcc -m32' in the GUI, or to edit the 'flags' variable in the script directly. (Since the machine code wasn't changing when I tried to add flags, I probably presumed that the flag wasn't working.)
- Btw is there some possibility that the machine code I'm producing is actually x64 and x32 machine code concatenated, or is there an issue of junk (unnecessary machine code) being removed? That is somehow two-way compatible.
- Btw also, are people getting the same machine code that I'm getting? How was joedf able to use 'gcc64' and get a different x64 machine code result in the image in the OP? Thanks.

Code: Select all

;joedf:
2,x86:aipYww==,x64:uCoAAADD

;me (same machine code twice):
2,x86:uCoAAADDkJCQkJCQkJCQkA==,x64:uCoAAADDkJCQkJCQkJCQkA==

;base64 as hex:
Tomasz Ostrowski - Base64 decoder
http://tomeko.net/online_tools/base64.php?lang=en
aipYww== ;hex: 6A2A58C3
uCoAAADD ;hex: B82A000000C3
uCoAAADDkJCQkJCQkJCQkA== ;hex: B82A000000C390909090909090909090

;source code (TheAnswerToEverything.c):
int TheAnswerToEverything() {
	return 42;
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

24 May 2018, 14:00

edit the flags set in the script, not in the gui. I think gcc-tdm does 64 by default so you were a bit unlucky that the code worked on 32 bit too.

Code: Select all

uCoAAADD ;hex: B82A000000C3
uCoAAADDkJCQkJCQkJCQkA== ;hex: B82A000000C390909090909090909090
The trailing 90... are nops (no operation (helgef memory :? )) which aren't really needed, all compilers will not add these, you can remove them.

There is no concatenation.

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

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

24 May 2018, 16:14

I've tried to create a stand-alone script for C++ to machine code:

C++: C++ to machine code via TDM-GCC - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 23&t=49554
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: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

28 May 2018, 14:20

Helgef wrote:
I installed tdm64-gcc-5.1.0-2.exe to C:\TDM-GCC-64.
You can specify -m32 for 32 bit code and -m64 for 64 bit code for gcc tdm. It looks like joedf's script determines the x86/x64 prefix based on the bitness of the compiler executable, this will not be correct for tdm gcc if you use the mentioned flags.

Code: Select all

MyFunction := MCode("2,x86:uCoAAADDkJCQkJCQkJCQkA==,2,x64:uCoAAADDkJCQkJCQkJCQkA==")
It works for me on both 32 and 64, I got the same result with -m32 -O3 and -m64 -O3.

Cheers.
What Helgef says here is exactly on point. How I had GCC set up is MinGW x86 and x64 installed and made copies of gcc.exe and renamed them gcc64.exe and gcc32.exe ... or something like that.
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]
quantum
Posts: 14
Joined: 04 Dec 2018, 05:37

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

07 Dec 2018, 00:48

Did anybody try compiling something that has external calls? Such as this.

Code: Select all

#include <windows.h>
void main()
{
MessageBox(0, "Hello", "Welcome Message", 1);
}
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

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

07 Dec 2018, 01:08

You can see nnniks mcode tutorial on how to use function pointers.
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

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

07 Dec 2018, 05:17

jeeswg wrote:
24 May 2018, 12:57
- Btw also, are people getting the same machine code that I'm getting?

;me (same machine code twice):

Code: Select all

2,x86:uCoAAADDkJCQkJCQkJCQkA==,x64:uCoAAADDkJCQkJCQkJCQkA==
The same result, uCoAAADDkJCQkJCQkJCQkA==, with the extra kJCQkJCQkA at the back.
joedf wrote:
28 May 2018, 14:20
What Helgef says here is exactly on point. How I had GCC set up is MinGW x86 and x64 installed and made copies of gcc.exe and renamed them gcc64.exe and gcc32.exe ... or something like that.
Where are you getting your MinGW files? People are referencing them, but not clarifying how they got x86 and x64

Do you mean that your x64 install is from here? https://sourceforge.net/projects/mingw-w64/

And your x86 install is from here? https://sourceforge.net/projects/mingw/

Or are you getting both MinGW x86 and x64 versions in a single download?

Note- for those that choose TDM-GCC, it's 2 different downloads for the x64 and x32 versions. http://tdm-gcc.tdragon.net/download
User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

07 Dec 2018, 12:33

@SOTE
Correct :+1:
I have used TDM, installation seems much more newbie friendly.
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]
feiyue
Posts: 381
Joined: 08 Aug 2014, 04:08

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

07 Dec 2018, 12:51

quantum wrote:Did anybody try compiling something that has external calls? Such as this.

Code: Select all

#include <windows.h>
void main()
{
MessageBox(0, "Hello", "Welcome Message", 1);
}
You can refer to this:
https://autohotkey.com/boards/viewtopic ... 413#p89413
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

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

07 Dec 2018, 16:11

joedf wrote:
07 Dec 2018, 12:33
@SOTE
Correct :+1:
I have used TDM, installation seems much more newbie friendly.
It seems the TDM-GCC is reporting back incorrect information with your script. With my previous downloads of MingGW 32-bit and 64-bit files, your script always reported back x86 or x64 correctly.

But if I download and install the 32-bit and 64-bit versions of TDM-GCC, your script doesn't show the x64 version information correctly. Both versions of TDM-GCC (to include the 64-bit one) will report back as x86. If there is a specific change that needs to be made in the script, perhaps it's best to show the code and location of what needs to be changed. Another way is maybe to recognize that it's a TDM-GCC installation and make the change automatically.
quantum
Posts: 14
Joined: 04 Dec 2018, 05:37

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

07 Dec 2018, 16:34

This is interesting but not sure if this is the same way AutoIt does it.
Why define global functions at all? It seems weird.

Here is an example that works with BinaryCall UDF.

Code: Select all

#include <windows.h>
#include <iphlpapi.h>
#pragma comment(lib, "iphlpapi.lib")

BOOL IsConnectedToServer(DWORD dwProcessId)
{
    DWORD dwSize = 0;
    if (GetExtendedTcpTable(0, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS, 0) != ERROR_INSUFFICIENT_BUFFER)
        return FALSE;

    HANDLE hHeap = GetProcessHeap();

    LPVOID lpTcpTable = HeapAlloc(hHeap, 0, dwSize);
    if (lpTcpTable == NULL)
        return FALSE;

    if (GetExtendedTcpTable(lpTcpTable, &dwSize, TRUE, AF_INET, TCP_TABLE_OWNER_PID_CONNECTIONS, 0) != NO_ERROR)
    {
        HeapFree(hHeap, 0, lpTcpTable);
        return FALSE;
    }

    DWORD i;
    MIB_TCPROW_OWNER_PID TcpTable;
    for (i = 0; i < ((PMIB_TCPTABLE_OWNER_PID)lpTcpTable)->dwNumEntries; i++)
    {
        TcpTable = ((PMIB_TCPTABLE_OWNER_PID)lpTcpTable)->table[i];
        if (TcpTable.dwOwningPid == dwProcessId && TcpTable.dwRemotePort == 0xEF21)
        {
            HeapFree(hHeap, 0, lpTcpTable);
            return TRUE;
        }
    }

    HeapFree(hHeap, 0, lpTcpTable);
    return FALSE;
}
BinaryCall v1.2.7z
(397.74 KiB) Downloaded 379 times
User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

08 Dec 2018, 12:57

SOTE wrote:
07 Dec 2018, 16:11
Another way is maybe to recognize that it's a TDM-GCC installation and make the change automatically.
Sure! :+1:
How would you recommend identifying TDM? and compiling x86 vs x64 commands?
(I dont have it setup on this PC)
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]
SOTE
Posts: 1426
Joined: 15 Jun 2015, 06:21

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

13 Dec 2018, 20:56

joedf wrote:
08 Dec 2018, 12:57
SOTE wrote:
07 Dec 2018, 16:11
Another way is maybe to recognize that it's a TDM-GCC installation and make the change automatically.
Sure! :+1:
How would you recommend identifying TDM? and compiling x86 vs x64 commands?
(I dont have it setup on this PC)
I think it's probably best to identify the directory or files in TDM that are associated with the 64-bit version. Such as "x86_64-w64-mingw32", "gdb64", or search for strings with 64 in them. Then set to "-m64". That, or specifically have an option on the GUI for TDM or any gcc.exe (future proofing), as a manual setting, that will attempt to give a 64-bit result with the "-m64" setting. So, you could have an "auto" setting where you attempt to identify if gcc.exe is 32-bit or 64-bit, or a manual setting where the person identifies that version of gcc.exe as being such.
User avatar
joedf
Posts: 9097
Joined: 29 Sep 2013, 17:08
Location: Canada
Contact:

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

13 Dec 2018, 22:02

well here's what it does currently

Code: Select all

Get_CompilerType(cp) {
	if !FileExist(cp)
		cp:=get_where_Path(cp)
	if Is64BitAssembly(cp)
		return "64"
	else
		return "32"
}
https://github.com/joedf/MCode4GCC/blob ... #L334-L341

But it isnt the most reliable because it seems that some compilers with 32bit exe's to make 64bit exe.... weird :b
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: Google [Bot] and 127 guests