Run C programs (TinyCC)

Get help with using AutoHotkey and its commands and hotkeys
User avatar
F4Jonatas
Posts: 33
Joined: 22 Oct 2015, 06:35

Run C programs (TinyCC)

07 Feb 2017, 10:50

This code run in ANSI.
Is there a way to run in Unicode?

Code: Select all

MsgBox % TCC( "C:\Users\..\Desktop\tcc", ""
	. "main() {"
		. "char*_result;"
		. "strcpy( _result, ""Hello World"" );"
		. "return 0;"
	. "}" )

TCC( LibPath, Script, ResultLen = 999 ) {
	Local Context, Count
	Local Result := ""
	Local Dll := LibPath "\libtcc.dll"
	Local Module := DllCall( "Kernel32.dll\LoadLibrary", "Str", Dll )

	If Not Module
		Return

	Context := DllCall( Dll "\tcc_new" )
	DllCall( Dll "\tcc_add_sysinclude_path", "UInt" , Context, "Str", LibPath "\include", "Cdecl UInt" )
	DllCall( Dll "\tcc_add_library_path", "UInt", Context, "Str", LibPath "\lib", "Cdecl UInt" )

	VarSetCapacity( Result, ResultLen )
	Script := RegExReplace( Script, "(char\s*\*\s*_result)([^;]*)", "$1 = " &Result, Count )

	If ( Count = 0 )
		MsgBox Found NO char* _result...
	Else {
		DllCall( Dll "\tcc_compile_string", "UInt",Context, "Str", Script, "Cdecl UInt" )
		DllCall( Dll "\tcc_run", "UInt", Context, "Cdecl UInt" )
	}

	DllCall( Dll "\tcc_delete", "UInt", Context, "Cdecl UInt" )
	DllCall( "Kernel32.dll\FreeLibrary", "UInt", Module )
	VarSetCapacity( Result, -1 )

	Return Result
}
Original Source
TCC
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Run C programs (TinyCC)

07 Feb 2017, 12:37

Code: Select all

MsgBox % TCC(A_ScriptDir, ""
	. "main() {"
		. "char*_result;"
		. "strcpy( _result, ""Hello World"" );"
		. "return 0;"
	. "}" )

TCC( LibPath, Script, ResultLen = 999 ) {
	Local Context, Count
	Local Result := ""
	Local Dll := LibPath "\libtcc.dll"
	Local Module := DllCall( "Kernel32.dll\LoadLibrary", "Str", Dll )
	Local CompileSucceeded := False

	If Not Module
		Return

	Context := DllCall( Dll "\tcc_new" )
	DllCall( Dll "\tcc_add_sysinclude_path", "UInt" , Context, "AStr", LibPath "\include", "Cdecl UInt" )
	DllCall( Dll "\tcc_add_library_path", "UInt", Context, "AStr", LibPath "\lib", "Cdecl UInt" )

	VarSetCapacity( Result, ResultLen )
	Script := RegExReplace( Script, "(char\s*\*\s*_result)([^;]*)", "$1 = " &Result, Count )

	If ( Count = 0 )
		MsgBox Found NO char* _result...
	Else {
		CompileSucceeded := DllCall( Dll "\tcc_compile_string", "UInt",Context, "AStr", Script, "Cdecl UInt" ) == 0
		DllCall( Dll "\tcc_run", "UInt", Context, "Cdecl UInt" ) ; returns exit code
	}

	DllCall( Dll "\tcc_delete", "UInt", Context, "Cdecl UInt" )
	DllCall( "Kernel32.dll\FreeLibrary", "UInt", Module )
	VarSetCapacity( Result, -1 )

	if (CompileSucceeded) {
		if (A_IsUnicode)
			Result := StrGet(&Result,, "CP0")
	}

	Return Result
}

/*
// In short: Str -> AStr and a call to StrGet to convert an ANSI string to Unicode
--- "New AutoHotkey Script (2).ahk"	2017-02-07 17:33:50 +0000
+++ "New AutoHotkey Script.ahk"	2017-02-07 17:33:36 +0000
@@ -1,4 +1,4 @@
-MsgBox % TCC( "C:\Users\..\Desktop\tcc", ""
+MsgBox % TCC(A_ScriptDir, ""
 	. "main() {"
 		. "char*_result;"
 		. "strcpy( _result, ""Hello World"" );"
@@ -10,13 +10,14 @@ TCC( LibPath, Script, ResultLen = 999 )
 	Local Result := ""
 	Local Dll := LibPath "\libtcc.dll"
 	Local Module := DllCall( "Kernel32.dll\LoadLibrary", "Str", Dll )
+	Local CompileSucceeded := False
 
 	If Not Module
 		Return
 
 	Context := DllCall( Dll "\tcc_new" )
-	DllCall( Dll "\tcc_add_sysinclude_path", "UInt" , Context, "Str", LibPath "\include", "Cdecl UInt" )
-	DllCall( Dll "\tcc_add_library_path", "UInt", Context, "Str", LibPath "\lib", "Cdecl UInt" )
+	DllCall( Dll "\tcc_add_sysinclude_path", "UInt" , Context, "AStr", LibPath "\include", "Cdecl UInt" )
+	DllCall( Dll "\tcc_add_library_path", "UInt", Context, "AStr", LibPath "\lib", "Cdecl UInt" )
 
 	VarSetCapacity( Result, ResultLen )
 	Script := RegExReplace( Script, "(char\s*\*\s*_result)([^;]*)", "$1 = " &Result, Count )
@@ -24,13 +25,18 @@ TCC( LibPath, Script, ResultLen = 999 )
 	If ( Count = 0 )
 		MsgBox Found NO char* _result...
 	Else {
-		DllCall( Dll "\tcc_compile_string", "UInt",Context, "Str", Script, "Cdecl UInt" )
-		DllCall( Dll "\tcc_run", "UInt", Context, "Cdecl UInt" )
+		CompileSucceeded := DllCall( Dll "\tcc_compile_string", "UInt",Context, "AStr", Script, "Cdecl UInt" ) == 0
+		DllCall( Dll "\tcc_run", "UInt", Context, "Cdecl UInt" ) ; returns exit code
 	}
 
 	DllCall( Dll "\tcc_delete", "UInt", Context, "Cdecl UInt" )
 	DllCall( "Kernel32.dll\FreeLibrary", "UInt", Module )
 	VarSetCapacity( Result, -1 )
 
+	if (CompileSucceeded) {
+		if (A_IsUnicode)
+			Result := StrGet(&Result,, "CP0")
+	}
+
 	Return Result
 }
*/
EDIT: While the AStr changes are correct, another approach you could take would be to use wchar_t and wcscpy (or _snwprintf...) instead so the StrGet at the end isn't needed
User avatar
F4Jonatas
Posts: 33
Joined: 22 Oct 2015, 06:35

Re: Run C programs (TinyCC)

08 Feb 2017, 05:44

Nicely done!
Now the only error is that returns this:
<string>:1: warning: assignment makes pointer from integer without a cast
Is there any way to fix it?

Thank you!
qwerty12
Posts: 468
Joined: 04 Mar 2016, 04:33
GitHub: qwerty12

Re: Run C programs (TinyCC)

08 Feb 2017, 11:04

I'm honestly curious, how is this warning displayed to you? I mean, I see it if I compile the code manually from the command-line with tcc.exe directly; however, when I try TCC from AutoHotkey as per your script, all I see is "Hello World"... Anyway, replacing the similar line 23 with Script := RegExReplace( Script, "(char\s*\*\s*_result)([^;]*)", "$1 = (char*)" &Result, Count ) should remove the warning (though now I'm curious as to whether you'll just simply get a blank result...)

EDIT: In reply to your reply below, I see. Thanks for replying!
Last edited by qwerty12 on 08 Feb 2017, 12:46, edited 1 time in total.
User avatar
F4Jonatas
Posts: 33
Joined: 22 Oct 2015, 06:35

Re: Run C programs (TinyCC)

08 Feb 2017, 12:08

I can see warning by running the script with the Sublime Text.

Solved!
Now it's running without warning...

Thanks man!
:dance:

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Chunjee, jgcampbell300, millansoft and 250 guests