简体   繁体   中英

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. I have taken care of all the HTTP request stuff and know for a fact that it is all working well. 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. Also there is an error code "Critical error detected 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.

You are declaring it as an array of MAX_PATH (260) number of PWSTR pointers.

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] . And *filepath is the same as *(&filepath[0]) , which is effectively 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. That part is ok, but wasteful.

However, PathAppend() requires a destination buffer that is an array of MAX_PATH number of WCHAR elements. 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. So, you are triggering errors because you are trying to modify memory that hasn't been allocated to hold that modification.

You don't need the PWSTR array at all. 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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