创建子进程 C# win32 API [英] Creating a child Process C# win32 API

查看:59
本文介绍了创建子进程 C# win32 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CreateProcess MSDN call() 手动启动应用程序,这是我的代码

I am using the CreateProcess MSDN call() to manually launch an application and here's my code

void LaunchProg()
{
    STARTUPINFO si = new STARTUPINFO();
    si.cb = Marshal.SizeOf(si);
    si.lpDesktop = "testProg";

    PROCESS_INFORMATION pi = new PROCESS_INFORMATION();

    var lpCurrentDirectory = Path.GetDirectoryName(executablePath);
    CreateProcess("c:\Windows\System32\notepad.exe", null, 
    IntPtr.Zero, IntPtr.Zero, true,
    NORMAL_PRIORITY_CLASS, IntPtr.Zero, lpCurrentDirectory, ref si, ref pi);
}

此代码运行良好,并在指定的桌面testProg"中启动程序,但问题是当 notepad.exe 创建具有新窗口的子进程时.此窗口显示在默认桌面中,而不是在testprog"桌面视图(活动桌面视图)中

This code works perfectly fine and launches the program in the specified desktop "testProg" but the issue is that when notepad.exe creates a child process which has a new window. This window is displayed in the Default desktop and not within the "testprog" desktop view (Active desktop view)

不确定哪个参数没有为在同一个活动桌面中生成的所有子窗口正确设置.我看了文档,不是很清楚.

Not sure as to which parameter is not set correctly for all the child windows to be spawned within the same active desktop. I looked at the documentation and it is not clearer to me.

观察更新:子进程不是从启动的应用程序继承的,而是在默认桌面上运行的系统进程的子进程.

Update on an observation: The child process is not inherited from the application launched but its a child process of a system process running in the default desktop.

有什么指点吗?提前致谢!

Any pointers? Thanks in advance!

推荐答案

来自 MSDN 文档 关于线程和桌面,如果在 STARTUPINFO 中指定了桌面,则使用,否则进程连接到的 windows 站的默认桌面将使用.在您的情况下,记事本似乎没有将桌面的句柄标记为可继承,或者它正在执行不继承句柄的 CreateProcess.也许您可以附加一个调试器并查看调用 CreateProcess 的参数是什么?

From the MSDN documentation about threads and desktops, if a desktop is specified in the STARTUPINFO, that is used, otherwise the default desktop for the windows station to which the process is connected will be used. In your case, it seems likely that notepad is not marking the handle to the desktop inheritable, or it is doing a CreateProcess that doesn't inherit handles. Maybe you could attach a debugger and see what parameters the CreateProcess is being called with?

这篇关于创建子进程 C# win32 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆