How do I start an application process in a new desktop (Desktop 2) with CPP and CreateProcess
Joseph RW
85
Reputation points
// Module_5_Processes.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <Windows.h>
#include <stdio.h>
#include <iostream>
#define NULL 0
int main()
{
//NOTES
/*
BOOL CreateProcessW(
[in, optional] LPCWSTR lpApplicationName, //const wchar_t AppName = "cmd.exe"; //-> the `C` in LPCWSTR measn `const`
[in, out, optional] LPWSTR lpCommandLine, //wchar_t
[in, optional] LPSECURITY_ATTRIBUTES lpProcessAttributes, //Null means default security attributes
[in, optional] LPSECURITY_ATTRIBUTES lpThreadAttributes, //Null means default security attributes
[in] BOOL bInheritHandles,
[in] DWORD dwCreationFlags,
[in, optional] LPVOID lpEnvironment,
[in, optional] LPCWSTR lpCurrentDirectory,//If the Exe you are running depends on DLLs that are in it's Current folder(C:\some-path\app.exe), specify the absolute path to the EXE - C:\some-path\
[in] LPSTARTUPINFOW lpStartupInfo,
[out] LPPROCESS_INFORMATION lpProcessInformation // No need to zero out this structure as it's only an OUT param, but do it anyway
);
typedef struct _STARTUPINFOW {
DWORD cb;
LPWSTR lpReserved;
LPWSTR lpDesktop;//si.lpDesktop = "Desktop 2" // to start the application in Desktop 2. or "Winstaion1\Desktop 2"
LPWSTR lpTitle;
DWORD dwX;
DWORD dwY;
DWORD dwXSize;
DWORD dwYSize;
DWORD dwXCountChars;
DWORD dwYCountChars;
DWORD dwFillAttribute;
DWORD dwFlags;
WORD wShowWindow;
WORD cbReserved2;
LPBYTE lpReserved2;
HANDLE hStdInput;
HANDLE hStdOutput;
HANDLE hStdError;
} STARTUPINFOW, *LPSTARTUPINFOW;
typedef struct _PROCESS_INFORMATION {
HANDLE hProcess;
HANDLE hThread;
DWORD dwProcessId;
DWORD dwThreadId;
} PROCESS_INFORMATION, *PPROCESS_INFORMATION, *LPPROCESS_INFORMATION;
*/
STARTUPINFO si = { sizeof(si) };
//si.cb = {sizeof(si)};
WCHAR Desktop[] = L"winsta0\\Desktop 2";
si.lpDesktop = Desktop;
//PROCESS_INFORMATION pi = { sizeof(pi) };//THis failed
//STARTUPINFOEXW siex;
//ZeroMemory(&siex, sizeof(siex));
//siex.StartupInfo.cb = sizeof(siex);
//siex.StartupInfo.lpDesktop = (LPWSTR)L"Desktop 2";
PROCESS_INFORMATION pi;
WCHAR appName[] = L"cmd.exe";
SECURITY_ATTRIBUTES sa;
sa.nLength = { sizeof(sa) };
//if (::CreateProcessW(NULL, appName, NULL, NULL, FALSE, CREATE_NEW_CONSOLE | EXTENDED_STARTUPINFO_PRESENT, NULL, NULL, (LPSTARTUPINFOW)&siex, &pi)) {
if (::CreateProcessW(NULL, appName, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
printf("Paint started with PID: %lu", pi.dwProcessId);
::WaitForSingleObject(pi.hProcess, INFINITE);
/*
BOOL GetExitCodeProcess(
[in] HANDLE hProcess,
[out] LPDWORD lpExitCode
);
*/
DWORD exitCode;
::GetExitCodeProcess(pi.hProcess, &exitCode);
printf("Exit Code: %lu", exitCode);//Obeserve the change in exit code when terminating the process via the GUI close button and when terminating with TAsk Maanger etc...
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
}
else {
printf("Process creation Failed: %u", ::GetLastError());
}
std::cout << "Hello World!\n";
return 0;
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Developer technologies | C++
Sign in to answer