我希望能够使用WriteFile
以下WriteConsole
3 个功能:
> Output.exe
> Output.exe > File.txt & type File.txt
> for /F "tokens=*" %A in ('Output.exe') do @echo %A
我设法让前两个使用我当前的代码。但现在 for 循环输出:The system cannot write to the specified device.
我猜它不喜欢我的 BOM。如何使我的代码与 for 循环一起工作?
这是我当前的代码。
DWORD dwBytesWritten;
DWORD dwConMode;
BOOL bRedirectedToFile;
WCHAR str[] = L"Hello World";
bRedirectedToFile = !GetConsoleMode(hStdOut, &dwConMode);
if (bRedirectedToFile)
{
LONG zero = 0;
DWORD pos = SetFilePointer(hStdOut, 0, &zero, FILE_CURRENT);
if (pos == 0)
{
WORD Unicode = ((BYTE*)L"A")[0] == 0 ? 0xfffe : 0xfeff;
WriteFile(hStdOut, &Unicode, 2, &dwBytesWritten, 0);
}
WriteFile(hStdOut, str, lstrlen(str) * sizeof(WCHAR), &dwBytesWritten, 0);
}
else
{
WriteConsole(hStdOut, str, lstrlen(str), &dwBytesWritten, 0);
}