InternetWriteFile function
Namespace: WinInet
Header: wininet.h
Syntax
BOOL InternetWriteFile(
HINTERNET hFile,
LPCVOID lpBuffer,
DWORD dwNumberOfBytesToWrite,
LPDWORD lpdwNumberOfBytesWritten
);
Parameters
| Parameter | Type | Description |
|---|---|---|
hFile |
HINTERNET |
Handle returned by InternetOpenUrl, FtpOpenFile, or HttpOpenRequest. |
lpBuffer |
LPCVOID |
Pointer to the buffer that contains the data to be written. |
dwNumberOfBytesToWrite |
DWORD |
Number of bytes to write from lpBuffer. |
lpdwNumberOfBytesWritten |
LPDWORD |
Pointer to a variable that receives the number of bytes written. |
Return value
Returns TRUE if successful; otherwise FALSE. Use GetLastError for extended error information.
Remarks
- The function writes data to an open file handle that was obtained through WinInet functions.
- When uploading data (e.g., via HTTP POST), you typically call
InternetWriteFileafterHttpOpenRequestand beforeHttpEndRequest. - For large transfers, consider calling the function repeatedly until all data is sent.
Example
#include <windows.h>
#include <wininet.h>
#pragma comment(lib, "wininet.lib")
int main() {
HINTERNET hSession = InternetOpen(L"MyUploader", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hSession) return 1;
HINTERNET hConnect = InternetConnect(hSession, L"example.com", INTERNET_DEFAULT_HTTP_PORT,
NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnect) { InternetCloseHandle(hSession); return 1; }
HINTERNET hRequest = HttpOpenRequest(hConnect, L"POST", L"/api/upload",
NULL, NULL, NULL, INTERNET_FLAG_RELOAD, 0);
if (!hRequest) { InternetCloseHandle(hConnect); InternetCloseHandle(hSession); return 1; }
const char* data = "field1=value1&field2=value2";
DWORD bytesWritten = 0;
BOOL result = InternetWriteFile(hRequest, data, (DWORD)strlen(data), &bytesWritten);
HttpEndRequest(hRequest, NULL, 0, 0);
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
return result ? 0 : 1;
}