Click here to Skip to main content
15,884,473 members
Articles / Programming Languages / Python

Using COM Objects in Scripting Languages -- Part 2 (Python)

Rate me:
Please Sign up or sign in to vote.
4.73/5 (7 votes)
20 Apr 2010CPOL6 min read 86.6K   642   28  
This article shows how to instantiate a COM object in Python and use its methods and properties.
// GraphicPoint.h : Declaration of the CGraphicPoint

#pragma once
#include "resource.h"       // main symbols

#include "SimpleCOM_i.h"

#if defined(_WIN32_WCE) && !defined(_CE_DCOM) && !defined(_CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA)
#error "Single-threaded COM objects are not properly supported on Windows CE platform, such as the Windows Mobile platforms that do not include full DCOM support. Define _CE_ALLOW_SINGLE_THREADED_OBJECTS_IN_MTA to force ATL to support creating single-thread COM object's and allow use of it's single-threaded COM object implementations. The threading model in your rgs file was set to 'Free' as that is the only threading model supported in non DCOM Windows CE platforms."
#endif

#include <cmath>

// CGraphicPoint

class ATL_NO_VTABLE CGraphicPoint :
	public CComObjectRootEx<CComSingleThreadModel>,
	public CComCoClass<CGraphicPoint, &CLSID_GraphicPoint>,
	public IDispatchImpl<IGraphicPoint, &IID_IGraphicPoint, &LIBID_SimpleCOMLib, /*wMajor =*/ 1, /*wMinor =*/ 0>,
	public IDispatchImpl<IColor, &__uuidof(IColor), &LIBID_SimpleCOMLib, /* wMajor = */ 1, /* wMinor = */ 0>,
	public IDispatchImpl<IPoint, &__uuidof(IPoint), &LIBID_SimpleCOMLib, /* wMajor = */ 1, /* wMinor = */ 0>
{
public:
	CGraphicPoint() :
	  m_dX(0)
		  ,m_dY(0)
		  ,m_dZ(0)
	{
	}

	DECLARE_REGISTRY_RESOURCEID(IDR_GRAPHICPOINT)


	BEGIN_COM_MAP(CGraphicPoint)
		COM_INTERFACE_ENTRY(IGraphicPoint)
		COM_INTERFACE_ENTRY2(IDispatch, IColor)
		COM_INTERFACE_ENTRY(IColor)
		COM_INTERFACE_ENTRY(IPoint)
	END_COM_MAP()



	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}

	void FinalRelease()
	{
	}

private:
	double m_dX;
	double m_dY;
	double m_dZ;
	OLE_COLOR m_Color;

public:

	STDMETHOD(Draw)(void);

	// IColor Methods
public:
	STDMETHOD(SetColor)( OLE_COLOR iColor )
	{
		m_Color = iColor;
		return S_OK;
	}
	STDMETHOD(GetColor)( OLE_COLOR* oColor )
	{
		*oColor = m_Color;
		return S_OK;
	}

	// IPoint Methods
public:
	STDMETHOD(get_X)( DOUBLE * pVal)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		*pVal = m_dX;
		return S_OK;
	}
	STDMETHOD(put_X)( DOUBLE newVal)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		m_dX = newVal;
		return S_OK;
	}
	STDMETHOD(get_Y)( DOUBLE * pVal)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		*pVal = m_dY;
		return S_OK;
	}
	STDMETHOD(put_Y)( DOUBLE newVal)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		m_dY = newVal;
		return S_OK;
	}
	STDMETHOD(get_Z)( DOUBLE * pVal)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		*pVal = m_dZ;
		return S_OK;
	}
	STDMETHOD(put_Z)( DOUBLE newVal)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		m_dZ = newVal;
		return S_OK;
	}
	STDMETHOD(Distance)( IPoint * iPoint,  DOUBLE * oDist)
	{
		AFX_MANAGE_STATE(AfxGetStaticModuleState());
		double x, y, z;
		iPoint->GetCoord(&x, &y, &z);
		*oDist  = (DOUBLE)sqrt(pow(m_dX - x, 2) + pow(m_dY - y, 2) + pow(m_dZ - z, 2));
		return S_OK;
	}
	STDMETHOD(SetCoord)(DOUBLE iX, DOUBLE iY, DOUBLE iZ);
	STDMETHOD(GetCoord)(DOUBLE* oX, DOUBLE* oY, DOUBLE* oZ);
};

OBJECT_ENTRY_AUTO(__uuidof(GraphicPoint), CGraphicPoint)

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Product Manager Mahindra & Mahindra
India India
Sharjith is a Mechanical Engineer with strong passion for Automobiles, Aircrafts and Software development.

Comments and Discussions