Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C#

Lingering COM Objects Caused by ActiveX Control Event Handlers

Rate me:
Please Sign up or sign in to vote.
4.67/5 (6 votes)
14 Mar 20068 min read 45.8K   501   22  
This article describes how, under some circumstances, an ActiveX control event handler can cause COM objects to linger.
// DataInfo.cpp : Implementation of CDataInfo

#include "stdafx.h"
#include "DataInfo.h"

// CDataInfo

// internal function to flush the data and close up the file 
void CDataInfo::Close(void)
{
	// update and close the file if open 
	if (m_hFile)
	{
		// write out our current data 
		DWORD written = 0; 
		WriteFile(m_hFile, m_bstrData.GetBSTR(), sizeof(wchar_t) * m_bstrData.length(), &written, NULL); 

		// close the file 
		::CloseHandle(m_hFile); 
	}
	m_hFile = NULL; 
}

// initialize to specify the output file 
STDMETHODIMP CDataInfo::Initialize(BSTR bstrFileName)
{
	// if already open, return error 
	if (m_hFile != NULL)
		return(E_FAIL); 

	// attempt to open the file for reading and writing (clear any existing contents for simplicity)
	m_hFile = ::CreateFile(bstrFileName, FILE_READ_DATA | FILE_WRITE_DATA, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 
	if (m_hFile == INVALID_HANDLE_VALUE) 
	{ 
		m_hFile = NULL; 
		return(HRESULT_FROM_WIN32(GetLastError())); 
	}

	// ideally we would refresh our internal buffer with the previous contents of the file here, 
	// but for simplicity, start with an empty buffer...

	// success
	return S_OK;
}

// update our datasource with new data 
STDMETHODIMP CDataInfo::put_DataContents(BSTR newVal)
{
	HRESULT hRes = S_OK; 

	// if they didn't initialize, then return error 
	if (!m_hFile) 
		return(E_FAIL); 
	
	// copy the input data 
	try
	{
		m_bstrData = newVal; 
	}
	catch (_com_error &e)
	{
		hRes = e.Error(); 
	}

	// status
	return(hRes);
}

// get the current contents of the datasource
STDMETHODIMP CDataInfo::get_DataContents(BSTR* pVal)
{
	HRESULT hRes = S_OK; 

	// if they didn't initialize, then return error 
	if (!m_hFile) 
		return(E_FAIL); 

	// return a copy of the current data 
	try
	{
		*pVal = m_bstrData.copy(); 
	}
	catch (_com_error &e)
	{
		hRes = e.Error(); 
	}

	// status
	return(hRes);
}

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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions