如何转换PWSTR为C ++中的字符串? [英] How do I convert PWSTR to string in C++?

查看:165
本文介绍了如何转换PWSTR为C ++中的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

// Fetch Local App Data folder path.
PWSTR localAppData = (PWSTR) malloc(128);
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &localAppData);

// Find out the absolute path to chrome.exe
stringstream ss;
ss << localAppData << "/Google/Chrome/Application/chrome.exe";

stringstreamer的结果 .str() 008F6788 / Google / Chrome / Application / chrome.exe ,这是错误的。

The result of stringstreamer's .str() is 008F6788/Google/Chrome/Application/chrome.exe, which is wrong.

因为类型不兼容,所以strstream或wcsncat都不能工作。

I can't seem to get stringstreamer to work, neither do strcat or wcsncat due to type incompatibilities.

如何将此PWSTR转换为字符串?

How do I cast this PWSTR to string?

推荐答案

[h1> 1。 Yuk!

Microsoft

1. Yuk!

Microsoft says:

typedef wchar_t* LPWSTR, *PWSTR;

所以让我们从你的测试用例中得到可怕的废话,并丢失C垃圾:

So let's get that horrid nonsense out of your testcase, and lose the C rubbish:

// Fetch Local App Data folder path.
wchar_t* localAppData = new wchar_t[128];
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &localAppData);

stringstream ss;
ss << localAppData << "/Google/Chrome/Application/chrome.exe";

delete[] localAppData;






2。警告!



这里有一个严重的缺陷。

SHGetKnownFolderPath 实际上设置指针的值,指向分配的内存 。您的代码有一个内存泄漏,我的最后一个代码段错误地释放内存错误。

SHGetKnownFolderPath actually sets the value of the pointer you give it to point to memory that it allocated. Your code has a memory leak, and my last snippet frees the memory subtly wrongly.

让我们通过阅读文档


ppszPath [out]

Type: PWSTR*

当此方法返回时,包含指向以null结束的Unicode字符串的指针的地址,该字符串指定已知文件夹的路径。调用过程负责通过调用CoTaskMemFree来释放这个资源,不再需要它。返回的路径不包括尾部反斜杠。例如,返回C:\Users,而不是C:\Users \。

When this method returns, contains the address of a pointer to a null-terminated Unicode string that specifies the path of the known folder. The calling process is responsible for freeing this resource once it is no longer needed by calling CoTaskMemFree. The returned path does not include a trailing backslash. For example, "C:\Users" is returned rather than "C:\Users\".



// Fetch Local App Data folder path.
wchar_t* localAppData = 0;
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &localAppData);

stringstream ss;
ss << localAppData << "/Google/Chrome/Application/chrome.exe";

CoTaskMemFree(static_cast<void*>(localAppData));

现在开始。

您的代码的语法问题是localAppData是 wchar_t ,但是正常 stringstream 可在 char 上运行。

The syntax issue with your code is that localAppData is a wchar_t, but normal stringstreams work on char.

幸运的是,使用 wchar_t wstringstream 变体。

Fortunately, there is a wide-char variant called wstringstream that uses wchar_t instead.

注意,这意味着你的文字必须使用 L 字符串文字前缀,从 wchar_t 。)

(Note that this means your literal will have to be built out of wchar_ts, too, using the L string literal prefix.)

现在最终代码:

// Fetch Local App Data folder path.
wchar_t* localAppData = 0;
SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &localAppData);

wstringstream ss;
ss << localAppData << L"/Google/Chrome/Application/chrome.exe";

CoTaskMemFree(static_cast<void*>(localAppData));

这篇关于如何转换PWSTR为C ++中的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆