Reputation: 672
I am trying to upload a file to a PHP page using WinInet. I'm getting an Access Violation on one of the functions, but can't see why. I've built the code from an example page.
Here is the code:
HINTERNET aInternet=InternetOpen("My-Custom-Agent/1.0",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
HINTERNET aConnect=InternetConnect(aInternet,"www.myserver.com",INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1);
if (aConnect)
{
HINTERNET aRequest=HttpOpenRequest(aConnect, (const char*)"POST","myphppage.php", NULL, NULL, (const char**)"*/*\0",0,1);
// ^^
// Exception happens on this line
// Exception thrown at 0x70C85B7C (ininet.dll) in TestApp.exe:
// 0xC00000005: Access violation reading location 0x002A2F2A
//
}
When I download from the server with InternetOpenURL()
, everything seems fine. It just doesn't like what I'm doing somewhere here. Any clue what I'm doing wrong?
Upvotes: 0
Views: 727
Reputation: 596266
Per the HttpOpenRequest()
documentation:
[in] lplpszAcceptTypes
A pointer to a null-terminated array of strings that indicates media types accepted by the client. Here is an example.
PCTSTR rgpszAcceptTypes[] = {_T("text/*"), NULL};
Failing to properly terminate the array with a NULL pointer will cause a crash.
You are passing in a pointer to a single null-terminated string, incorrectly type-casted to const char**
:
lplpszAcceptTypes -> "*/*"
But the function requires a pointer to an array of pointers to null-terminated strings, where the last element in the array must be NULL to terminate the array (since there is no function parameter to specify the number of elements in the array):
-----
lplpszAcceptTypes -> | 0 | -> "*/*"
|---|
| 1 | -> NULL
-----
See the difference?
The function is misinterpreting the content of your string literal as if it were a pointer, which it is not, hence the AV crash. The address where the AV is occurring, 0x002A2F2A
, is literally the same bytes as the content of your string literal ("*/*“
= 0x2A 0x2F 0x2A 0x00
).
You need to use this instead:
LPCSTR rgpszAcceptTypes[] = {"*/*", NULL};
HINTERNET aRequest = HttpOpenRequest(aConnect, "POST", "myphppage.php", NULL, NULL, rgpszAcceptTypes, 0, 1);
Upvotes: 2