0

Okay, so I have been trying to do some simple stuff like downloading and transferring files in Visual C++, and I had been using the URLDownloadToFile() function. The code compiled, and it didn't give any errors, however it would not download anything. Obviously, I didn't just run over here the minute my code doesn't work, so I did some debugging and such, which I will mention in a minute. Here's the code.

#pragma comment(lib, "urlmon.lib")

#include <Windows.h>
#include <urlmon.h>
#include <stdio.h>
#include <tchar.h>
#include <string>

using namespace std;

void dwfile();

int main() {
    dwfile();
    return 0;
}

int dwfile() {
    string url("http://0.0.0.0/putty.exe"); // I changed the IP for privacy 
    string file("C:\\Putty");

    const char *strUrl = url.c_str(); 
    const char *strFile = file.c_str(); // I had been testing with these, not currently using them

    HRESULT hr = URLDownloadToFile(NULL, url.c_str(), file.c_str(), 0, NULL);

    if (FAILED(hr)) {
        printf("failed\n");
        OutputDebugString("\n");
        OutputDebugString("FAILED\n");
        OutputDebugString("\n");
        GetLastError();
        system("pause");
    }
}

Keep in mind that I am not a professional, and that I am not incredibly experienced in C++. I read a few books, but I am on the younger side, and I am currently doing this for fun.

As for debugging, url and file would throw errors when called in the URlDownloadToFunction until I did .c_str() and converted them. I set a break point at the function call, and noticed that the variable hr had the value of 0xcccccccc, until the next break point, at the printf statement, when hr then had the value of E_ABORT Operation Aborted and nothing else. There didn't seem to be an issue with the strings, but it didn't tell me anything else. I'm honestly just stumped. Any and all help is appreciated greatly.

EDIT

I feel like an idiot, but I was trying to write to the root of my C:\ drive, even though I had forgot to enable UAC. When I enabled it, it downloaded and ran Putty just fine, although I'm considering changing from URLDownloadToFile() to using WinInet. Thanks for all the help!

  • 3
    Unless you've turned off UAC or are running your program elevated, it won't be able to write to files in the root of C: as you're trying to do. – Jonathan Potter Feb 09 '17 at 23:53
  • 1
    `URLDownloadToFile()` has a lot of issues, it has a bad tendency to not work well, and its error reporting (or lack of) sucks. You are better off using the WinInet API instead (`InternetOpen()`, `InternetConnect()`, `HttpOpenRequest()`, `HttpSendRequest()`, `HttpQueryInfo()`, `InternetReadFile()`). See [HTTP Sessions](https://msdn.microsoft.com/en-us/library/windows/desktop/aa384322.aspx) for details. – Remy Lebeau Feb 10 '17 at 00:53
  • 1
    Also, your call to `GetLastError()` is useless, and you should include the actual value of `hr` in your error messages. – Remy Lebeau Feb 10 '17 at 00:54
  • @JonathanPotter I am such an idiot. I am so stupid, I don't even... The minute I changed it to write to my Desktop, it worked. Thanks. – LifeInKernelSpace Feb 10 '17 at 00:59
  • @RemyLebeau I will definitely consider looking into it, but for now, it works because I fixed it. The issue was writing to the root of my C: drive. – LifeInKernelSpace Feb 10 '17 at 00:59
  • @LifeInKernelSpace, if this issue is resolved, you could share the solution as the answer, and mark it. So it could help other community members who meet the same issue. Have a nice day:) – Jack Zhai Feb 10 '17 at 01:53

0 Answers0