Click here to Skip to main content
15,894,017 members
Articles / Desktop Programming / MFC

Creating an OLE DB Data Provider

Rate me:
Please Sign up or sign in to vote.
3.45/5 (13 votes)
12 Jan 20029 min read 128.3K   3.4K   69  
This article shows how to create an OLE DB Data Provider that wraps both a C struct and C++ class containing data that is to be made accessible by a SQL query.
// DlgProxy.cpp : implementation file
//

#include "stdafx.h"
#include "SensorInterf.h"
#include "DlgProxy.h"
#include "SensorInterfDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSensorInterfDlgAutoProxy

IMPLEMENT_DYNCREATE(CSensorInterfDlgAutoProxy, CCmdTarget)

CSensorInterfDlgAutoProxy::CSensorInterfDlgAutoProxy()
{
	EnableAutomation();
	
	// To keep the application running as long as an automation 
	//	object is active, the constructor calls AfxOleLockApp.
	AfxOleLockApp();

	// Get access to the dialog through the application's
	//  main window pointer.  Set the proxy's internal pointer
	//  to point to the dialog, and set the dialog's back pointer to
	//  this proxy.
	ASSERT (AfxGetApp()->m_pMainWnd != NULL);
	ASSERT_VALID (AfxGetApp()->m_pMainWnd);
	ASSERT_KINDOF(CSensorInterfDlg, AfxGetApp()->m_pMainWnd);
	m_pDialog = (CSensorInterfDlg*) AfxGetApp()->m_pMainWnd;
	m_pDialog->m_pAutoProxy = this;
}

CSensorInterfDlgAutoProxy::~CSensorInterfDlgAutoProxy()
{
	// To terminate the application when all objects created with
	// 	with automation, the destructor calls AfxOleUnlockApp.
	//  Among other things, this will destroy the main dialog
	if (m_pDialog != NULL)
		m_pDialog->m_pAutoProxy = NULL;
	AfxOleUnlockApp();
}

void CSensorInterfDlgAutoProxy::OnFinalRelease()
{
	// When the last reference for an automation object is released
	// OnFinalRelease is called.  The base class will automatically
	// deletes the object.  Add additional cleanup required for your
	// object before calling the base class.

	CCmdTarget::OnFinalRelease();
}

BEGIN_MESSAGE_MAP(CSensorInterfDlgAutoProxy, CCmdTarget)
	//{{AFX_MSG_MAP(CSensorInterfDlgAutoProxy)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

BEGIN_DISPATCH_MAP(CSensorInterfDlgAutoProxy, CCmdTarget)
	//{{AFX_DISPATCH_MAP(CSensorInterfDlgAutoProxy)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()

// Note: we add support for IID_ISensorInterf to support typesafe binding
//  from VBA.  This IID must match the GUID that is attached to the 
//  dispinterface in the .ODL file.

// {428B04F1-4F52-405E-A6BC-E18384454AC0}
static const IID IID_ISensorInterf =
{ 0x428b04f1, 0x4f52, 0x405e, { 0xa6, 0xbc, 0xe1, 0x83, 0x84, 0x45, 0x4a, 0xc0 } };

BEGIN_INTERFACE_MAP(CSensorInterfDlgAutoProxy, CCmdTarget)
	INTERFACE_PART(CSensorInterfDlgAutoProxy, IID_ISensorInterf, Dispatch)
END_INTERFACE_MAP()

// The IMPLEMENT_OLECREATE2 macro is defined in StdAfx.h of this project
// {166F9F2E-EA77-4CD9-923A-E97A3B2F8131}
IMPLEMENT_OLECREATE2(CSensorInterfDlgAutoProxy, "SensorInterf.Application", 0x166f9f2e, 0xea77, 0x4cd9, 0x92, 0x3a, 0xe9, 0x7a, 0x3b, 0x2f, 0x81, 0x31)

/////////////////////////////////////////////////////////////////////////////
// CSensorInterfDlgAutoProxy message handlers

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
Web Developer
United States United States
David Utz
Senior Software Engineer
Analytical Graphics, Inc.
40 General Warren Blvd.
Malvern, PA 19355
dutz@stk.com

Comments and Discussions