简体   繁体   English

创建一个子进程C#win32 API

[英]Creating a child Process C# win32 API

I am using the CreateProcess MSDN call() to manually launch an application and here's my code 我正在使用CreateProcess MSDN call()手动启动应用程序,这是我的代码

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);
}

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. 该代码可以正常工作,并在指定的桌面“ testProg”中启动该程序,但是问题是,当notepad.exe创建具有新窗口的子进程时。 This window is displayed in the Default desktop and not within the "testprog" desktop view (Active desktop view) 此窗口显示在“默认”桌面中,而不显示在“ testprog”桌面视图(活动桌面视图)中

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! 提前致谢!

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. 从有关线程和桌面的MSDN文档中 ,如果在STARTUPINFO中指定了桌面,则使用该桌面,否则将使用该进程连接到的Windows工作站的默认桌面。 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. 在您的情况下,记事本似乎没有标记可继承桌面的句柄,或者正在执行不继承句柄的CreateProcess。 Maybe you could attach a debugger and see what parameters the CreateProcess is being called with? 也许您可以附加一个调试器,然后查看用哪个参数调用CreateProcess?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM