在 C++ 中将字符串隐藏为 LPVOID 和 LPCWSTR
我正在使用 c++ 中的 winHTTP API,并且已经开始为我的应用程序编写一个包装类。为了简单起见,我有许多函数可以接受字符串参数并在 winHTTP 调用中使用它们。然而,其中许多要求数据为 LPVOID 或 LPCWSTR。我可以通过让我的包装函数采用 LPVOID 或 LPCWSTR 参数来使其全部工作,但我更喜欢字符串参数。那么有什么办法可以从 sting 转换为 LPVOID/LPCWSTR 呢?
我的尝试(如下)只是产生了胡言乱语
bool httpWrapper::setPostData(const string &postData){
_postData = (LPVOID)postData.c_str();
_postData_len = 47;
return false;
}
任何帮助将不胜感激
谢谢
I’m working with the winHTTP API in c++ and I have started to write a wrapper class for my application. For simplicity I have a number of functions that can take string parameters and use them in the winHTTP calls. However many of these require the data to be LPVOID or LPCWSTR. I can make it all work by making my wrapper functions take LPVOID or LPCWSTR parameters, but I would prefer string parameters. So is there any way to covert from sting to LPVOID/ LPCWSTR?
My attempts (below) just produced jibberish
bool httpWrapper::setPostData(const string &postData){
_postData = (LPVOID)postData.c_str();
_postData_len = 47;
return false;
}
Any help would be much appreciated
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请注意 c_str() 的行为。它返回的 char* 是临时的,在字符串被修改或破坏后它将变得无效。这很可能在使用 _postData 之前发生,可能在 setPostData() 返回时发生。你需要复制它。
下一个问题是 c_str() 不返回 LPCWSTR,它是 LPCSTR。演员表无法转换它,从而产生中文。您需要使用例如 MultiByteToWideChar() 将其转换为 Unicode 字符串。
Be careful for behavior of c_str(). The char* it returns is temporary, it will become invalid after the string is modified or destructed. Which may well happen before the _postData is used, possibly as soon as setPostData() returns. You'll need to copy it.
The next problem is that c_str() doesn't return a LPCWSTR, it is a LPCSTR. A cast cannot convert it, that produces Chinese. You'll need to convert it to a Unicode string with for example MultiByteToWideChar().
使用这个:
您可以将 LPWSTR 传递给需要 LPCWSTR 的方法。所以你可以使用字符串。
顺便说一句,您是否只是尝试传递字符串本身?我希望这也能起作用,并且比取出 LPWSTR 更好。
所以,在这种情况下,它看起来像:
Use this:
You can pass a LPWSTR to methods which expect a LPCWSTR. So you can work with strings.
btw, did you just try passing the string itself? I would expect that to work too and is better than getting a LPWSTR out.
So, in that case it would look like:
当然,有一种方法可以将字符串转换为某种内容,您可以获取一个地址并将其作为 LPCWSTR 或 LPVOID 传递(如果是 LPVOID,则可能会有所不同)。
现在请仔细阅读上述声明。
要知道该怎么做,首先您必须知道 LPCWSTR 和 LPVOID 是什么。我有一种奇怪的直觉,感觉这些可能是一些指示。 MSDN 可能会有所帮助。尝试此处例如
然后你必须知道您将这些参数传递给的函数将如何处理这些指针指向的内存。是直接读取还是修改?如果是 LPVOID,它将如何解释它。
我无法帮助您使用 LPVOID,因为我不知道 API,但 LPCWSTR 建议您必须创建一个以 null 结尾的 wide 字符串,该字符串将被读取。创建一个 wstring 并使用它的 c_str() 方法怎么样?
Of course there is a way to convert your string to something, You can take an address of and pass it as LPCWSTR or as LPVOID (it may be a different thing in case of LPVOID).
Now read the above statement carefully.
To know what to do, first You have to know what LPCWSTR and LPVOID are. I have a strange gut feeling those might be some pointers. MSDN might help. Try here for example
Then You have to know what the function, You pass those parameters to, is going to do with the memory those pointers point to. Is it going to just read it or maybe modify it? And how is it going to interpret it in case of LPVOID.
I can't help You with LPVOID because I don't know the API, but LPCWSTR suggests You have to create a null terminated wide string that will be just read. How about creating a wstring and using it's c_str() method?
LPVOID 只是 void*,因此您可以将任何指针转换为它,因为任何指针都可以转换为 void*。但是,它不保证此操作将给出您期望的有效结果。
简单地说,根据以下方案在情况下使用 LPVOID
问题是,您必须保证
i
至少在通过pv< 访问/使用数据时保持活动状态/代码> 指针。您必须考虑您的 w 那么
,您可以这样做:
但您必须保证您通过引用作为
postData
传递的字符串对象将保持活动状态至少与_postData< /code> 指向它。事实上,
_postData
指向c_str()
返回的内部位置此外,看来您将使用
c_str()
返回的值> 作为LPWSTR
。要使用 LPWSTR,您需要从 ANSI 转换为宽字符,例如使用 MultiByteToWideChar 函数。换句话说,从一个指针到另一个指针的转换本身并不是问题。问题是保证适当的对象生命周期和使用。
LPVOID is just void*, so you can convert any pointer to it as any pointer is convertible to void*. However, it does not guarantee that this operation will give valid result in terms of your expectations.
Simply, LPVOID is used in situations according to the following scheme
The problem is that you have to guarantee that
i
will stay alive for at least as long as the data is accessed/used throughpv
pointer. You have to consider if your wSo, you can do this:
but you have to guarantee that the string object you pass by reference as
postData
will stay alive for at least as long as_postData
points to it. In fact,_postData
points to internal location of returned byc_str()
Also, it seems you're going to use value returned by
c_str()
asLPWSTR
. To use LPWSTR you need to convert from ANSI to wide characters for instance, using MultiByteToWideChar function.In other words, the conversion from one pointer to another is not a problem itself. The problem is to guarantee proper objects lifetime and usage.