Popen utf8路径

时间:2016-02-14 10:18:11

标签: c++ gcc utf-8

当我想用 _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();

在这个cout afther路径上,我得到了这个输出:C:\Windows\System32\certutil -hashfile C:\Users\Vuzee\Desktop\testÄ\test.jar

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

最后一个cout我得到:

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

1 个答案:

答案 0 :(得分:0)

您的应用程序可能会将字符串存储为UTF-8,但Windows操作系统有两种应用程序界面 - 也不是UTF-8。

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

进一步阅读: