Created
March 4, 2015 02:47
-
-
Save peace2048/2d1b1f0c7c4060df7b56 to your computer and use it in GitHub Desktop.
デスクトップを作成した上でアプリケーションを起動する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stdafx.h" | |
#include <Windows.h> | |
int _tmain(int argc, _TCHAR* argv[]) | |
{ | |
TCHAR commandLine[MAX_PATH] = { 0 }; | |
HDESK hOldDesk, hInputDesk, hNewDesk; | |
STARTUPINFO si; | |
PROCESS_INFORMATION pi; | |
BOOL success; | |
TCHAR* NEW_DESKTOP_NAME = _T("NewDesktop"); | |
if (argc <= 1) | |
{ | |
wprintf(_T("USAGE: %s command [args...]"), argv[0]); | |
return 0; | |
} | |
for (int i = 1; i < argc; i++) | |
{ | |
if (i > 1) | |
{ | |
lstrcat(commandLine, TEXT(" ")); | |
} | |
lstrcat(commandLine, TEXT("\"")); | |
lstrcat(commandLine, argv[i]); | |
lstrcat(commandLine, TEXT("\"")); | |
} | |
hOldDesk = GetThreadDesktop(GetCurrentThreadId()); | |
if (hOldDesk == NULL) | |
{ | |
puts("ERROR: GetThreadDesktop"); | |
return 1; | |
} | |
hInputDesk = OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP); | |
if (hInputDesk == NULL) | |
{ | |
puts("ERROR: OpenInputDesktop"); | |
return 1; | |
} | |
hNewDesk = CreateDesktop(NEW_DESKTOP_NAME, NULL, NULL, 0, GENERIC_ALL, NULL); | |
if (hNewDesk == NULL) | |
{ | |
puts("ERROR: CreateDesktop"); | |
CloseDesktop(hInputDesk); | |
return 1; | |
} | |
if (!SetThreadDesktop(hNewDesk)) | |
{ | |
puts("ERROR: SetThreadDesktop"); | |
CloseDesktop(hNewDesk); | |
CloseDesktop(hInputDesk); | |
return 1; | |
} | |
if (!SwitchDesktop(hNewDesk)) | |
{ | |
puts("ERROR: SwitchDesktop"); | |
SetThreadDesktop(hOldDesk); | |
CloseDesktop(hNewDesk); | |
CloseDesktop(hInputDesk); | |
return 1; | |
} | |
ZeroMemory(&si, sizeof(si)); | |
si.cb = sizeof(si); | |
si.lpDesktop = NEW_DESKTOP_NAME; | |
ZeroMemory(&pi, sizeof(pi)); | |
success = CreateProcess(NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi); | |
if (success) | |
{ | |
WaitForSingleObject(pi.hProcess, INFINITE); | |
} | |
SwitchDesktop(hInputDesk); | |
SetThreadDesktop(hOldDesk); | |
CloseDesktop(hNewDesk); | |
CloseDesktop(hInputDesk); | |
if (!success) | |
{ | |
puts("ERROR: CreateProcess"); | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment