0

Recently I have installed git-lfs (from https://git-lfs.github.com/) for Windows. Accidentally I tried to run the executable 'git-lfs.exe' outside the console by double clicking on it; the following message appears:

enter image description here

This prompt awoke my curiosity. Usually, there is no distinction between running executable from command line or from Windows Explorer. How can windows executable know if it has been executed from console or not? Imagine we want to write a program which detects if a program was ran from console or not. Can this be written platform independent, or it requires the usage of platform dependent API (as stated in How to check if the program is run from a console?)?

Mark Lumar
  • 15
  • 2

1 Answers1

0

Let's find out! It's an open source project, so the code is all there for exploring (pun intended).

First off, clone the repository for easier searching: git clone https://github.com/git-lfs/git-lfs.git. One can search via Github too, but a local copy is easier to work with.

Then, use a tool such as grep to find a piece of the error message:

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"

Looks like there is a string that contains the message. What's the Mousetrap thing and where is it used?

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)

Looks interesting. Let's go to https://github.com/inconshreveable/mousetrap for the source. There's the command_win.go file that contains the magic.

TL;DR: A Windows process has startup info that contains parent process id. If that's the same Explorer.exe (Win GUI shell), assume GUI launching.

vonPryz
  • 22,996
  • 7
  • 54
  • 65