0

好的,所以我一直在尝试做一些简单的事情,比如在 Visual C++ 中下载和传输文件,我一直在使用这个URLDownloadToFile()函数。代码编译,它没有给出任何错误,但它不会下载任何东西。显然,我并没有在我的代码不起作用的那一刻就跑过来,所以我做了一些调试等,我稍后会提到。这是代码。

#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");
    }
}

请记住,我不是专业人士,而且我在 C++ 方面的经验并不丰富。我读了几本书,但我比较年轻,我现在这样做是为了好玩。

至于调试,在调用urlfile会抛出错误,URlDownloadToFunction直到我完成.c_str()并转换它们。我在函数调用处设置了一个断点,并注意到变量 hr 的值是 0xcccccccc,直到下一个断点,在printf语句处,hr然后才具有值,E_ABORT Operation Aborted而没有其他值。字符串似乎没有问题,但它没有告诉我其他任何事情。老实说,我很难过。非常感谢任何和所有帮助。

编辑

我觉得自己像个白痴,但我试图写入我的 C:\ 驱动器的根目录,即使我忘记启用 UAC。当我启用它时,它下载并运行 Putty 就好了,尽管我正在考虑从 URLDownloadToFile() 更改为使用 WinInet。感谢所有的帮助!

4

0 回答 0