I need an example on downloading an internet file in a windows service application before logon using c++.

Dani AI

Generated

asked for a way to download an Internet file before user logon. Several replies rightly flagged that behavior as sensitive (, ). For legitimate use cases (pre-boot config, patch manifests, remote provisioning) the correct, supported pattern is: implement a Windows service that starts at system boot, runs under a suitable service account, performs the HTTP(S) download with a server-side HTTP API, writes to a machine-wide location, and handles retries and logging.

Prefer WinHTTP for service code (WinINet is intended for interactive apps). Run the service under a managed service account or NetworkService when you need network access; use LocalSystem only if absolutely required. Avoid per-user folders (no user profile is loaded before logon) — write to C:\ProgramData or the service profile. Implement TLS validation, proxy handling, and exponential backoff/retry in the service. See Microsoft guidance on WinHTTP and on service accounts for details. (learn.microsoft.com)

A compact synchronous WinHTTP pattern (error handling omitted) — adapt for production (certificate checks, timeouts, logging):

#include <windows.h>
#include <winhttp.h>
#include <stdio.h>
#pragma comment(lib, "winhttp.lib")

int DownloadToFile(const wchar_t* host, INTERNET_PORT port, const wchar_t* path, const wchar_t* outFile)
{
    HINTERNET hSession = WinHttpOpen(L"SvcDownloader/1.0",
        WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
    if (!hSession) return -1;
    HINTERNET hConnect = WinHttpConnect(hSession, host, port, 0);
    HINTERNET hRequest = WinHttpOpenRequest(hConnect, L"GET", path, NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES,
        (port == INTERNET_DEFAULT_HTTPS_PORT) ? WINHTTP_FLAG_SECURE : 0);
    WinHttpSendRequest(hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
    WinHttpReceiveResponse(hRequest, NULL);

    FILE* f = _wfopen(outFile, L"wb");
    DWORD avail = 0;
    while (WinHttpQueryDataAvailable(hRequest, &avail) && avail > 0) {
        BYTE* buf = (BYTE*)malloc(avail);
        DWORD read = 0;
        WinHttpReadData(hRequest, buf, avail, &read);
        fwrite(buf, 1, read, f);
        free(buf);
    }
    fclose(f);
    WinHttpCloseHandle(hRequest); WinHttpCloseHandle(hConnect); WinHttpCloseHandle(hSession);
    return 0;
}

Make the service resilient: set startup to Automatic (or Automatic (Delayed Start)), add explicit service dependencies (Tcpip, LanmanWorkstation or other needed components) or implement retries — otherwise the service may start before network stacks or domain logon services are ready. Use sc or the service installer to set DependOnService and test cold-boot and domain scenarios. For persistent, production deployments prefer gMSAs and thorough EventLog telemetry. (learn.microsoft.com)

Note: 's bootloader idea is technically possible but is extreme, risky, and unnecessary for almost all cases. A properly written, dependency-aware service is the pragmatic solution.

Recommended Answers

All 5 Replies

Why? This is a suspicious activity, so we need a good reason before helping you with it.

Why does it need to be before logon?

Why? This is a suspicious activity, so we need a good reason before helping you with it.

Yeah, that's indeed a suspicious activity ...

Why does it need to be before logon?

Good question, it's much easier to implement if it's after the logon process :) ...

Its really a suspicious activity .
But you could read a bit about registry , at http://www.bleepingcomputer.com/tutorials/tutorial44.html .
There are some pathes that start with windows before login or after .
You'l just need to add your exe file (or dll/sys file in some cases) to it start before login.

Go hard core. Write a bootloader to load your program with the proper drivers, download, clean up, goto real OS.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.