Function Main() As Long
Dim As Long iPid, hWin, hCon, hDlg, hBtn
Dim As Double ST
// 获取命令行
Dim sBasePath As String = Replace$(OS_GetCommand(2), """", "")
Dim sMakeType As String = Replace$(OS_GetCommand(3), """", "")
Dim sProcName As String = Replace$(OS_GetCommand(4), """", "")
Dim sSavePath As String = Trim(OS_GetCommand(5), Any """")
If sBasePath <> "" Then
// 软件路径自动补末尾
Dim sChr As String = Right$(sBasePath, 1)
If (sChr <> "\") And (sChr <> "/") Then
sBasePath &= "\"
End If
// 自动纠错 sMakeType 选项
Select Case sMakeType
Case "thinBasic.exe", "thinBasicc.exe"
Case Else
sMakeType = "thinBasic.exe"
End Select
// 生成路径和命令行
Dim sBuild As String = sBasePath & "bin\build\ThinBasic_Bundle_UI.exe"
Dim sRunner As String = sBasePath & "bin\" & sMakeType
Dim sScript As String = sBasePath & "temp\boot.tbasic"
Dim sExePath As String = sBasePath & "temp\" & sProcName & ".exe"
Dim sCommand As String = """" & sBuild & """ -f """ & sScript & """ -t """ & sRunner & """"
// 检查文件路径
If FILE_Exists(sBuild) = 0 Then
// 返回代码 [构建程序文件不存在]
MsgBox "编译失败,错误代码 11(编译器程序文件不存在)。", , "GeekBot"
Return 11
End If
If FILE_Exists(sRunner) = 0 Then
// 返回代码 [执行器程序文件不存在]
MsgBox "编译失败,错误代码 12(执行器程序文件不存在)。", , "GeekBot"
Return 12
End If
If FILE_Exists(sScript) = 0 Then
// 返回代码 [脚本文件不存在]
MsgBox "编译失败,错误代码 13(脚本文件不存在)。", , "GeekBot"
Return 13
End If
// 清理之前残留的编译器进程和文件
OS_ProcessKillByName("ThinBasic_Bundle_UI.exe")
FILE_Kill(sExePath)
// 运行编译器
RunApp(sCommand, True)
// 查找窗口
ST = Timer
Do
hWin = Window_Find(104, "FORM_THINBASIC_BUNDLE_UI_FRMMAIN_CLASS", "ThinBasic_Bundle_UI", &H10000000, 0, 0, 0)
If hWin <> 0 Then
iPid = Window_GetPID(hWin)
Window_Show(hWin, 0)
Exit Do
End If
If (Timer - ST) > 10 Then
// 返回代码 [查找窗口超时]
MsgBox "编译失败,错误代码 21(编译器未启动)。", , "GeekBot"
Return 21
End If
Delay 60
Loop
// 查找工具栏
ST = Timer
Do
hCon = Window_E2H(hWin, "ToolbarWindow32", 1)
If hCon <> 0 Then
Exit Do
End If
If (Timer - ST) > 6 Then
// 返回代码 [查找工具栏超时]
MsgBox "编译失败,错误代码 22(编译器加载不完全)。", , "GeekBot"
Return 22
End If
Delay 60
Loop
// 点击工具栏的编译按钮
Window_Post(hWin, 273, 5101, hCon)
// 查找消息框
ST = Timer
Do
hDlg = Window_Find(88, "#32770", "ThinBasic_Bundle_UI", &H10000000, 0, 0, 0)
If hDlg <> 0 Then
Exit Do
End If
If (Timer - ST) > 12 Then
// 返回代码 [查找消息框超时]
MsgBox "编译失败,错误代码 23(无法确认编译结果)。", , "GeekBot"
Return 23
End If
Delay 60
Loop
// 结束 编译器 进程
PID_Kill(iPid)
// 检查编译后的文件是否存在
If FILE_Exists(sExePath) = 0 Then
// 返回代码 [编译后找不到文件]
MsgBox "编译失败,错误代码 31(找不到编译后的可执行程序)。", , "GeekBot"
Return 31
End If
// 询问保存位置、保存文件
If sSavePath = "" Then
Dim sPtr As Long = Dlg_SaveFile(0, "GeekBot.exe", "可执行程序 (*.exe)|*.exe||")
sSavePath = Peek$(ASCIIZ, sPtr)
End If
If sSavePath = "" Then
// 返回代码 [用户取消了保存文件]
Function = 41
Else
File_Copy(sExePath, sSavePath)
If MsgBox("文件成功生成到 " & sSavePath & vbcrlf & vbcrlf & "是否打开目录查看生成的文件?", %MB_YESNO or %MB_ICONINFORMATION) = %IDYES Then
RunApp("explorer.exe /select,""" & sSavePath & """")
End If
End If
// 清理文件
File_Kill(sScript)
File_Kill(sBasePath & "temp\main.tbasic")
File_Kill(sExePath)
Else
// 返回代码 [命令行参数不正确]
Return 1
End If
Return 0
End Function
This code can't be run directly with ThinBasic because I modified some things, but the general principle should be clear, and a lot of it is in the ThinBasic manual, or available using some analysis tools.
Bookmarks