MFC CreateProcess 不传递 UTF8 命令行参数来处理

发布于 2025-01-02 13:19:29 字数 823 浏览 0 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉梦枕江山 2025-01-09 13:19:29

根据您的编译器设置,您将调用 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 takes char strings in the current code page, or CreateProcessW which takes wchar_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.

猫弦 2025-01-09 13:19:29

假设您正在以多字节模式(而不是 unicode)进行编译。

CT2W path(m_strProg);
CreateProcessW(path, ...);

Assuming you are compiling in multi-byte mode (not unicode).

CT2W path(m_strProg);
CreateProcessW(path, ...);
红衣飘飘貌似仙 2025-01-09 13:19:29

只是说你那里有两个负面因素,用英语来说这使它成为正面因素 - 所以你说它实际上确实通过了 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文