how to use urlmon.h DownloadProgressCallback in C language, not c++

66 views Asked by At

I hope make progress bar to urlmon in C. but it wasn't work I don't know what the problem is. If there's another way to download the URL and receive download progress, I will give that a try and if this question messed. please inform me, i Modify this question

#include <stdio.h>
#include <windows.h>
#include <urlmon.h>

// 진행 상황 콜백 함수
void DownloadProgressCallback(
    ULONG ulProgress,
    ULONG ulProgressMax) {
}

int main() {
    HRESULT hr;
    LPCWSTR url = L"LINK";
    LPCWSTR localFileName = L"C:/Temp/test.zip";

    // 진행 상황 콜백을 사용하여 다운로드
    hr = URLDownloadToFile(NULL, url, localFileName, 0, DownloadProgressCallback);

    if (hr == S_OK) {
        wprintf(L"\nDownload completed successfully.\n");
    }
    else {
        wprintf(L"\nDownload failed with error code: 0x%08X\n", hr);
    }

    return 0;
}


Result "Thread 0x4754 has exited (code: 0x0). Exception thrown (0x00007FFC2A1045CF in urlmon.dll, TestURL.exe): 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF."

1

There are 1 answers

3
the busybee On BEST ANSWER

Short answer: you cannot.

The last expected parameter is a pointer to an object implementing the IBindStatusCallback interface that only C++ can provide. Your error reveals that the called URLDownloadToFile() tries to dereference the given pointer, and fails for sure.

As a solution you can provide a small C++ class implementing that interface, and use an instance of this class to call your C callback. However, since you don't want to use C++, I did not prepare an example.