简体   繁体   English

从 HTTP 请求触发断点下载图像

[英]Download image from HTTP request triggering a breakpoint

I am trying to download an image onto the user's desktop from a URL using Win32.我正在尝试使用 Win32 从 URL 将图像下载到用户的桌面上。 I have taken care of all the HTTP request stuff and know for a fact that it is all working well.我已经处理了所有 HTTP 请求的东西,并且知道它一切正常。 When I go to call CreateFile() the Visual Studios debugger just says "Exception: Application.exe has triggered a breakpoint" and that it will resume on the CreateFile() line.当我 go 调用CreateFile()时,Visual Studios 调试器只会说“异常:Application.exe 已触发断点”并且它将在 CreateFile() 行上恢复。 Also there is an error code "Critical error detected c0000374"还有一个错误代码“检测到严重错误c0000374”

Here is my code:这是我的代码:

VARIANT varResponse;
VariantInit(&varResponse);

...

hr = pIWinHttpRequest->get_ResponseBody(&varResponse);

...

if (SUCCEEDED(hr)) {
    
    long upperBounds;
    long lowerBounds;
    unsigned char* buff;
    //Make sure that varResponse is an array of unsigned bytes
    if (varResponse.vt == (VT_ARRAY | VT_UI1)) {
        long Dims = SafeArrayGetDim(varResponse.parray);
        
        //It should only have one dimension
        if (Dims == 1) {
            
            //Get Array lower and upper bounds
            SafeArrayGetLBound(varResponse.parray, 1, &lowerBounds);
            SafeArrayGetUBound(varResponse.parray, 1, &upperBounds);
            upperBounds++;

            SafeArrayAccessData(varResponse.parray, (void**)&buff);

            HANDLE hFile;
            DWORD dwBytesWritten;

            PWSTR filepath[MAX_PATH];
            HRESULT hr = SHGetKnownFolderPath(FOLDERID_Desktop, 0, NULL, &*filepath);
            if (SUCCEEDED(hr)) {
                
                //PathCombine(filepathForImage, filepathToDesktop, L"\\todaysDailyImage.jpg");
                
                PathAppend(*filepath, L"todaysDailyImage.jpg");
                MessageBox(NULL, *filepath, L"Check if filepath works", MB_OK);
            }
            
            hFile = CreateFile(*filepath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
            if (hFile == INVALID_HANDLE_VALUE) {
                //File failed
                
            }
            else {
                WriteFile(hFile, buff, upperBounds - lowerBounds, &dwBytesWritten, NULL);
                //File was written
            }
            CloseHandle(hFile);
            CoTaskMemFree(filepath);
            SafeArrayUnaccessData(varResponse.parray);
            MessageBox(NULL, L"Everything was cleaned up", L"Update:", MB_OK);
        }
    }
}

Am I doing anything wrong?我做错什么了吗?

The way you are using filepath is all wrong.您使用文件filepath的方式都是错误的。

You are declaring it as an array of MAX_PATH (260) number of PWSTR pointers.您将其声明为MAX_PATH (260) 个PWSTR指针的数组。

When you refer to an array by its name alone, you end up with a pointer to the 1st element of the array.当您仅通过名称引用数组时,您最终会得到指向数组第一个元素的指针。 So, &*filepath is the same as &*(&filepath[0]) , which is effectively &filepath[0] .因此, &*filepath&*(&filepath[0])相同,实际上是&filepath[0] And *filepath is the same as *(&filepath[0]) , which is effectively filepath[0] .并且*filepath*(&filepath[0])相同,实际上是filepath[0] So, as far as SHGetKnownFolderPath() and MessageBox() are concerned, they are only operating on the 1st PWSTR pointer in the array, and the other 259 array elements are ignored.所以,就SHGetKnownFolderPath()MessageBox()而言,它们只对数组中的第一个PWSTR指针进行操作,而忽略其他 259 个数组元素。 That part is ok, but wasteful.那部分还可以,但是很浪费。

However, PathAppend() requires a destination buffer that is an array of MAX_PATH number of WCHAR elements.但是, PathAppend()需要一个目标缓冲区,该缓冲区是一个包含MAX_PATHWCHAR元素的数组。 You are appending to the WCHAR[] array that SHGetKnownFolderPath() allocates as its output, which is not large enough to hold the filename you are trying to append to it.您将附加到SHGetKnownFolderPath()分配为其 output 的WCHAR[]数组,该数组不足以容纳您尝试将 append 分配给它的文件名。 So, you are triggering errors because you are trying to modify memory that hasn't been allocated to hold that modification.因此,您正在触发错误,因为您正在尝试修改尚未分配用于保存该修改的 memory。

You don't need the PWSTR array at all.您根本不需要PWSTR数组。 Try something more like this instead:尝试更多类似的东西:

PWSTR folderpath;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Desktop, 0, NULL, &folderpath);
if (FAILED(hr)) {
    // ...
}
else {
    PWSTR filepath;
    hr = PathAllocCombine(folderpath,  L"todaysDailyImage.jpg", 0, &filepath);
    if (FAIlED(hr)) {
        // ...
    }
    else {
        MessageBoxW(NULL, filepath, L"Check if filepath works", MB_OK);
            
        hFile = CreateFileW(filepath, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
        if (hFile == INVALID_HANDLE_VALUE) {
            //File failed
        }
        else {
            WriteFile(hFile, buff, upperBounds - lowerBounds, &dwBytesWritten, NULL);
            //File was written
            CloseHandle(hFile);
        }

        LocalFree(filepath);
    }

    CoTaskMemFree(folderpath);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM