Popen utf8 path

Popen utf8 path

本文关键字:path utf8 Popen      更新时间:2023-10-16

我想用 _popen 打开 utf8 路径时遇到问题

所以这是我的代码:

char buf1520[1500];
string testme;
const char * sroot2 = getenv ("systemroot");
string md5cmd2 = sroot2;
md5cmd2 += "\System32\certutil -hashfile ";
md5cmd2 += "C:\Users\Vuzee\Desktop\testč\test.jar";
cout << md5cmd2 << endl;
md5cmd2 += " MD5";
const char* md5cmdnovo2 = md5cmd2.c_str();
FILE *p1502 = _popen(md5cmdnovo2, "r");
for (size_t count; (count = fread(buf1520, 1, sizeof(buf1520), p1502));)
    testme += string(buf1520, buf1520 + count);
    _pclose(p1502);
cout << "HASH:" << testme << endl;
cin.ignore();

在这条路径上,我得到以下输出:C:WindowsSystem32certutil -hashfile C:UsersVuzeeDesktoptestÄtest.jar

所以我认为问题是因为字符串无法保存 UTF8 字符,那么我该如何解决这个问题并在 popen 中调用它?

对于最后的cout,我得到:

HASH:CertUtil: -hashfile command FAILED: 0x80070003 (WIN32: 3)
CertUtil: The system cannot find the path specified.

您的应用程序可以将字符串存储为 UTF-8,但 Windows 操作系统有两种类型的应用程序接口 — UTF-8 也不是。

您可以使用处理wchar_t(宽字符(的 _wopen 函数使应用程序正常工作。 首先,您必须将 md5cmd2 中的 string 值转换为宽字符串(转换为 char* 将不起作用(。

延伸阅读:

  • 我想将 std::string 转换为常量 wchar_t *
  • _popen, _wpopen