[c++]代码库
UINT CFormatDisk::CMDFormat()
{
// 格式化命令行
m_strCMD.Format(_T("format.com %c:/fs:FAT32/q/v:%s/y"), m_cLetter, m_strLable);
SECURITY_ATTRIBUTES sa;
HANDLE hRead,hWrite;
//创建管道
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
if (!CreatePipe(&hRead,&hWrite,&sa,0))
{
return -1;
}
//创建子进程
STARTUPINFO si;
PROCESS_INFORMATION pi = {0};
si.cb = sizeof(STARTUPINFO);
GetStartupInfo(&si);
si.hStdError = hWrite;
si.hStdOutput = hWrite;
si.wShowWindow = SW_HIDE;
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
if (!CreateProcess(NULL, m_strCMD.GetBuffer(), NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi))
{
return -1;
}
m_strCMD.ReleaseBuffer();
//关闭写管道句柄
CloseHandle(hWrite);
UINT uPos = 0;
CString strInfo;
//读取管道数据直到子程序结束
char buffer[4097] = {0};
DWORD dwReadSize = 0;
// while(true)
// {
// if((ReadFile(hRead,buffer,4096,&dwReadSize,NULL) == NULL) || (dwReadSize == 0))
// break;
// }
//获得程序返回值
DWORD dwExitCode = 0;
GetExitCodeProcess(pi.hProcess, &dwExitCode);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return dwExitCode;
}