1

I tried to print out the information given by the function called "GetUrlCacheEntryInfo" but it's not working, it's printing me out only a character in the debug - log. How can i print all the data correctly?

GDaniel
  • 11
  • 4

1 Answers1

0

Here is a working sample, with an url (https://h6.msn.com/library/8.8/dapmsn.js) that I found in my cache (since I don't use IE this was the first I got from FindFirstUrlCacheEntry. The possbile issue why you could be printing only one character is that maybe you are not using wide string print functions. The lpszSourceUrlName and lpszLocalFileName fields are wide string fields so you should print them accordingly (see usage of std::cout and std::wcout in the sample below).

#include <Windows.h>
#include <Wininet.h>

#include <memory>

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

#include <iostream>

bool GetUrlCacheEntryInfo(const wchar_t* pUrl, std::unique_ptr<unsigned char[]>& pUrlCacheEntryInfo, DWORD nBufferSize = sizeof(INTERNET_CACHE_ENTRY_INFO))
{
    std::unique_ptr<unsigned char[]> pTemporaryUrlCacheEntryInfo(new unsigned char[nBufferSize]);
    if (GetUrlCacheEntryInfo(pUrl, reinterpret_cast<LPINTERNET_CACHE_ENTRY_INFO>(pTemporaryUrlCacheEntryInfo.get()), &nBufferSize) == FALSE)
    {
        const DWORD nError = GetLastError();
        if (nError == ERROR_INSUFFICIENT_BUFFER)
        {
            pTemporaryUrlCacheEntryInfo.reset(new unsigned char[nBufferSize]);
            if (GetUrlCacheEntryInfo(pUrl, reinterpret_cast<LPINTERNET_CACHE_ENTRY_INFO>(pTemporaryUrlCacheEntryInfo.get()), &nBufferSize) == FALSE)
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    pUrlCacheEntryInfo = std::move(pTemporaryUrlCacheEntryInfo);
    return true;
}

void PrintUrlCacheEntryInfo(LPINTERNET_CACHE_ENTRY_INFO pUrlCacheEntryInfo)
{
    std::cout << "Cache Entry Info:\n\tSize: " << pUrlCacheEntryInfo->dwStructSize << "\n\tUrl: ";
    std::wcout << pUrlCacheEntryInfo->lpszSourceUrlName;
    std::cout << "\n\tLocal File:";
    std::wcout << pUrlCacheEntryInfo->lpszLocalFileName;
    std::cout << "\n\tType: ";
    switch (pUrlCacheEntryInfo->CacheEntryType)
    {
    case EDITED_CACHE_ENTRY:
        std::cout << "EDITED_CACHE_ENTRY";
        break;
    case SPARSE_CACHE_ENTRY:
        std::cout << "SPARSE_CACHE_ENTRY";
        break;
    case STICKY_CACHE_ENTRY:
        std::cout << "STICKY_CACHE_ENTRY";
        break;
    case TRACK_OFFLINE_CACHE_ENTRY:
        std::cout << "TRACK_OFFLINE_CACHE_ENTRY";
        break;
    case TRACK_ONLINE_CACHE_ENTRY:
        std::cout << "TRACK_ONLINE_CACHE_ENTRY";
        break;
    default:
        std::cout << "UNKNOWN";
        break;
    }
    std::cout << "\n\tHeaders: ";
    std::wcout << pUrlCacheEntryInfo->lpHeaderInfo;
    std::cout << "\n";
}

int main(int argc, char** argv)
{
    const wchar_t* pUrl = L"https://h6.msn.com/library/8.8/dapmsn.js";
    std::unique_ptr<unsigned char[]> pUrlCacheEntryInfo;
    if (GetUrlCacheEntryInfo(pUrl, pUrlCacheEntryInfo))
    {
        PrintUrlCacheEntryInfo(reinterpret_cast<LPINTERNET_CACHE_ENTRY_INFO>(pUrlCacheEntryInfo.get()));
    }
    else
    {
        std::wcout << L"Cache entry for url " << pUrl << L" was  not found!\n";
    }
    return EXIT_SUCCESS;
}
Rudolfs Bundulis
  • 11,636
  • 6
  • 33
  • 71
  • Ok, do you know why URLDownloadToFile is downloading the files in appdata (iecache) instead of downloading them directly to the specified path? – GDaniel Oct 31 '16 at 08:16
  • I asked about the geturlcacheentryinfo because i wanted to find out the locaton of the downloaded file and then move it – GDaniel Oct 31 '16 at 08:18
  • @GDaniel no idea, and since there is an explicit URLDownloadToCacheFile function I really dont know why URLDownloadToFile should put the file in cache. What is the return code? Are you providing the callback interface as the last argument? – Rudolfs Bundulis Oct 31 '16 at 08:38
  • Currently i'm not home (at my pc) but when i'll be i'll debug it again and print the result here for you. – GDaniel Oct 31 '16 at 09:03
  • Done, look here http://pastebin.com/9As4Pew5. The problem is that the downloaded file isn't going to the correct path, even though i specified the path in the URLDownloadToFile func. (so the file is downloaded successfuly but it's going to ..\AppData\Local\Microsoft\Windows\INetCache\IE\). I hope you'll understand, if not please tell me, thanks! – GDaniel Oct 31 '16 at 14:23