启动Win32进程A并启动进程B--获取B的ID/HWND
好的,我在这个网站上花了一些时间弄清楚如何使用 C# 的 Win32 调用来启动“子”进程(即新进程将窗口父进程设置为我)。只要不跨越 UAC 边界,它就可以工作。美好的。
现在我尝试使用一个卸载程序(进程 A)来执行此操作,该程序引导一个实际执行该工作的临时程序(进程 B)。进程 A 在创建 B 后消失。我的代码需要一个进程 ID,从中获取一个传递给 SetParent 的窗口句柄。看起来像这样:
Process p = new Process();
try
{
p.EnableRaisingEvents = true;
p.StartInfo.FileName = fileName;
p.StartInfo.Arguments = arguments;
if (p.Start())
{
p.WaitForInputIdle(10000);
IntPtr pHwnd = p.MainWindowHandle;
if (pHwnd == IntPtr.Zero)
{
return null;
}
IntPtr currentHwnd = Process.GetCurrentProcess().MainWindowHandle;
if (SetParent(pHwnd, currentHwnd) == 0)
{
if (Marshal.GetLastWin32Error() == 5) // access denied
{
// Need to launch privileged process that launches process
// and sets parent on UAC enabled OS.
}
else
{
return null;
}
}
// AND SO ON AND SO FORTH
只要 p 不消失,效果就很好。在这种情况下,p 在启动 p' 后变得繁荣。无论如何,p 从来没有窗口句柄。
那么我如何监视 p 以查看它是否启动 p' 并获取 p' 的 id(或更重要的是窗口句柄)?我可以从 id 获取 HWND,但我需要获取其中之一。
谢谢!
Okay, I spent a bit of time on this site figuring out how to start a "child" process (i.e., new process sets window parent to me) using Win32 calls from C#. It kinda works so long as it doesn't cross UAC boundaries. Fine.
Now I'm trying to do this with an uninstall program (process A) that bootstraps a temporary program (process B) which actually does the work. Process A goes away after creating B. My code requires a process ID from which to get an window handle which gets passed to SetParent. Looks something like this:
Process p = new Process();
try
{
p.EnableRaisingEvents = true;
p.StartInfo.FileName = fileName;
p.StartInfo.Arguments = arguments;
if (p.Start())
{
p.WaitForInputIdle(10000);
IntPtr pHwnd = p.MainWindowHandle;
if (pHwnd == IntPtr.Zero)
{
return null;
}
IntPtr currentHwnd = Process.GetCurrentProcess().MainWindowHandle;
if (SetParent(pHwnd, currentHwnd) == 0)
{
if (Marshal.GetLastWin32Error() == 5) // access denied
{
// Need to launch privileged process that launches process
// and sets parent on UAC enabled OS.
}
else
{
return null;
}
}
// AND SO ON AND SO FORTH
Works great so long as p doesn't go away. In this case p goes boom after starting p'. Regardless, p never has a window handle.
So how do I monitor p to see if it starts p' and get the id (or more importantly the window handle) for p'? I can get the HWND from the id, but I need to get one or the other.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个简单的解决方案可能是,在尝试获取其 MainWindowHandle 之前检查 p 是否为 null。以下是一些示例代码,您可以根据需要进行调整。
A simple solution might be, check to see if p is null before trying to get its MainWindowHandle. Here's some sample code which you can adapt if needed.