不确定它是否在我的代码中或我如何处理启动进程,但在下面的代码中,如果我打开布尔 outputToFile TRUE,程序将愉快地将 UWFMGR 的标准输出输出到 out.log 文件和它是可读的,看起来就像从 cmd 行执行时一样。
如果我关闭该布尔值并尝试使用我从管道中读取的标准输出,那它就是垃圾......像“UA n yzna......a/”这样的东西。buf、csTemp 和 m_csOutput 都有 off 字符。如果我使用其他程序(如 ipconfig、netsh 等)而不是 uwfmgr 运行相同的程序,它会完美运行。过去我什至在 ewfmgr 中使用过这段代码,它运行良好。我不确定 uwfmgr 有什么不同,但它破坏了这段代码。另一个必须是线索的奇怪的事情是 WaitForSingleObject 可以正常工作,除了 uwfmgr,当我运行 uwfmgr 时,WaitForSingleObject 永远不会返回并永远等待。
另一件需要注意的事情是对 uwfmgr 的调用可以正常工作,例如“uwfmgr filter enable”将在系统重新启动时启用过滤器,即使 std 输出不可读。
有任何想法吗?谢谢!
String csExecute;
csExecute = "uwfmgr get-config";
bool outputToFile = FALSE;
SECURITY_ATTRIBUTES secattr;
ZeroMemory(&secattr, sizeof(secattr));
secattr.nLength = sizeof(secattr);
secattr.bInheritHandle = TRUE;
secattr.lpSecurityDescriptor = NULL;
HANDLE rPipe = NULL;
HANDLE wPipe = NULL;
//For test use....File works and captures output correctly
HANDLE h = CreateFile(_T("out.log"),
FILE_APPEND_DATA,
FILE_SHARE_WRITE | FILE_SHARE_READ,
&secattr,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL );
// Create pipes to write and read data
//if(outputToFile)
//{
// CreatePipe(&rPipe, &h, &secattr, 0);
//}else
//{
CreatePipe(&rPipe, &wPipe, &secattr, 0);
//}
STARTUPINFOW sInfo;
PROCESS_INFORMATION pInfo;
ZeroMemory(&sInfo, sizeof(STARTUPINFOW));
ZeroMemory(&pInfo, sizeof(PROCESS_INFORMATION));
sInfo.cb = sizeof(STARTUPINFOW);
sInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
sInfo.wShowWindow = SW_HIDE;
sInfo.hStdInput = NULL;
if(outputToFile)
{
sInfo.hStdOutput = h;
sInfo.hStdError = h;
}else
{
sInfo.hStdOutput = wPipe;
sInfo.hStdError = wPipe;
}
char command[1024];
wcstombs(command, csExecute.c_str(), sizeof(command));
ShowMessage(csExecute);
CreateProcessWithLogonW(L"username", L"PCDOMAIN", L"password",
LOGON_NETCREDENTIALS_ONLY, NULL, csExecute.w_str(),
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, NULL,
&sInfo, &pInfo);
//Never returns with UWFMGR, works fine on other processes like ipconfig, netsh, ect
//WaitForSingleObject(pInfo.hThread, Infinite);
if(outputToFile)
{
CloseHandle(h);
}else
{
CloseHandle(wPipe);
}
//returns 0 and works fine (actual call to uwfmgr succeeded, for example uwfmgr filter enable turns the filter on even though the output is garbage unless outputted to a file
ShowMessage(GetLastError());
// now read the output pipe here.
#define BUFSIZE 100
BOOL res = FALSE;
String m_csOutput = "";
for(;;)
{
char buf[BUFSIZE+1] = "";
DWORD reDword;
if(!::ReadFile(rPipe, buf, BUFSIZE, &reDword, 0))
{
DWORD error = ::GetLastError();
ShowMessage("Error #....");
ShowMessage(error);
break;
}
if(reDword == 0)
{
break;
}
buf[reDword] = '\0';
ShowMessage(buf);
String csTemp = buf;
ShowMessage(csTemp);
m_csOutput += csTemp;//.SubString(1, reDword);
ShowMessage(m_csOutput);
}