Hi

I am trying to convert the outbuff from WinHttpQueryHeaders to string but its not working properly...

my code

if( GetLastError( ) == ERROR_INSUFFICIENT_BUFFER )
    {

        lpOutBuffer = new WCHAR[dwSize/sizeof(WCHAR)];

		
        // Now, use WinHttpQueryHeaders to retrieve the header.
        bResults = WinHttpQueryHeaders( hRequest, 
                                   WINHTTP_QUERY_RAW_HEADERS_CRLF,
                                   WINHTTP_HEADER_NAME_BY_INDEX, 
                                  lpOutBuffer, &dwSize, 
                                   WINHTTP_NO_HEADER_INDEX);

	


    }
}

string str=(char*)lpOutBuffer;

string just showing the 1st charecter "H" after that.

Thanks in advance

Dani AI

Generated

the buffer you get back from WinHttpQueryHeaders is a UTF-16 (wide) string. If you cast that to char* and build a std::string, the first byte of the first wide character is 'H' and the second byte is 0, so the narrow string stops there. Treat it as wide, or convert it. The API also reports buffer lengths in bytes, not characters, so size calculations must divide by sizeof(WCHAR). Docs: WinHttpQueryHeaders. (learn.microsoft.com)

Two safe options:

  • Keep it wide: wrap the buffer in std::wstring and use wide-aware printing/logging.
  • Convert explicitly to UTF-8 (or a code page you choose) with WideCharToMultiByte:
const wchar_t* w = /* buffer filled by WinHttpQueryHeaders */;
int need = WideCharToMultiByte(CP_UTF8, 0, w, -1, nullptr, 0, nullptr, nullptr);
std::string headers;
headers.resize(need - 1);                // exclude the terminating '\0'
WideCharToMultiByte(CP_UTF8, 0, w, -1, headers.data(), need, nullptr, nullptr);
// use headers

WideCharToMultiByte is the supported, locale-independent way to convert from UTF-16; it also lets you choose the target encoding (UTF-8 via CP_UTF8, or the process ANSI code page via CP_ACP if you must). Docs: WideCharToMultiByte. (learn.microsoft.com)

And +1 to ’s point: sprintf is not a Unicode converter. It is locale/codepage dependent and its %S/%ls handling has differed across MSVC versions, which makes it fragile for this job. Prefer an explicit conversion as above. Background on %S/%ls in MSVC. (devblogs.microsoft.com)

Practical tip: use the two-call pattern with WINHTTP_NO_OUTPUT_BUFFER to get the required size first; remember the size is in bytes. Then allocate dwSize/sizeof(WCHAR) wide chars before calling again. Docs: WinHttpQueryHeaders. (learn.microsoft.com)

Recommended Answers

All 2 Replies

Hi

I am trying to convert the outbuff from WinHttpQueryHeaders to string but its not working properly...

my code

if( GetLastError( ) == ERROR_INSUFFICIENT_BUFFER )
    {

        lpOutBuffer = new WCHAR[dwSize/sizeof(WCHAR)];

		
        // Now, use WinHttpQueryHeaders to retrieve the header.
        bResults = WinHttpQueryHeaders( hRequest, 
                                   WINHTTP_QUERY_RAW_HEADERS_CRLF,
                                   WINHTTP_HEADER_NAME_BY_INDEX, 
                                  lpOutBuffer, &dwSize, 
                                   WINHTTP_NO_HEADER_INDEX);

	


    }
}

string str=(char*)lpOutBuffer;

string just showing the 1st charecter "H" after that.

Thanks in advance

The string assignment operator expects a null terminated character array. Meaning as soon as it sees a 0 value, it will stop. The extra bits in WCHAR probably contain a 0 after the H as padding, since it's unused. You'll need to find a way to convert WCHAR to char before trying to use the assignment operator like this. Or maybe you can use wstring. I try to stay away from that whole "wide" character mess, so I'm not the best one to ask... but now that you know what the problem is you probably know better than me how to fix it.
-Greywolf

thanks for replay , but i have already solved the problem using sprintf :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.