0

When HTTP posting data; if I use std::wstring as a parameter not all the text is posted. But if I use a standard TCHAR array then all the text is posted. Why is this happening and how can I keep using the std::wstring?

// wont compile unless cast to LPVOID and not all data is sent. HttpSendRequest(hRequest, hdrs, -1, (LPVOID)fData.c_str(), fData.size()); // successfully sends all fData text TCHAR s[1024] = {0}; _tcscpy(s, fData.c_str()); HttpSendRequest(hRequest, hdrs, -1, s, sizeof(s)); 
5
  • 1
    "wont compile unless cast to LPVOID" tells you that you have the wrong types. Commented Jul 26, 2016 at 5:17
  • @Cheersandhth.-Alf the functions 4th parameter expects a LPVOID, ie, void pointer. A void pointer can be any type. Commented Jul 26, 2016 at 5:21
  • HttpSendRequest should use fData.size() * 2 if that's std::wstring Commented Jul 26, 2016 at 5:23
  • @JakeM: Any pointer T* converts implicitly to void*, no cast required. So presumably this is a const_cast then. You could have included that information: the comment indicated wrong type. Commented Jul 26, 2016 at 5:28
  • 1
    Are you sure you actually want to send fData "as is"? HttpSendRequest does not perform any translation of that parameter, it just sends the raw bytes, so you are assuming that on the other end there's somebody who is expecting to receive UTF-16 data, which is a bit unusual in the HTTP world. Commented Jul 26, 2016 at 6:50

1 Answer 1

1

The HttpSendRequest documentation says:

dwOptionalLength [in]
The size of the optional data, in bytes. This parameter can be zero if there is no optional data to send.

size() returns the number of character elements in the string, not the size of the string in bytes. The byte size of a wchar_t on Windows is 2, so instead of this:

HttpSendRequest(hRequest, hdrs, -1, (LPVOID)fData.c_str(), fData.size()); 

It needs to be this:

HttpSendRequest(hRequest, hdrs, -1, (LPVOID)fData.c_str(), fData.size() * sizeof(wchar_t)); 
Sign up to request clarification or add additional context in comments.

2 Comments

I am sure that works for you but there is no relation between TCHAR and std::wstring, or whatever fData is supposed to be.
I fixed the answer. There is no place for TCHAR here.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.