Click here to Skip to main content
15,891,951 members
Articles / Desktop Programming / MFC

Automated IE SaveAs MHTML

Rate me:
Please Sign up or sign in to vote.
5.00/5 (29 votes)
4 Sep 20023 min read 474.5K   7.9K   95  
This article demonstrates how to automate IE's Save As functionality


//
#include "stdafx.h"

#include <stdlib.h>
#include <objbase.h>
#include <assert.h>

#include "SaveAsWebbrowser.h"
#include "SaveAsThread.h"




///////////////////////////////////////////////////////////
//
// Constructor
//
CSaveAsThread::CSaveAsThread() 
{
	m_ThreadId = 0 ;	
	m_hThread  = NULL ;
	m_hComponentReadyEvent = NULL ;
	m_WaitTime = 500 ;
} 

///////////////////////////////////////////////////////////
//
// Destructor
//
CSaveAsThread::~CSaveAsThread()
{
	// The thread must be stopped before we are deleted
	// because the WorkerFunction is in the derived class.
	StopThread();
}


// public API
//

void CSaveAsThread::SetKeyWnd(HWND hwnd) 
{ 
	m_keyhwnd = hwnd; 
}

void CSaveAsThread::Config(CString &szFilename, SaveType type)
{
	m_szFilename = szFilename;
	m_nSaveType = type;
}

CString CSaveAsThread::GetFilename()
{ 
	return m_szFilename; 
}
SaveType CSaveAsThread::GetSaveAsType() 
{ 
	return m_nSaveType; 
}



///////////////////////////////////////////////////////////
//
// StartThread
//   - Create and start the thread.
//
BOOL CSaveAsThread::StartThread() 
{

	m_bShouldStopNow=FALSE;


	if (IsThreadStarted())
	{
		return FALSE ;
	}

	// Create the thread.
	m_hThread = ::CreateThread(NULL,              // Default security
	                           0,                 // Default stack size
	                           RealThreadProc,
	                           (void*)this,
	                           CREATE_SUSPENDED,  // Create the thread suspended.
	                           &m_ThreadId) ;     // Get the Thread ID.
	if (m_hThread == NULL)
	{
		return FALSE ;
	}


	// Create an event for the thread to signal when it is finished. 
	m_hComponentReadyEvent = ::CreateEvent(NULL, FALSE, FALSE, NULL) ;
	if (m_hComponentReadyEvent == NULL)
	{
		return FALSE ;
	}

	// Initialize the wait time.
	m_WaitTime = 250; // 250ms

	// Thread was created suspended; start the thread.
	DWORD r = ResumeThread(m_hThread) ;
	assert(r != 0xffffffff) ;

	return TRUE;
}


///////////////////////////////////////////////////////////
//
// Stop Thread
//
void CSaveAsThread::StopThread()
{
	m_bShouldStopNow = TRUE;
}
	
///////////////////////////////////////////////////////////
//
// Current thread status
//
BOOL CSaveAsThread::IsThreadStarted()
{
	return (m_hThread != NULL) ;
}

///////////////////////////////////////////////////////////
//
// Thread procedure
//
DWORD WINAPI CSaveAsThread::RealThreadProc(void* pv) 
{
	CSaveAsThread* pApartment = reinterpret_cast<CSaveAsThread*>(pv) ;
	return pApartment->ClassThreadProc() ;
}

///////////////////////////////////////////////////////////
//
// Thread procedure
//
DWORD CSaveAsThread::ClassThreadProc()
{

	// Wait for the signal to create a component.
	BOOL bContinue = TRUE;
	BOOL bSignaled = FALSE;

	while ( bContinue )
	{
		switch(	::WaitForSingleObject( m_hComponentReadyEvent, m_WaitTime) )
		{
			// Update the surface becoz now it's ready
			case WAIT_OBJECT_0:
				if ( !m_bShouldStopNow )
				{
					//processPublishDirectory();
				}
				break ;
			// Process Windows messages.
			case (WAIT_OBJECT_0 + 1):
				MSG msg ;
				while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
				{
					if (msg.message == WM_QUIT)
					{
						bContinue = FALSE ;
						break ;
					}
					DispatchMessage(&msg) ;
				}
				break ;
			// Do background processing.
			case WAIT_TIMEOUT:
				if (m_bShouldStopNow)
					bContinue=FALSE;
				else
				{
					if ( ::IsWindowVisible(m_keyhwnd) )
					{
						bSignaled = TRUE;
						bContinue = FALSE;
					}
					MSG msg ;
					while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
					{
						if (msg.message == WM_QUIT)
						{
							bContinue = FALSE ;
							break ;
						}
						DispatchMessage(&msg) ;
					}

				}

				break ;

			default:
				break;
		}
	}

	if (bSignaled)
	{
		CSaveAsWebbrowser surrenderNow;
		surrenderNow.Config( GetFilename(), GetSaveAsType() );
		surrenderNow.UpdateSaveAs( m_keyhwnd );
	}


	delete this;

	return 0 ;
}







By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
France France
Addicted to reverse engineering. At work, I am developing business intelligence software in a team of smart people (independent software vendor).

Need a fast Excel generation component? Try xlsgen.

Comments and Discussions