| 1 | package Win32::InheritHandles; | ||
| 2 | use strict; | ||
| 3 | #use Win32::API; | ||
| 4 | |||
| 5 | our $VERSION = '0.79'; | ||
| 6 | |||
| 7 | # This only works with Windows version 8+ (Vista onwards) | ||
| 8 | |||
| 9 | 1; | ||
| 10 | |||
| 11 | __END__ | ||
| 12 | |||
| 13 | // https://github.com/rprichard/win32-console-docs/blob/master/src/HandleTests/CreateProcess_InheritList.cc | ||
| 14 | // https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873 | ||
| 15 | |||
| 16 | BOOL CreateProcessWithExplicitHandles( | ||
| 17 | __in_opt LPCTSTR lpApplicationName, | ||
| 18 | __inout_opt LPTSTR lpCommandLine, | ||
| 19 | __in_opt LPSECURITY_ATTRIBUTES lpProcessAttributes, | ||
| 20 | __in_opt LPSECURITY_ATTRIBUTES lpThreadAttributes, | ||
| 21 | __in BOOL bInheritHandles, | ||
| 22 | __in DWORD dwCreationFlags, | ||
| 23 | __in_opt LPVOID lpEnvironment, | ||
| 24 | __in_opt LPCTSTR lpCurrentDirectory, | ||
| 25 | __in LPSTARTUPINFO lpStartupInfo, | ||
| 26 | __out LPPROCESS_INFORMATION lpProcessInformation, | ||
| 27 | // here is the new stuff | ||
| 28 | __in DWORD cHandlesToInherit, | ||
| 29 | __in_ecount(cHandlesToInherit) HANDLE *rgHandlesToInherit) | ||
| 30 | { | ||
| 31 | BOOL fSuccess; | ||
| 32 | BOOL fInitialized = FALSE; | ||
| 33 | SIZE_T size = 0; | ||
| 34 | LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL; | ||
| 35 | fSuccess = cHandlesToInherit < 0xFFFFFFFF / sizeof(HANDLE) && | ||
| 36 | lpStartupInfo->cb == sizeof(*lpStartupInfo); | ||
| 37 | if (!fSuccess) { | ||
| 38 | SetLastError(ERROR_INVALID_PARAMETER); | ||
| 39 | } | ||
| 40 | if (fSuccess) { | ||
| 41 | fSuccess = InitializeProcThreadAttributeList(NULL, 1, 0, &size) || | ||
| 42 | GetLastError() == ERROR_INSUFFICIENT_BUFFER; | ||
| 43 | } | ||
| 44 | if (fSuccess) { | ||
| 45 | lpAttributeList = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST> | ||
| 46 | (HeapAlloc(GetProcessHeap(), 0, size)); | ||
| 47 | fSuccess = lpAttributeList != NULL; | ||
| 48 | } | ||
| 49 | if (fSuccess) { | ||
| 50 | fSuccess = InitializeProcThreadAttributeList(lpAttributeList, | ||
| 51 | 1, 0, &size); | ||
| 52 | } | ||
| 53 | if (fSuccess) { | ||
| 54 | fInitialized = TRUE; | ||
| 55 | fSuccess = UpdateProcThreadAttribute(lpAttributeList, | ||
| 56 | 0, PROC_THREAD_ATTRIBUTE_HANDLE_LIST, | ||
| 57 | rgHandlesToInherit, | ||
| 58 | cHandlesToInherit * sizeof(HANDLE), NULL, NULL); | ||
| 59 | } | ||
| 60 | if (fSuccess) { | ||
| 61 | STARTUPINFOEX info; | ||
| 62 | ZeroMemory(&info, sizeof(info)); | ||
| 63 | info.StartupInfo = *lpStartupInfo; | ||
| 64 | info.StartupInfo.cb = sizeof(info); | ||
| 65 | info.lpAttributeList = lpAttributeList; | ||
| 66 | fSuccess = CreateProcess(lpApplicationName, | ||
| 67 | lpCommandLine, | ||
| 68 | lpProcessAttributes, | ||
| 69 | lpThreadAttributes, | ||
| 70 | bInheritHandles, | ||
| 71 | dwCreationFlags | EXTENDED_STARTUPINFO_PRESENT, | ||
| 72 | lpEnvironment, | ||
| 73 | lpCurrentDirectory, | ||
| 74 | &info.StartupInfo, | ||
| 75 | lpProcessInformation); | ||
| 76 | } | ||
| 77 | if (fInitialized) DeleteProcThreadAttributeList(lpAttributeList); | ||
| 78 | if (lpAttributeList) HeapFree(GetProcessHeap(), 0, lpAttributeList); | ||
| 79 | return fSuccess; | ||
| 80 | } |