Click here to Skip to main content
15,883,883 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: RegQueryValueExW() Pin
sashoalm21-Mar-09 0:34
sashoalm21-Mar-09 0:34 
QuestionThreading Pin
MsmVc20-Mar-09 20:51
MsmVc20-Mar-09 20:51 
AnswerRe: Threading Pin
Rajesh R Subramanian20-Mar-09 23:37
professionalRajesh R Subramanian20-Mar-09 23:37 
GeneralRe: Threading Pin
MsmVc21-Mar-09 2:50
MsmVc21-Mar-09 2:50 
GeneralRe: Threading Pin
Rajesh R Subramanian21-Mar-09 4:10
professionalRajesh R Subramanian21-Mar-09 4:10 
QuestionURLDownloadtoFile() Pin
p_196020-Mar-09 20:35
p_196020-Mar-09 20:35 
AnswerRe: URLDownloadtoFile() Pin
sashoalm20-Mar-09 21:26
sashoalm20-Mar-09 21:26 
Generalclass DownloadUrl Pin
sashoalm20-Mar-09 21:27
sashoalm20-Mar-09 21:27 
// helper for downloading data from http
class DownloadUrl
{
public:
	// constructor, requires the url of the resource to download
	DownloadUrl(const string& url);

	~DownloadUrl();

	// get (i.e. download) some content (into memory)
	int Get(void* buffer, int count);

	// how much of the content is already downloaded (as percentage)
	int PercentageRead();

private:
	// for PercentageRead
	DWORD m_Length;
	int m_BytesReadTotal;

	// managed pointers
	typedef auto_ptr<CInternetSession> apCInternetSession;
	typedef auto_ptr<CHttpConnection> apCHttpConnection;
	typedef auto_ptr<CHttpFile> apCHttpFile;

	// the resources will be automatically released (because they are managed pointers)
	apCInternetSession m_pInternetSession;
	apCHttpConnection m_pServer;
	apCHttpFile m_pFile;
};



DownloadUrl::DownloadUrl( const string& url )
{
	// use AfxParseURL to separate server from object
	CString server, object;
	DWORD service_type;
	INTERNET_PORT internet_port;
	AfxParseURL(url.c_str(), service_type, server, object, internet_port);
	if (server.IsEmpty() || object.IsEmpty())
		throw exception("Could not parse URL");

	// open an internet session
	m_pInternetSession = apCInternetSession(new CInternetSession("Microsoft Internet Explorer 5.0"));
	m_pInternetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT, 15000);
	m_pInternetSession->SetOption(INTERNET_OPTION_CONNECT_BACKOFF, 1000);
	m_pInternetSession->SetOption(INTERNET_OPTION_CONNECT_RETRIES, 2);
	m_pInternetSession->EnableStatusCallback(TRUE);

	// open a new connection to server
	m_pServer = apCHttpConnection(m_pInternetSession->GetHttpConnection(server, internet_port));
	if (m_pServer.get() == NULL)
		throw exception("HTTP connection failed");

	// make a GET request	
	m_pFile = apCHttpFile(m_pServer->OpenRequest("GET", object, NULL));
	if (m_pFile.get() == NULL)
		throw exception("Request failed");
	m_pFile->AddRequestHeaders("Accept: audio/x-aiff, audio/basic, audio/midi, audio/mpeg, audio/wav, image/jpeg, image/gif, image/jpg, image/png, image/mng, image/bmp, text/plain, text/html, text/htm\r\n");
	m_pFile->AddRequestHeaders("User-Agent: GetWebFile/1.0\r\n", HTTP_ADDREQ_FLAG_ADD_IF_NEW);
	m_pFile->SendRequest();

	// check that everything was OK
	DWORD dwStatus;	
	m_pFile->QueryInfoStatusCode(dwStatus);
	if (dwStatus != HTTP_STATUS_OK)
		throw exception("URL was denied by server");

	// query the length of the file we're getting
	m_Length = 0;
	m_pFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, m_Length);

	m_BytesReadTotal = 0;
}

DownloadUrl::~DownloadUrl()
{

}

int DownloadUrl::Get( void* buffer, int count )
{
	UINT bytes_read = m_pFile->Read(buffer, count);
	m_BytesReadTotal += bytes_read;
	return bytes_read;
}

int DownloadUrl::PercentageRead()
{
	if (!m_Length)
		return 0;

	// what percentage of content has been downloaded?
	double ratio = m_BytesReadTotal;
	ratio /= m_Length;
	ratio *= 100;

	return (int) ratio;
}


There is sufficient light for those who desire to see, and there is sufficient darkness for those of a contrary disposition.
Blaise Pascal

GeneralHow to use Pin
sashoalm20-Mar-09 21:35
sashoalm20-Mar-09 21:35 
GeneralRe: How to use Pin
p_196020-Mar-09 21:39
p_196020-Mar-09 21:39 
GeneralRe: How to use Pin
sashoalm20-Mar-09 21:46
sashoalm20-Mar-09 21:46 
GeneralRe: How to use Pin
p_196020-Mar-09 22:19
p_196020-Mar-09 22:19 
GeneralRe: How to use Pin
sashoalm20-Mar-09 22:22
sashoalm20-Mar-09 22:22 
GeneralRe: How to use Pin
p_196020-Mar-09 22:35
p_196020-Mar-09 22:35 
GeneralRe: How to use Pin
sashoalm20-Mar-09 22:39
sashoalm20-Mar-09 22:39 
QuestionProblem in compling through vc++ 2005 Pin
raj157620-Mar-09 19:16
raj157620-Mar-09 19:16 
AnswerRe: Problem in compling through vc++ 2005 Pin
Stuart Dootson21-Mar-09 0:37
professionalStuart Dootson21-Mar-09 0:37 
Questionconverting hex to image Pin
annese20-Mar-09 18:59
annese20-Mar-09 18:59 
GeneralRe: converting hex to image Pin
norish20-Mar-09 20:33
norish20-Mar-09 20:33 
GeneralRe: converting hex to image Pin
annese20-Mar-09 20:47
annese20-Mar-09 20:47 
GeneralRe: converting hex to image Pin
CPallini21-Mar-09 1:06
mveCPallini21-Mar-09 1:06 
GeneralRe: converting hex to image Pin
norish21-Mar-09 1:19
norish21-Mar-09 1:19 
GeneralRe: converting hex to image Pin
annese21-Mar-09 3:42
annese21-Mar-09 3:42 
GeneralRe: converting hex to image Pin
annese21-Mar-09 6:28
annese21-Mar-09 6:28 
QuestionCPU Time Pin
Max++20-Mar-09 18:33
Max++20-Mar-09 18:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.