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