将全局变量成员的指针发送到 WinHttpQueryHeaders (WinHTTP API) 不会改变它的值
sending a pointer of global variable's member to WinHttpQueryHeaders (WinHTTP API) does not change it's value
我有以下代码片段:
// 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));
}
}
而 st
是具有成员 dwSize
的全局对象。
当我 运行 这部分处于调试模式时,我看到 st.dwSize
在调用 WinHttpQueryHeaders
后没有改变它的值。
但是如果我创建一个本地变量 DWORD dwSize = 0
并将 &dwSize
发送到 WinHttpQueryHeaders
,它会获取缓冲区大小并成功更改其值。
为什么我不应该将全局对象成员的指针发送到 WinHttpQueryHeaders
或任何其他外部 API 函数?
WinHttpQueryHeaders
没有成功更改的原因 st.dwSize
是因为我将 st
declered 作为 static
全局变量。
static WinHttpSubtransport st;
如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.
我有以下代码片段:
// 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));
}
}
而 st
是具有成员 dwSize
的全局对象。
当我 运行 这部分处于调试模式时,我看到 st.dwSize
在调用 WinHttpQueryHeaders
后没有改变它的值。
但是如果我创建一个本地变量 DWORD dwSize = 0
并将 &dwSize
发送到 WinHttpQueryHeaders
,它会获取缓冲区大小并成功更改其值。
为什么我不应该将全局对象成员的指针发送到 WinHttpQueryHeaders
或任何其他外部 API 函数?
WinHttpQueryHeaders
没有成功更改的原因 st.dwSize
是因为我将 st
declered 作为 static
全局变量。
static WinHttpSubtransport st;
如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.