在 C++ 中将字符串隐藏为 LPVOID 和 LPCWSTR

发布于 2024-08-20 15:07:47 字数 435 浏览 5 评论 0原文

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

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

发布评论

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

评论(4

从来不烧饼 2024-08-27 15:07:47

请注意 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().

眼眸里的那抹悲凉 2024-08-27 15:07:47

使用这个:

bool httpWrapper::setPostData(const string &postData){ 

 _postData = (LPWSTR)postData.c_str(); 

 _postData_len = 47; // Something else, actually.

 return false; 
}

LPWSTR _postData;

您可以将 LPWSTR 传递给需要 LPCWSTR 的方法。所以你可以使用字符串。

顺便说一句,您是否只是尝试传递字符串本身?我希望这也能起作用,并且比取出 LPWSTR 更好。

所以,在这种情况下,它看起来像:

bool httpWrapper::setPostData(const string &postData){ 

 _postData = postData; 

 _postData_len = 47; // Whatever.

 return false; 
}
string _postData;

Use this:

bool httpWrapper::setPostData(const string &postData){ 

 _postData = (LPWSTR)postData.c_str(); 

 _postData_len = 47; // Something else, actually.

 return false; 
}

LPWSTR _postData;

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:

bool httpWrapper::setPostData(const string &postData){ 

 _postData = postData; 

 _postData_len = 47; // Whatever.

 return false; 
}
string _postData;
给我一枪 2024-08-27 15:07:47

当然,有一种方法可以将字符串转换为某种内容,您可以获取一个地址并将其作为 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?

小女人ら 2024-08-27 15:07:47

LPVOID 只是 void*,因此您可以将任何指针转换为它,因为任何指针都可以转换为 void*。但是,它不保证此操作将给出您期望的有效结果。

简单地说,根据以下方案在情况下使用 LPVOID

int i = 10; // some real data
int* pi = &i; // pointer to data

// convert to opaque pointer, not usable (readable), onlly can be passed around
// for instance to thread procedure
void* pv = pi;

pi = reinterpret_cast<int*>(pv); // convert back to pointer to data
int j = *pi; // access real data

问题是,您必须保证 i 至少在通过 pv< 访问/使用数据时保持活动状态/代码> 指针。您必须考虑您的 w 那么

,您可以这样做:

bool httpWrapper::setPostData(const string &postData){ 

 _postData = reinterpret_cast<LPVOID>(postData.c_str()); 
 return false; 
}

但您必须保证您通过引用作为 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

int i = 10; // some real data
int* pi = &i; // pointer to data

// convert to opaque pointer, not usable (readable), onlly can be passed around
// for instance to thread procedure
void* pv = pi;

pi = reinterpret_cast<int*>(pv); // convert back to pointer to data
int j = *pi; // access real data

The problem is that you have to guarantee that i will stay alive for at least as long as the data is accessed/used through pv pointer. You have to consider if your w

So, you can do this:

bool httpWrapper::setPostData(const string &postData){ 

 _postData = reinterpret_cast<LPVOID>(postData.c_str()); 
 return false; 
}

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 by c_str()

Also, it seems you're going to use value returned by c_str() as LPWSTR. 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.

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