Terje Rosenlund
Terje Rosenlund

Reputation: 163

Download from Sharepoint 2010 server, open in local application on Windows client

Sharepoint 2010 server (on a https-url) and local application on a Windows client
The local application is our executable written in C++ / C#

On the site we have documents with extention that has been mapped on the client machine to open in our local application
Clicking on such a document presents a popup asking for Open or Save
Selecting Open causes the document to be downloaded and our application is started by windows with the selected file as the command-line parameter
The problem is that the command-line parameter is set to the local path for the downloaded file

Is there a way to fetch the original url?

Upvotes: 2

Views: 267

Answers (1)

Terje Rosenlund
Terje Rosenlund

Reputation: 163

Found out myself - use WinINet.lib: http://msdn.microsoft.com/en-gb/library/aa383928.aspx

Include WinINet.lib in project and WinINet.h in source

#include <wininet.h>
class CCorrelateAxCtrl::findInCache
{
public:
    _bstr_t cachedUrl;
    findInCache(_bstr_t localName)
    {
        _findInCache(localName);
    }
    virtual ~findInCache() {}

private:
    int _continue;
    DWORD _dwEntrySize;
    static const DWORD MAX_CACHE_ENTRY_INFO_SIZE = 4096;
    LPINTERNET_CACHE_ENTRY_INFO _lpCacheEntry;
    HANDLE _hCacheDir;
    _bstr_t _localName;

    void _setCachEntry(DWORD dwEntrySize)
    {
        _dwEntrySize = dwEntrySize;
        _lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[_dwEntrySize];
        _lpCacheEntry->dwStructSize = _dwEntrySize;
    }
    bool _checkFound() 
    {
        if ( _lpCacheEntry->lpszLocalFileName!=NULL && _localName.length() )
        {
            if ( !wcscmp(_lpCacheEntry->lpszLocalFileName, _localName) ) {
                cachedUrl = _lpCacheEntry->lpszSourceUrlName;
                delete[] _lpCacheEntry;
                return true;
            }
        }
        return false;
    }
    int _checkContinue(DWORD dwEntrySize) 
    {
        DWORD err = GetLastError();
        if (err)
        {
            if (err == ERROR_INSUFFICIENT_BUFFER)
            {
                delete[]_lpCacheEntry;
                _setCachEntry(dwEntrySize);
                return 2;
            }
            else
            {
                FindCloseUrlCache(_hCacheDir);
                return 0;
            }
        }
        return 1;
    }
    void _findInCache(_bstr_t &localName)
    {
        _localName = localName;
        cachedUrl = "";
        _setCachEntry(MAX_CACHE_ENTRY_INFO_SIZE);
        while(1)
        {
            _hCacheDir = FindFirstUrlCacheEntry(NULL, _lpCacheEntry, &_dwEntrySize);
            _continue = _hCacheDir ? 1 : _checkContinue(_dwEntrySize);
            if (_continue!=2)
                break;
        }
        if ( !_continue || _checkFound() ) 
            return;

        _setCachEntry(MAX_CACHE_ENTRY_INFO_SIZE);
try {
        while(1)
        {
            _continue = FindNextUrlCacheEntry(_hCacheDir, _lpCacheEntry, &_dwEntrySize);
                _continue = _continue ? 1 : _checkContinue(_dwEntrySize);
            if ( _continue==1 )
            {
                if ( _checkFound() )
                    break;
            }
            else if (_continue!=2)
                break;
        }
}
catch (char * error)
{
    _bstr_t test = error;
}



    }   
};

Upvotes: 1

Related Questions