Click here to Skip to main content
15,885,914 members

vc++ UrlDownloadToFile not working in password proteted proxy

shankha2010 asked:

Open original thread
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 :(
Tags: C++, C, Visual C++ 9.0, ATL, COM

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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