MFC CreateProcess 不传递 UTF8 命令行参数来处理
我尝试使用 CreateProcess 从 MFC 应用程序启动控制台应用程序。
cmd 变量是 CString,其中包含应用程序名称和命令行 arg(中文 UTF8 文件名)。
文件名未以 UTF8 格式传递,应用失败。
如何以正确的方式发送命令?
BOOL bRetVal = ::CreateProcess( NULL,
cmd.GetBuffer(m_strProg.GetLength()), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi // Pointer to PROCESS_INFORMATION structure.
);
I try to start console application from MFC application using CreateProcess.
cmd variable is CString which contains the application name and command line arg which is chineese UTF8 file name.
The file name is not passed in UTF8 format and the application fails.
How can I send the command in the right way?
BOOL bRetVal = ::CreateProcess( NULL,
cmd.GetBuffer(m_strProg.GetLength()), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi // Pointer to PROCESS_INFORMATION structure.
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的编译器设置,您将调用
CreateProcessA
(它接受当前代码页中的char
字符串)或CreateProcessW
(它接受Unicode UTF-16 格式的 wchar_t
字符串。它们都无法处理 UTF-8 字符串作为输入。您应该将 UTF-8 字符串转换为 UTF-16,然后从中构建命令行。
Depending on your compiler settings, you will be calling either
CreateProcessA
which takeschar
strings in the current code page, orCreateProcessW
which takeswchar_t
strings in Unicode UTF-16. Neither of those will be able to handle a UTF-8 string as input.You should convert the UTF-8 string to UTF-16, then build your command line from that.
假设您正在以多字节模式(而不是 unicode)进行编译。
Assuming you are compiling in multi-byte mode (not unicode).
只是说你那里有两个负面因素,用英语来说这使它成为正面因素 - 所以你说它实际上确实通过了 UTF8。无论如何,您需要阅读 http://www.joelonsoftware.com/articles/Unicode.html< /a> 另外,您需要更改您的区域设置和代码页,类似于 Windows 命令行中的 Unicode 字符 - 如何操作?
Was just saying that you have two negatives there, in english that makes it a positive - so you were saying that it does, in fact, pass UTF8. Anyway, you need to read http://www.joelonsoftware.com/articles/Unicode.html Also, you need to change your locale and codepage, something along the lines of Unicode characters in Windows command line - how?