c++11winhttp

sending a pointer of global variable's member to WinHttpQueryHeaders (WinHTTP API) does not change it's value


I have the following code snippet:

        // Using HttpQueryInfo to obtain the size of the buffer into dwSize.
        if (!WinHttpQueryHeaders(hRequest,
            WINHTTP_QUERY_RAW_HEADERS_CRLF,
            WINHTTP_HEADER_NAME_BY_INDEX, NULL, &st.dwSize, WINHTTP_NO_HEADER_INDEX))
        {
            // An ERROR_INSUFFICIENT_BUFFER is expected because you
            // are looking for the size of the headers.  If any other
            // error is encountered, display error information.
            DWORD dwErr = GetLastError();
            if (dwErr != ERROR_INSUFFICIENT_BUFFER)
            {
                DEBUG_PRINT(("Error %d encountered.", dwErr));
                return;
            } else {
                // enters here and prints '0' (initial value)
                DEBUG_PRINT(("size of buffer: ", &st.dwSize));
            }
        }

while st is a global object with a member dwSize.

When I'm running this part in debugging mode I see that st.dwSize does not change its value after the call to WinHttpQueryHeaders.

But if I create a local var DWORD dwSize = 0 and send &dwSize to WinHttpQueryHeaders, it does obtain buffer size and succeeds to change its value.

is there any reason why I shouldn't send a pointer of a global object's member to WinHttpQueryHeaders or to any other external API functions?


Solution

  • the reason WinHttpQueryHeaders did not succeed to change st.dwSize it's because I declered st as a static global var.

    static WinHttpSubtransport st;
    

    and as written in Scope rules of the "persistent" variables in C:

    A static global variable is a global variable that can only be accessed by functions in the same C program file as the variable.