如何解决将unicode文本插入cmd.exe
我希望将unicode(UTF-16)文本写入cmd的stdin并从其stdout读取unicode,但是cmd帮助只能说它可以使用/ U开关输出unicode。我注意到该站点中有一些QA声称cmd.exe能够接受unicode输入,但是找不到一个告诉我如何使用管道的代码。
在下面的代码示例中,创建了具有多语言名称的文件,然后使用cmd.exe / U对其进行操作。 cmd进程正确输出utf-16文本,但仍希望使用ansi输入。
STARTUPINFOW si={.cb=sizeof(STARTUPINFOW),.dwFlags=STARTF_FORCEOFFFEEDBACK|STARTF_USESTDHANDLES};
HANDLE hWritePipeStdIn,hReadPipeStdOut;
CreatePipe(&si.hStdInput,&hWritePipeStdIn,0);
CreatePipe(&hReadPipeStdOut,&si.hStdOutput,0);
SetHandleInformation(si.hStdInput,HANDLE_FLAG_INHERIT,1);
SetHandleInformation(si.hStdOutput,1);
si.hStdError=si.hStdOutput;
CloseHandle(CreateFileW(L".\\aaaالعربيةবাংলা日本語한국어ไทยTiếng ViệtYorùbá中文",FILE_WRITE_DATA,CREATE_ALWAYS,0));//create a file with multilanguage name
PROCESS_INFORMATION pi;
wchar_t cmdline[]=L"cmd.exe /U";//set output unicode
CreateProcessW(0,cmdline,1,CREATE_NO_WINDOW,&si,&pi);
CloseHandle(si.hStdInput);CloseHandle(si.hStdOutput);
CloseHandle(pi.hProcess);CloseHandle(pi.hThread);
HANDLE hFile=CreateFileW(L".\\a.txt",FILE_SHARE_READ,0);
char buf[512];DWORD len;
static char cmdline1[]="dir /b aaa*&exit\r\n";
WriteFile(hWritePipeStdIn,cmdline1,sizeof cmdline1-sizeof*cmdline1,&len,0);//still accepts ansi input
static wchar_t cmdline2[]=L"del aaaالعربيةবাংলা日本語한국어ไทยTiếng ViệtYorùbá中文&exit\r\n";
//WriteFile(hWritePipeStdIn,cmdline2,sizeof cmdline2-sizeof*cmdline2,0);//can't use this. how to delete the file using cmd?
while(ReadFile(hReadPipeStdOut,buf,sizeof buf,0))WriteFile(hFile,len,0);//a.txt is filled with legit UTF-16 text from the dir command
CloseHandle(hFile);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。