From 535d23f22aaff855d6087a468d9ac7a8337b95d4 Mon Sep 17 00:00:00 2001 From: Mike McLaughlin Date: Fri, 4 Mar 2016 17:19:20 -0800 Subject: [PATCH] Added CreateProcessForLaunch, ResumeProcess and CloseResumeHandle that can be better supported cross-plat. Commit migrated from https://github.com/dotnet/coreclr/commit/86103a426ace432520c28b0f7ac19bcb88ce8343 --- src/coreclr/src/dlls/dbgshim/dbgshim.cpp | 98 ++++++++++++++++++++++++++++++ src/coreclr/src/dlls/dbgshim/dbgshim.h | 18 +++++- src/coreclr/src/dlls/dbgshim/dbgshim.ntdef | 3 + 3 files changed, 118 insertions(+), 1 deletion(-) diff --git a/src/coreclr/src/dlls/dbgshim/dbgshim.cpp b/src/coreclr/src/dlls/dbgshim/dbgshim.cpp index 7f683f3..1299bbd 100644 --- a/src/coreclr/src/dlls/dbgshim/dbgshim.cpp +++ b/src/coreclr/src/dlls/dbgshim/dbgshim.cpp @@ -62,6 +62,104 @@ Notes: } \ CONTRACTL_END; \ + +//----------------------------------------------------------------------------- +// Public API. +// +// CreateProcessForLaunch - a stripped down version of the Windows CreateProcess +// that can be supported cross-platform. +// +//----------------------------------------------------------------------------- +HRESULT +CreateProcessForLaunch( + __in LPWSTR lpCommandLine, + __in BOOL bSuspendProcess, + __in LPVOID lpEnvironment, + __in LPCWSTR lpCurrentDirectory, + __out PDWORD pProcessId, + __out HANDLE *pResumeHandle) +{ + PUBLIC_CONTRACT; + + PROCESS_INFORMATION processInfo; + STARTUPINFOW startupInfo; + DWORD dwCreationFlags = 0; + + ZeroMemory(&processInfo, sizeof(processInfo)); + ZeroMemory(&startupInfo, sizeof(startupInfo)); + + startupInfo.cb = sizeof(startupInfo); + + if (bSuspendProcess) + { + dwCreationFlags = CREATE_SUSPENDED; + } + + BOOL result = CreateProcessW( + NULL, + lpCommandLine, + NULL, + NULL, + FALSE, + dwCreationFlags, + lpEnvironment, + lpCurrentDirectory, + &startupInfo, + &processInfo); + + if (!result) { + *pProcessId = 0; + *pResumeHandle = NULL; + return HRESULT_FROM_WIN32(GetLastError()); + } + + if (processInfo.hProcess != NULL) + { + CloseHandle(processInfo.hProcess); + } + + *pProcessId = processInfo.dwProcessId; + *pResumeHandle = processInfo.hThread; + + return S_OK; +} + +//----------------------------------------------------------------------------- +// Public API. +// +// ResumeProcess - to be used with the CreateProcessForLaunch resume handle +// +//----------------------------------------------------------------------------- +HRESULT +ResumeProcess( + __in HANDLE hResumeHandle) +{ + PUBLIC_CONTRACT; + if (ResumeThread(hResumeHandle) == (DWORD)-1) + { + return HRESULT_FROM_WIN32(GetLastError()); + } + return S_OK; +} + +//----------------------------------------------------------------------------- +// Public API. +// +// CloseResumeHandle - to be used with the CreateProcessForLaunch resume handle +// +//----------------------------------------------------------------------------- +HRESULT +CloseResumeHandle( + __in HANDLE hResumeHandle) +{ + PUBLIC_CONTRACT; + if (!CloseHandle(hResumeHandle)) + { + return HRESULT_FROM_WIN32(GetLastError()); + } + return S_OK; +} + #ifdef FEATURE_PAL static diff --git a/src/coreclr/src/dlls/dbgshim/dbgshim.h b/src/coreclr/src/dlls/dbgshim/dbgshim.h index 979a7ee..777c706 100644 --- a/src/coreclr/src/dlls/dbgshim/dbgshim.h +++ b/src/coreclr/src/dlls/dbgshim/dbgshim.h @@ -11,6 +11,23 @@ typedef VOID (*PSTARTUP_CALLBACK)(IUnknown *pCordb, PVOID parameter, HRESULT hr); EXTERN_C HRESULT +CreateProcessForLaunch( + __in LPWSTR lpCommandLine, + __in BOOL bSuspendProcess, + __in LPVOID lpEnvironment, + __in LPCWSTR lpCurrentDirectory, + __out PDWORD pProcessId, + __out HANDLE *pResumeHandle); + +EXTERN_C HRESULT +ResumeProcess( + __in HANDLE hResumeHandle); + +EXTERN_C HRESULT +CloseResumeHandle( + __in HANDLE hResumeHandle); + +EXTERN_C HRESULT RegisterForRuntimeStartup( __in DWORD dwProcessId, __in PSTARTUP_CALLBACK pfnCallback, @@ -56,4 +73,3 @@ EXTERN_C HRESULT CreateDebuggingInterfaceFromVersion( __in LPCWSTR szDebuggeeVersion, __out IUnknown ** ppCordb); - diff --git a/src/coreclr/src/dlls/dbgshim/dbgshim.ntdef b/src/coreclr/src/dlls/dbgshim/dbgshim.ntdef index 4b726a9..6915bee 100644 --- a/src/coreclr/src/dlls/dbgshim/dbgshim.ntdef +++ b/src/coreclr/src/dlls/dbgshim/dbgshim.ntdef @@ -3,6 +3,9 @@ ; See the LICENSE file in the project root for more information. EXPORTS + CreateProcessForLaunch + ResumeProcess + CloseResumeHandle RegisterForRuntimeStartup UnregisterForRuntimeStartup GetStartupNotificationEvent -- 2.7.4