windows 可执行文件如何知道它是否从控制台打开
How windows executable knows whether it is opened from console
最近我为 Windows 安装了 git-lfs(来自 https://git-lfs.github.com/)。我不小心试图通过双击 运行 控制台外的可执行文件 'git-lfs.exe' ;出现以下消息:
这个提示勾起了我的好奇心。通常,运行ning 命令行或 Windows 资源管理器中的可执行文件没有区别。 windows 可执行文件如何知道它是否已从控制台执行? 假设我们要编写一个程序来检测程序是否 运行 来自控制台或不是。这可以独立于平台编写,还是需要使用依赖于平台的API(如How to check if the program is run from a console?中所述)?
让我们一探究竟!这是一个开源项目,所以代码都是为了探索(双关语)。
首先,克隆存储库以便于搜索:git clone https://github.com/git-lfs/git-lfs.git
。也可以通过 Github 搜索,但使用本地副本更容易。
然后,使用grep
之类的工具找到一段错误信息:
grep -r "command line tool" *
vendor/github.com/spf13/cobra/cobra.go:var MousetrapHelpText string = `This is a command line tool.
vendor/github.com/inconshreveable/mousetrap/README.md:Windows developers unfamiliar with command line tools will often "double-click"
看起来有一个包含消息的字符串。捕鼠器是什么东西,用在哪里?
grep -r "MousetrapHelpText" *
vendor/github.com/spf13/cobra/cobra.go:// MousetrapHelpText enables an information splash screen on Windows
vendor/github.com/spf13/cobra/cobra.go:var MousetrapHelpText string = `This is a command line tool.
vendor/github.com/spf13/cobra/command_win.go: if MousetrapHelpText != "" && mousetrap.StartedByExplorer() {
vendor/github.com/spf13/cobra/command_win.go: c.Print(MousetrapHelpText)
看起来很有趣。让我们去 https://github.com/inconshreveable/mousetrap
寻找来源。 command_win.go
文件包含魔法。
TL;DR: Windows 进程的启动信息包含父进程 ID。如果相同 Explorer.exe(Win GUI shell),假设 GUI 启动。
最近我为 Windows 安装了 git-lfs(来自 https://git-lfs.github.com/)。我不小心试图通过双击 运行 控制台外的可执行文件 'git-lfs.exe' ;出现以下消息:
这个提示勾起了我的好奇心。通常,运行ning 命令行或 Windows 资源管理器中的可执行文件没有区别。 windows 可执行文件如何知道它是否已从控制台执行? 假设我们要编写一个程序来检测程序是否 运行 来自控制台或不是。这可以独立于平台编写,还是需要使用依赖于平台的API(如How to check if the program is run from a console?中所述)?
让我们一探究竟!这是一个开源项目,所以代码都是为了探索(双关语)。
首先,克隆存储库以便于搜索:git clone https://github.com/git-lfs/git-lfs.git
。也可以通过 Github 搜索,但使用本地副本更容易。
然后,使用grep
之类的工具找到一段错误信息:
grep -r "command line tool" *
vendor/github.com/spf13/cobra/cobra.go:var MousetrapHelpText string = `This is a command line tool.
vendor/github.com/inconshreveable/mousetrap/README.md:Windows developers unfamiliar with command line tools will often "double-click"
看起来有一个包含消息的字符串。捕鼠器是什么东西,用在哪里?
grep -r "MousetrapHelpText" *
vendor/github.com/spf13/cobra/cobra.go:// MousetrapHelpText enables an information splash screen on Windows
vendor/github.com/spf13/cobra/cobra.go:var MousetrapHelpText string = `This is a command line tool.
vendor/github.com/spf13/cobra/command_win.go: if MousetrapHelpText != "" && mousetrap.StartedByExplorer() {
vendor/github.com/spf13/cobra/command_win.go: c.Print(MousetrapHelpText)
看起来很有趣。让我们去 https://github.com/inconshreveable/mousetrap
寻找来源。 command_win.go
文件包含魔法。
TL;DR: Windows 进程的启动信息包含父进程 ID。如果相同 Explorer.exe(Win GUI shell),假设 GUI 启动。