- 1 year ago
CreateProcess function creates the process in de the default dekstop instead of creating it inside the new desktop
Comment
1 / 1
When using the CreateProcess
function to create a new process in Windows, by default, the process is created in the same desktop as the calling process. If you want to create the process in a new desktop, you need to specify the desired desktop name in the STARTUPINFO
structure passed to CreateProcess
.
Here's an example of how you can create a process in a new desktop using CreateProcess
:
cpp#include <Windows.h>
int main()
{
// Desktop name for the new process
LPCTSTR desktopName = _T("NewDesktop");
// Initialize STARTUPINFO structure
STARTUPINFO startupInfo;
ZeroMemory(&startupInfo, sizeof(startupInfo));
startupInfo.cb = sizeof(startupInfo);
startupInfo.lpDesktop = (LPTSTR)desktopName;
// Process information
PROCESS_INFORMATION processInfo;
ZeroMemory(&processInfo, sizeof(processInfo));
// Create the process in the new desktop
BOOL result = CreateProcess(
nullptr,
_T("C:\\Path\\To\\YourProgram.exe"),
nullptr,
nullptr,
FALSE,
0,
nullptr,
nullptr,
&startupInfo,
&processInfo
);
if (result)
{
// Successfully created the process in the new desktop
// Wait for the process to exit or do other operations
// Close process and thread handles
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
else
{
// Failed to create the process
// Handle the error
}
return 0;
}
In this example, the lpDesktop
member of the STARTUPINFO
structure is set to the desired desktop name (NewDesktop
in this case) before calling CreateProcess
. The new process will be created in the specified desktop.
Make sure you have the necessary permissions to create and access desktops, and handle any errors that may occur during the process creation.