Created
May 31, 2015 16:08
-
-
Save tmplinshi/bea11a459e1e4ef25dad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;----------------------------------------------------------------------- | |
;@Ahk2Exe-SetDescription 把 exe 转换为命令行或窗口模式 | |
;@Ahk2Exe-ConsoleApp | |
;@Ahk2Exe-SetVersion 1.0.0.0 | |
;----------------------------------------------------------------------- | |
; AutoHotkey 版本: 1.1.22.02 U32 | |
; 系统版本 : Win7 x64 | |
; 作者 : tmplinshi | |
; 发布日期 : 2015-5-31 | |
; 编译工具 : 新版 ahk2exe (未包含在 AutoHotkey 安装包中) | |
; 论坛地址: http://ahkscript.org/boards/viewtopic.php?f=24&t=521 | |
; 下载地址: http://pan.baidu.com/s/1mgHx2R2 | |
;----------------------------------------------------------------------- | |
#NoEnv | |
#NoTrayIcon | |
file = %1% | |
mode = %2% | |
If (file = "") || (file = "/?") || !FileExist(file) | |
PrintHelp() | |
Else | |
switch_subsystem( file, StrReplace(mode, "/") ) | |
; ========================================================================================================= | |
PrintHelp() { | |
text = | |
(LTrim | |
把 exe 转换为命令行或窗口模式 | |
用法: %A_ScriptName% "exe文件路径" [/c | /g] | |
参数: | |
%A_Space% /c 转换为命令行模式 | |
%A_Space% /g 转换为窗口模式 | |
(如果没有指定 /c 或者 /g,则自动切换命令行/窗口模式) | |
) | |
FileAppend, %text%, * | |
} | |
/* | |
Modified from http://www.autohotkey.com/board/topic/21189-compile-ahk-ii-for-those-who-compile/?p=316030 | |
Args: | |
filename - bin/exe file to switch | |
subsystem - "C" (console) or "G" (gui). if omitted, switches between the two. | |
*/ | |
switch_subsystem(filename, subsystem := "") { | |
static IMAGE_DOS_SIGNATURE := 0x5A4D | |
, IMAGE_NT_SIGNATURE := 0x4550 | |
, IMAGE_SIZEOF_FILE_HEADER := 20 | |
, IMAGE_SUBSYSTEM_WINDOWS_GUI := 2 | |
, IMAGE_SUBSYSTEM_WINDOWS_CUI := 3 | |
; Open file for read/write. | |
If !f := FileOpen(filename, "rw") | |
Return "CreateFile failed" | |
; Verify EXE signature. | |
e_magic := f.ReadUShort() | |
If (e_magic != IMAGE_DOS_SIGNATURE) | |
Return "Bad exe file: no DOS sig", f.Close() | |
; Get offset of IMAGE_NT_HEADERS. | |
f.Pos := 60 | |
e_lfanew := f.ReadInt() | |
; Verify NT signature. | |
f.Pos := e_lfanew | |
ntSignature := f.ReadUInt() | |
If (ntSignature != IMAGE_NT_SIGNATURE) | |
Return "Bad exe file: no NT sig", f.Close() | |
; Calculate offset of IMAGE_OPTIONAL_HEADER and its Subsystem field. | |
offset_optional_header := e_lfanew + 4 + IMAGE_SIZEOF_FILE_HEADER | |
offset_Subsystem := offset_optional_header + 68 | |
; Read current subsystem. | |
f.Pos := offset_Subsystem | |
Cur_Subsystem := f.ReadUShort() | |
; Write new subsystem. | |
Subsystem := (Subsystem = "C") ? IMAGE_SUBSYSTEM_WINDOWS_CUI | |
: (Subsystem = "G") ? IMAGE_SUBSYSTEM_WINDOWS_GUI | |
: (Cur_Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI) ? IMAGE_SUBSYSTEM_WINDOWS_GUI | |
: IMAGE_SUBSYSTEM_WINDOWS_CUI | |
f.Pos := offset_Subsystem | |
f.WriteUShort(Subsystem) | |
f.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment