Can one detect how .exe was launched?

百般思念 提交于 2019-12-10 10:04:14

问题


I want to be able to detect whether a given exe was shellex'd programmatically or if it was entered and executed interactively in, say, CMD.EXE.

Is there anything about the way an exe is launched that indicates the mechanism that was used to launch it?

Context: Windows XP, Visual Studio 6 languages.


回答1:


There might be an easier way, but the only way I can think of is to check the parent process name, which involves a few steps:

  1. Get the ID of the parent process.
  2. Get the handle of the process, using the ID.
  3. Use GetModuleFileNameEx with the handle found (and NULL as the module) to get the executable's name.
  4. Check if the executable's name is cmd.exe or whatever.

Bear in mind that the parent process might already be gone when (or while) you do this check.

Edit:

If your program is a console application, you can also check the console it's running in. If it was run from cmd, it will usually use the same console. So, you can use GetConsoleTitle, for instance, and see if it's "Command Prompt". This might not work on localized or different versions of Windows, but it's easy if you have limitated cases. You can also use GetConsoleWindow and GetWindowThreadProcessId instead of steps 1 and 2.




回答2:


You can differ between say CMD and Explorer by inspecting the parent process, but you can't tell if it happened due to user action or not. Also AFAIK all ways to launch a process result in the same NtCreateProcess/PspCreateProcess call, so you can't tell which API was used either.



来源:https://stackoverflow.com/questions/7252977/can-one-detect-how-exe-was-launched

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!