Click here to Skip to main content
15,867,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am trying to run UrlDownloadToFile()
CBSCallbackImplPX callback;
::URLDownloadToFileA(NULL, "http://www.codeproject.com/KB/system/keyboard.aspx" ,"c:\\test\\a.html",NULL,&callback);


Where
class CBSCallbackImplPX : public IBindStatusCallback 


It is working fine even in proxy.as it uses IE's credential.

But today when I am trying the same code under password protected proxy it is returning
E_ACCESSDENIED
.

I studied this link
http://www.ureader.com/msg/1510234.aspx[^]

and came to know about
IAuthenticate
Interface.

but not able to implement.

below is my call back code

class CBSCallbackImplPX : public IBindStatusCallback //,IServiceProvider,TIAuthenticate
{
public:
  CBSCallbackImplPX();
  ~CBSCallbackImplPX();
//TIAuthenticate  class taken from http://www.ureader.com/msg/1510234.aspx
//TIAuthenticate myAuthenticate;

 CString m_csData; 
 DWORD m_dwSize;
 int nTitmeOut;
  // IUnknown methods
  STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject);
  STDMETHOD_(ULONG, AddRef)();
  STDMETHOD_(ULONG, Release)();

  // IBindStatusCallback methods
  STDMETHOD(OnStartBinding)(DWORD, IBinding *);
  STDMETHOD(GetPriority)(LONG *);
  STDMETHOD(OnLowResource)(DWORD);
  STDMETHOD(OnProgress)(ULONG ulProgress,
    ULONG ulProgressMax,
    ULONG ulStatusCode,
    LPCWSTR szStatusText);
  STDMETHOD(OnStopBinding)(HRESULT, LPCWSTR);
  STDMETHOD(GetBindInfo)(DWORD *, BINDINFO *);
  STDMETHOD(OnDataAvailable)(DWORD, DWORD, FORMATETC *, STGMEDIUM *);
  STDMETHOD(OnObjectAvailable)(REFIID, IUnknown *);

//   HRESULT STDMETHODCALLTYPE QueryService( REFGUID guidService, REFIID riid, void __RPC_FAR *__RPC_FAR *ppvObject) ;
protected:
  ULONG m_ulObjRefCount;
  clock_t m_startDownload,m_endDownload;

};






// BSCallbackImpl.cpp: implementation of the CBSCallbackImplPX class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "BSCallbackImplPX.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//--------------------------------------------------------------------
CBSCallbackImplPX::CBSCallbackImplPX()
{
	nTitmeOut=10;
	m_ulObjRefCount = 1;
	m_csData="";
	m_dwSize=0;
	m_startDownload,m_endDownload=0;
}


//--------------------------------------------------------------------
CBSCallbackImplPX::~CBSCallbackImplPX()
{
}


//--------------------------------------------------------------------
// IUnknown
STDMETHODIMP CBSCallbackImplPX::QueryInterface(REFIID riid, void **ppvObject)
{
	
	*ppvObject = NULL;

	// IUnknown
	if (::IsEqualIID(riid, __uuidof(IUnknown)))
	{
		//TRACE(_T("IUnknown::QueryInterface(IUnknown)\n"));

		*ppvObject = this;
	}
	// IBindStatusCallback
	else if (::IsEqualIID(riid, __uuidof(IAuthenticate)))
	{
		//TRACE(_T("IUnknown::QueryInterface(IBindStatusCallback)\n"));

		AfxMessageBox("found something");//can I implement the callback here??
		//*ppvObject = /*static_cast(*/this/*)*/;
	}

		if (::IsEqualIID(riid, __uuidof(IUnknown)))
		{}
	if (*ppvObject)
	{
		this->AddRef();
		/*(*reinterpret_cast(ppvObject))->AddRef();*/  //Ram:As unicode casting.

		return S_OK;
	}

	return E_NOINTERFACE;
}





//--------------------------------------------------------------------
STDMETHODIMP_(ULONG) CBSCallbackImplPX::AddRef()
{
	//TRACE(_T("IUnknown::AddRef\n"));

	return ++m_ulObjRefCount;
}


//--------------------------------------------------------------------
STDMETHODIMP_(ULONG) CBSCallbackImplPX::Release()
{
	//TRACE(_T("IUnknown::Release\n"));

	return --m_ulObjRefCount;
}


//--------------------------------------------------------------------
// IBindStatusCallback
STDMETHODIMP CBSCallbackImplPX::OnStartBinding(DWORD d, IBinding *ib)
{
	//ib=&myAuthenticate;
	//TRACE(_T("IBindStatusCallback::OnStartBinding\n"));
	m_startDownload=clock();
	return S_OK;
}


//--------------------------------------------------------------------
STDMETHODIMP CBSCallbackImplPX::GetPriority(LONG *)
{
	//TRACE(_T("IBindStatusCallback::GetPriority\n"));

	return E_NOTIMPL;
}


//--------------------------------------------------------------------
STDMETHODIMP CBSCallbackImplPX::OnLowResource(DWORD)
{
	//TRACE(_T("IBindStatusCallback::OnLowResource\n"));

	return S_OK;
}


//--------------------------------------------------------------------
STDMETHODIMP CBSCallbackImplPX::OnProgress(ULONG ulProgress,
										   ULONG ulProgressMax,
										   ULONG ulStatusCode,
										   LPCWSTR szStatusText)
{
	m_endDownload=clock();
	int nTimeTakenForThisDownload=0;
	nTimeTakenForThisDownload=((int)( m_endDownload - m_startDownload)/CLOCKS_PER_SEC) ;
	if(nTimeTakenForThisDownload>=nTitmeOut)
	{
		////CMTLog::WriteLogMT("Download cancelled by client");
		return E_ABORT;  // Download canceled by the client
	}
	return S_OK;
}


//--------------------------------------------------------------------
STDMETHODIMP CBSCallbackImplPX::OnStopBinding(HRESULT, LPCWSTR)
{
	//TRACE(_T("IBindStatusCallback::OnStopBinding\n"));

	return S_OK;
}


//--------------------------------------------------------------------
STDMETHODIMP CBSCallbackImplPX::GetBindInfo(DWORD *d, BINDINFO * b)
{
	//b->pUnk = &myAuthenticate;
	//TRACE(_T("IBindStatusCallback::GetBindInfo\n"));
	return S_OK;
}


//--------------------------------------------------------------------
STDMETHODIMP CBSCallbackImplPX::OnDataAvailable(DWORD dww, DWORD dwSize,
												FORMATETC *fww, STGMEDIUM *stgmed )
{
	//TRACE(_T("IBindStatusCallback::OnDataAvailable %ld\n"), dwSize );
//	//CMTLog::WriteLogMT2("IBindStatusCallback::OnDataAvailable %ld ",dwSize);
	if ( stgmed ) {
		if ( stgmed->pstm ) {

			ULONG bytesRead = 0;

			dwSize=dwSize-m_dwSize;

			BYTE *data = new BYTE[ dwSize + 1 ];
			//*data=(char)NULL;
			HRESULT hRes = stgmed->pstm->Read( data, dwSize, &bytesRead );
			data[dwSize] = '\0';

			m_dwSize+=bytesRead;
			m_csData +=(char *)data;

			/*
			BYTE *data = (BYTE*)m_csData.GetBuffer( m_csData.GetLength() + dwSize + 1 );
			HRESULT hRes = stgmed->pstm->Read( data, dwSize, &bytesRead );
			m_dwSize+=bytesRead;
			m_csData.ReleaseBuffer();
			*/

			delete data;
			data=NULL;

			return hRes;
		}
	}

	return S_OK;
}


//--------------------------------------------------------------------
STDMETHODIMP CBSCallbackImplPX::OnObjectAvailable(REFIID, IUnknown *)
{
	//TRACE(_T("IBindStatusCallback::OnObjectAvailable\n"));

	return S_OK;
}


//--------------------------------------------------------------------


please help :(
Posted
Comments
shankha2010 13-Dec-11 5:44am    
after studying this link
http://msdn.microsoft.com/en-us/library/ms775123(v=vs.85).aspx

I think ::URLDownloadToFileA(/*this is the place*/, "a.com","c:\a.html",NULL,&callback);

where I can implement
TIAuthenticate class of http://www.ureader.com/msg/1510234.aspx
and now I am getting the control but

HRESULT __stdcall TIAuthenticate::QueryInterface(const IID& iid,void ** ppv)
{
//

if(iid == IID_IUnknown)
{
*ppv = static_cast<iauthenticate*>(this);
}
else if(iid == IID_IAuthenticate)
{
*ppv = static_cast<iauthenticate*>(this);
AfxMessageBox("Authenticate interface requested.");
}
else
{
*ppv = NULL;
return E_NOINTERFACE;//it is coming here :(
}
reinterpret_cast<iunknown*>(*ppv)->AddRef();
return S_OK;

}

1 solution

ultimately got one way out

URLDownloadToFile(NULL,"http://prodex:prodex123@www.codeproject.com/KB/system/keyboard.aspx","c:\\test\\a.html",NULL,NULL);

its working :)

still not able to do through callback.

please send some code snippet for that.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900