views:

384

answers:

4

Hi,

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

A: 

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;
Moron
+1  A: 

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().

Hans Passant
A: 

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.

mloskot
A: 

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?

Maciej Hehl