Creating a child Process C# win32 API

2019-05-20 02:49发布

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

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!

1条回答
我欲成王,谁敢阻挡
2楼-- · 2019-05-20 03:46

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?

查看更多
登录 后发表回答