Search code examples
c++stringwinhttp

Covert String to LPVOID and LPCWSTR in C++


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


Solution

  • 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;