전에 쓴 적이 있습니다CMD 에코는 익명 파이프라인을 사용하여 구현됩니다지금 생각해보면 그 당시 코드가 정말 약해서 화가 나서 다시 작성했어요.
[mw_shl_code=c,true]void ExecuteCommand(WCHAR * pszCommand, WCHAR *pszResult, DWORD dwCount)
{ BOOL bRet = 거짓; USES_CONVERSION;
WCHAR szCmdPath[MAX_PATH] = {0}; GetSystemDirectory(szCmdPath, sizeof(szCmdPath)); PathAppend(szCmdPath, L"cmd.exe");
SECURITY_ATTRIBUTES 보안 속성; SecurityAttributes.nLength = sizeof(SECURITY_ATTRIBUTES); SecurityAttributes.lpSecurityDescrip{filter}tor = NULL; SecurityAttributes.bInheritHandle = TRUE;
핸들 hRead = NULL; 핸들 hWrite = NULL;
if(CreatePipe(&hRead, &hWrite, &SecurityAttributes, 0))
{ STARTUPINFO StartupInfo = {0}; StartupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; StartupInfo.hStdOutput = hWrite; StartupInfo.hStdError = hWrite; StartupInfo.wShowWindow = SW_HIDE;
PROCESS_INFORMATION 프로세스정보 = {0};
WCHAR szCurrentPath[MAX_PATH] = {0}; GetCurrentDirectory(sizeof(szCurrentPath), szCurrentPath);
if(CreateProcess(szCmdPath, pszCommand, NULL, NULL, TRUE, 0, NULL, szCurrentPath, &StartupInfo, &ProcessInformation))
{ 클로즈핸들(hWrite);
char szBuffer[4096] = {0}; DWORD dwRead = 0; if(pszResult)
{ pszResult[0] = 0; while(참)
{ memset(szBuffer, 0, sizeof(szBuffer)); bRet = ReadFile(hRead, szBuffer, sizeof(szBuffer), &dwRead, NULL); if((거짓 == bRet) || (dwRead = 0)) 브레이크;
wcscat_s(pszResult, dwCount, A2W(szBuffer));
}
}
}
클로즈핸들(hRead);
} }[/mw_shl_code]
|