是否可以确定哪个进程启动我的 .Net 应用程序?

发布于 2024-07-05 03:19:40 字数 82 浏览 7 评论 0原文

我正在 .Net 中开发控制台应用程序,我想根据应用程序是从 cmd.exe 或 explorer.exe 启动的信息稍微改变一下行为。 是否可以?

I am developing console application in .Net and I want to change a behavior a little based on information that application was started from cmd.exe or from explorer.exe. Is it possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满地尘埃落定 2024-07-12 03:19:40

ToolHelp/ManagementObject 方法的一个问题是父进程可能已经退出。

GetStartupInfo Win32 函数(如果没有 .NET 等效函数,请使用 PInvoke)填充包含窗口标题的结构。 对于 Win32 控制台应用程序“app.exe”,从 cmd 启动时此标题字符串为“app”,从资源管理器(或 VS 调试器)启动时为“c:\full\path\to\app.exe”。

当然,这是一个 hack(可能会在其他版本等中发生变化)。

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int main()
{
  STARTUPINFO si;
  GetStartupInfo(&si);
  MessageBox(NULL, si.lpTitle, NULL, MB_OK);
  return 0;
}

One issue with the ToolHelp/ManagementObject approaches is that the parent process could already have exited.

The GetStartupInfo Win32 function (use PInvoke if there's no .NET equivalent) fills in a structure that includes the window title. For a Win32 console application "app.exe", this title string is "app" when started from cmd and "c:\full\path\to\app.exe" when started from explorer (or the VS debugger).

Of course this is a hack (subject to change in other versions, etc.).

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int main()
{
  STARTUPINFO si;
  GetStartupInfo(&si);
  MessageBox(NULL, si.lpTitle, NULL, MB_OK);
  return 0;
}
梦幻的味道 2024-07-12 03:19:40
Process this_process = Process.GetCurrentProcess();
int parent_pid = 0;
using (ManagementObject MgmtObj = new ManagementObject("win32_process.handle='" + this_process.Id.ToString() + "'"))
{
    MgmtObj.Get();
    parent_pid = Convert.ToInt32(MgmtObj["ParentProcessId"]);
}
string parent_process_name = Process.GetProcessById(parent_pid).ProcessName;
Process this_process = Process.GetCurrentProcess();
int parent_pid = 0;
using (ManagementObject MgmtObj = new ManagementObject("win32_process.handle='" + this_process.Id.ToString() + "'"))
{
    MgmtObj.Get();
    parent_pid = Convert.ToInt32(MgmtObj["ParentProcessId"]);
}
string parent_process_name = Process.GetProcessById(parent_pid).ProcessName;
爱已欠费 2024-07-12 03:19:40

CreateToolhelp32Snapshot 函数 有一个 Process32First 方法,该方法将允许您读取 PROCESSENTRY32 结构。 该结构有一个属性可以为您提供所需的信息:

th32ParentProcessID - 标识符
创建这个的过程
进程(其父进程)。

本文将帮助您开始使用 ToolHelpSnapshot 函数:

http://www.codeproject。 com/KB/cs/IsApplicationRunning.aspx

The CreateToolhelp32Snapshot Function has a Process32First method that will allow you to read a PROCESSENTRY32 Structure. The structure has a property that will get you the information you want:

th32ParentProcessID - The identifier
of the process that created this
process (its parent process).

This article will help you get started using the ToolHelpSnapshot function:

http://www.codeproject.com/KB/cs/IsApplicationRunning.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文