m***@gmail.com
2017-05-09 15:25:00 UTC
Hi,
I just started with WinHttp and I'm glad I succeeded with most tasks/obstacles.
One obstacle lasts - to get a "custom" header I used the following scheme to get the value:
1. use WinHttpQueryHeaders with WINHTTP_QUERY_CUSTOM and a nullptr as LPVOID parameter to get the needed size of the buffer.
2. I allocate memory (alloca or malloc) for the buffer
3. WinHttpQueryHeaders is called a second time with the buffer to retrieve the header value
(4. I free memory allocated with malloc)
Using alloca the memory corruption exception is raised by VS2015 leaving the function, while the heap corruption exception is raised by VS2015 as soon as the memory gets deallocated.
Here the source code example:
DWORD dwBufferSize = 0;
WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_CUSTOM,
L"X-W3C-Validator-Status",
nullptr, &dwBufferSize,
WINHTTP_NO_HEADER_INDEX);
dwError = GetLastError();
if (dwError == ERROR_INSUFFICIENT_BUFFER)
{
LPWSTR pszBuffer = (LPWSTR)malloc(dwBufferSize / sizeof(WCHAR));
if (WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_CUSTOM,
L"X-W3C-Validator-Status",
(LPVOID)pszBuffer, &dwBufferSize,
WINHTTP_NO_HEADER_INDEX) != FALSE)
{
// ... do something ...
}
free(pszBuffer);
}
The values I retrieve:
1. call: dwBufferSize = 12
2. call:
- dwBufferSize = 10
- pszBuffer = L"Valid"
I tested by allocating more and setting the memory to 0xFF to see, if something weird is going on, but I didn't get it!
Anychance to get help here?
Best regards,
Martin Lemburg
I just started with WinHttp and I'm glad I succeeded with most tasks/obstacles.
One obstacle lasts - to get a "custom" header I used the following scheme to get the value:
1. use WinHttpQueryHeaders with WINHTTP_QUERY_CUSTOM and a nullptr as LPVOID parameter to get the needed size of the buffer.
2. I allocate memory (alloca or malloc) for the buffer
3. WinHttpQueryHeaders is called a second time with the buffer to retrieve the header value
(4. I free memory allocated with malloc)
Using alloca the memory corruption exception is raised by VS2015 leaving the function, while the heap corruption exception is raised by VS2015 as soon as the memory gets deallocated.
Here the source code example:
DWORD dwBufferSize = 0;
WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_CUSTOM,
L"X-W3C-Validator-Status",
nullptr, &dwBufferSize,
WINHTTP_NO_HEADER_INDEX);
dwError = GetLastError();
if (dwError == ERROR_INSUFFICIENT_BUFFER)
{
LPWSTR pszBuffer = (LPWSTR)malloc(dwBufferSize / sizeof(WCHAR));
if (WinHttpQueryHeaders(
hRequest,
WINHTTP_QUERY_CUSTOM,
L"X-W3C-Validator-Status",
(LPVOID)pszBuffer, &dwBufferSize,
WINHTTP_NO_HEADER_INDEX) != FALSE)
{
// ... do something ...
}
free(pszBuffer);
}
The values I retrieve:
1. call: dwBufferSize = 12
2. call:
- dwBufferSize = 10
- pszBuffer = L"Valid"
I tested by allocating more and setting the memory to 0xFF to see, if something weird is going on, but I didn't get it!
Anychance to get help here?
Best regards,
Martin Lemburg