📄 stafprocess.cpp
字号:
STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString("Error creating ") + "stdout file: " + startData->stdoutRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } } else { if (startData->stdoutMode == kSTAFProcessIOAppendFile) { DWORD outFP = SetFilePointer( newOutHandle, GetFileSize(newOutHandle, NULL), NULL, FILE_BEGIN); if (outFP == (unsigned)-1) { if (ret == kSTAFOk) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString( "Error appending to stdout file: ") + startData->stdoutRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } } } else if (!SetEndOfFile(newOutHandle)) { if (osRC) *osRC = GetLastError(); ret = kSTAFBaseOSError; } } } else newOutHandle = GetStdHandle(STD_OUTPUT_HANDLE); HANDLE currentProcess = GetCurrentProcess(); if (startData->stderrMode == kSTAFProcessIOStdout) { if (!DuplicateHandle(currentProcess, newOutHandle, currentProcess, &newErrHandle, 0, TRUE, DUPLICATE_SAME_ACCESS)) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString("Error redirecting ") + "stderr to stdout.\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } } else if (startData->stderrMode != kSTAFProcessIONoRedirect) { startInfo.dwFlags |= STARTF_USESTDHANDLES; newErrHandle = CreateFile( STAFString(startData->stderrRedirect). toCurrentCodePage()->buffer(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0); if (newErrHandle == INVALID_HANDLE_VALUE) { if (ret == kSTAFOk) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString("Error creating ") + "stderr file: " + startData->stderrRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } } else { if (startData->stderrMode == kSTAFProcessIOAppendFile) { DWORD errFP = SetFilePointer( newErrHandle, GetFileSize(newErrHandle, NULL), NULL, FILE_BEGIN); if (errFP == (unsigned)-1) { if (ret == kSTAFOk) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString( "Error appending to stderr file: ") + startData->stderrRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } } } else if (!SetEndOfFile(newErrHandle)) { if (ret == kSTAFOk) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString( "Error setting the end of file for stderr file ") + startData->stderrRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } } } } else newErrHandle = GetStdHandle(STD_ERROR_HANDLE); // Note: Do not use DUPLICATE_CLOSE_SOURCE, as I am closing // the handles below. Since in some cases the handle // we are duplicating may be the actual console (when // no stdin/stdout/stderr has been specified), we get // into trouble by specifying this option. Instead I // check what we duplicated before closing the handle. startInfo.hStdInput = INVALID_HANDLE_VALUE; startInfo.hStdOutput = INVALID_HANDLE_VALUE; startInfo.hStdError = INVALID_HANDLE_VALUE; if ((ret == kSTAFOk) && (!DuplicateHandle(currentProcess, newInpHandle, currentProcess, &startInfo.hStdInput, 0, TRUE, DUPLICATE_SAME_ACCESS))) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString( "Error duplicating the handle for stdin file: ") + startData->stdinRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } if ((ret == kSTAFOk) && (!DuplicateHandle(currentProcess, newOutHandle, currentProcess, &startInfo.hStdOutput, 0, TRUE, DUPLICATE_SAME_ACCESS))) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString( "Error duplicating the handle for stdout file: ") + startData->stdoutRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } if ((ret == kSTAFOk) && (!DuplicateHandle(currentProcess, newErrHandle, currentProcess, &startInfo.hStdError, 0, TRUE, DUPLICATE_SAME_ACCESS))) { ret = kSTAFBaseOSError; systemErrorCode = GetLastError(); if (osRC) *osRC = systemErrorCode; if (errorBuffer) { STAFString_t systemErrorMsg = 0; STAFUtilWin32LookupSystemErrorMessage( systemErrorCode, &systemErrorMsg); STAFString errMsg = STAFString( "Error duplicating the handle for stderr file: ") + startData->stderrRedirect + "\nOS RC " + systemErrorCode; STAFString systemErrorString = STAFString( systemErrorMsg, STAFString::kShallow); if (systemErrorString.length() != 0) errMsg += ": " + STAFString(systemErrorString); *errorBuffer = errMsg.adoptImpl(); } } if (ret == kSTAFOk) { startInfo.dwFlags |= STARTF_USESHOWWINDOW; if (startData->consoleFocus == kSTAFProcessForeground) startInfo.wShowWindow = SW_SHOW; else if (startData->consoleFocus == kSTAFProcessMinimized) startInfo.wShowWindow = SW_SHOWMINNOACTIVE; else startInfo.wShowWindow = SW_SHOWNOACTIVATE; BOOL ranCreateProcessAsUser = FALSE; STAFString returnErrorMsg = ""; STAFString failedFunction = ""; if (hUsrToken == 0) { rc = CreateProcess( 0, command, 0, 0, TRUE, ((startData->consoleMode == kSTAFProcessNewConsole) ? CREATE_NEW_CONSOLE : 0) | CREATE_NEW_PROCESS_GROUP, environment, workdir, &startInfo, &processInfo); if (rc != TRUE) { systemErrorCode = GetLastError(); failedFunction = "Error starting the process. CreateProcess"; } } else { // Create Process As a Different User: // Direct the process into WinSta0, the interactive logon session startInfo.lpDesktop = "WinSta0\\default"; BOOL errorFound = FALSE; // Adjust the interactive winsta/desktop DACLs STAFString grantAccessToWinstaErrMsg; rc = GrantAccessToWinsta(hUsrToken, TRUE, grantAccessToWinstaErrMsg); if (rc != TRUE) { errorFound = TRUE; systemErrorCode = GetLastError(); failedFunction = "GrantAccessToWinsta (" + grantAccessToWinstaErrMsg + ")"; } else { // Load the user profile rc = sLoadUserProfileFunc(hUsrToken, &profinfo); } if (rc != TRUE) { if (!errorFound) { profinfo.hProfile = 0; errorFound = TRUE; systemErrorCode = GetLastError(); failedFunction = "LoadUserProfile"; } } else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -