Home   Index   About
Ultimate Pack


Custom Search
POINTS.CPP (LINES OLE Sample)

/*************************************************************************

  • OLE Automation Points object

  • points.cpp

  • CPoints collection implementation

  • Written by Microsoft Product Support Services, Windows Developer Support

  • (c) Copyright Microsoft Corp. 1993 All Rights Reserved

  • ***********************************************************************/

#include <windows.h>

#include <windowsx.h>

#ifdef WIN16

#include <ole2.h>

#include <compobj.h>

#include <dispatch.h>

#include <variant.h>

#include <olenls.h>

#endif

#include "lines.h"

/*

* CPoints::Create

*

* Purpose:

* Creates an instance of a collection object and initializes it.

*

* Parameters:

* lMaxSize Maximum number of items that can added to collection.

* lLBound Lower bound of index of collection.

* pPane Pointer to pane that contains this collection. Required because the pane coordinates

* the lines and points collection.

* ppPoints Returns collection object.

*

* Return Value:

* HRESULT

*

*/

HRESULT

CPoints::Create(ULONG lMaxSize, long lLBound, CPane FAR* pPane, CPoints FAR* FAR* ppPoints)

{

HRESULT hr;

CPoints FAR* pPoints = NULL;

SAFEARRAYBOUND sabound[1];

*ppPoints = NULL;

// Create new collection

pPoints = new CPoints();

if (pPoints == NULL)

goto error;

pPoints->m_cMax = lMaxSize;

pPoints->m_cElements = 0;

pPoints->m_lLBound = lLBound;

pPoints->m_pPane = pPane;

// Load type information for the points collection from type library.

hr = LoadTypeInfo(&pPoints->m_ptinfo, IID_IPoints);

if (FAILED(hr))

goto error;

// Create a safe array which is used to implement the collection.

sabound[0].cElements = lMaxSize;

sabound[0].lLbound = lLBound;

pPoints->m_psa = SafeArrayCreate(VT_VARIANT, 1, sabound);

if (pPoints->m_psa == NULL)

{

hr = ResultFromScode(E_OUTOFMEMORY);

goto error;

}

*ppPoints = pPoints;

return NOERROR;

error:

if (pPoints == NULL)

return ResultFromScode(E_OUTOFMEMORY);

if (pPoints->m_ptinfo)

pPoints->m_ptinfo->Release();

if (pPoints->m_psa)

SafeArrayDestroy(pPoints->m_psa);

pPoints->m_psa = NULL;

pPoints->m_ptinfo = NULL;

delete pPoints;

return hr;

}

/*

* CPoints::CPoints

*

* Purpose:

* Constructor for CPoints object. Initializes members to NULL.

*

*/

#pragma warning (disable : 4355)

CPoints::CPoints() : m_SupportErrorInfo(this, IID_IPoints)

#pragma warning (default : 4355)

{

m_cRef = 0;

m_psa = NULL;

m_ptinfo = NULL;

}

/*

* CPoints::~CPoints

*

* Purpose:

* Destructor for CPoints object.

*

*/

CPoints::~CPoints()

{

if (m_ptinfo) m_ptinfo->Release();

if (m_psa) SafeArrayDestroy(m_psa);

}

/*

* CPoints::QueryInterface, AddRef, Release

*

* Purpose:

* Implements IUnknown::QueryInterface, AddRef, Release

*

*/

STDMETHODIMP

CPoints::QueryInterface(REFIID iid, void FAR* FAR* ppv)

{

*ppv = NULL;

if (iid == IID_IUnknown || iid == IID_IPoints || iid == IID_IDispatch)

*ppv = this;

else if (iid == IID_ISupportErrorInfo)

*ppv = &m_SupportErrorInfo;

else return ResultFromScode(E_NOINTERFACE);

AddRef();

return NOERROR;

}

STDMETHODIMP_(ULONG)

CPoints::AddRef(void)

{

#ifdef _DEBUG

TCHAR ach[50];

wsprintf(ach, TEXT("Ref = %ld, Points\r\n"), m_cRef+1);

OutputDebugString(ach);

#endif

return ++m_cRef;

}

STDMETHODIMP_(ULONG)

CPoints::Release(void)

{

#ifdef _DEBUG

TCHAR ach[50];

wsprintf(ach, TEXT("Ref = %ld, Points\r\n"), m_cRef-1);

OutputDebugString(ach);

#endif

if(--m_cRef == 0)

{

delete this;

return 0;

}

return m_cRef;

}

/*

* CPoints::GetTypeInfoCount

*

* Purpose:

* Implements IDispatch::GetTypeInfoCount.

*

*/

STDMETHODIMP

CPoints::GetTypeInfoCount(UINT FAR* pctinfo)

{

*pctinfo = 1;

return NOERROR;

}

/*

* CPoints::GetTypeInfo

*

* Purpose:

* Implements IDispatch::GetTypeInfo.

*

*/

STDMETHODIMP

CPoints::GetTypeInfo(

UINT itinfo,

LCID lcid,

ITypeInfo FAR* FAR* pptinfo)

{

*pptinfo = NULL;

if(itinfo != 0)

return ResultFromScode(DISP_E_BADINDEX);

m_ptinfo->AddRef();

*pptinfo = m_ptinfo;

return NOERROR;

}

/*

* CPoints::GetIDsOfNames

*

* Purpose:

* Implements IDispatch::GetIDsOfNames. The standard implementation, DispGetIDsOfNames,

* is used.

*

*/

STDMETHODIMP

CPoints::GetIDsOfNames(

REFIID riid,

OLECHAR FAR* FAR* rgszNames,

UINT cNames,

LCID lcid,

DISPID FAR* rgdispid)

{

return DispGetIDsOfNames(m_ptinfo, rgszNames, cNames, rgdispid);

}

/*

* CPoints::Invoke

*

* Purpose:

* Implements IDispatch::Invoke. The standard implementation, DispInvoke,

* is used. Properties and methods exposed by the points object will

* set m_bRaiseException to raise an exception.

*

*/

STDMETHODIMP

CPoints::Invoke(

DISPID dispidMember,

REFIID riid,

LCID lcid,

WORD wFlags,

DISPPARAMS FAR* pdispparams,

VARIANT FAR* pvarResult,

EXCEPINFO FAR* pexcepinfo,

UINT FAR* puArgErr)

{

HRESULT hr;

m_bRaiseException = FALSE;

hr = DispInvoke(

this, m_ptinfo,

dispidMember, wFlags, pdispparams,

pvarResult, pexcepinfo, puArgErr);

if (m_bRaiseException)

{

if (NULL != pexcepinfo)

_fmemcpy(pexcepinfo, &m_excepinfo, sizeof(EXCEPINFO));

return ResultFromScode(DISP_E_EXCEPTION);

}

else return hr;

}

/*

* CPoints::get_Count

*

* Purpose:

* Returns number of items in collection.

*

*/

STDMETHODIMP

CPoints::get_Count(long FAR* plCount)

{

*plCount = m_cElements;

return NOERROR;

}

/*

* CPoints::get_Item

*

* Purpose:

* Retrieves item from collection, given an index.

*

* Parameters:

* lIndex Index of item to be retrieved.

*

* Returns

* IDispatch of item retrieved from collection.

*

*/

STDMETHODIMP

CPoints::get_Item(long lIndex, IPoint FAR* FAR* ppPoint)

{

HRESULT hr;

VARIANT v;

LPDISPATCH pdisp = NULL;

// Check if integer index is within range

if (lIndex < m_lLBound || lIndex >= (long)(m_lLBound+m_cElements))

return RaiseException(IDS_InvalidIndex);

// Retrieve and return item. Note that SafeArrayGetElement AddRefs so an additional AddRef

// is not required.

VariantInit(&v);

hr = SafeArrayGetElement(m_psa, &lIndex, &v);

if (FAILED(hr))

return RaiseException(IDS_Unexpected);

*ppPoint = (IPoint FAR*) V_DISPATCH(&v);

return NOERROR;

}

/*

* CPoints::get_NewEnum

*

* Purpose:

* Returns an enumerator (IEnumVARIANT) for the items curently in the collection.

* The NewEnum property is restricted and so is invisible to users of an

* automation controller's scripting language. Automation controllers that support

* a 'For Each' statement to iterate through the elements of a collection will use

* the enumerator returned by NewEnum. The enumerator creates a snapshot of the

* the current state of the collection.

*

*/

STDMETHODIMP

CPoints::get__NewEnum(IUnknown FAR* FAR* ppunkEnum)

{

CEnumVariant FAR* penum = NULL;

HRESULT hr;

// Create new enumerator for items currently in collection and QI for IUnknown

hr = CEnumVariant::Create(m_psa, m_cElements, &penum);

if (FAILED(hr))

{hr = RaiseException(IDS_OutOfMemory); goto error;}

hr = penum->QueryInterface(IID_IUnknown, (VOID FAR* FAR*)ppunkEnum);

if (FAILED(hr))

{hr = RaiseException(IDS_Unexpected); goto error;}

return NOERROR;

error:

if (penum)

delete penum;

return hr;

}

/*

*

* The following methods are not exposed through Automation

*

*/

/*

* CPoints::Add

*

* Purpose:

* Adds an item to the collection. The points collection does not have duplicates.

*

* Parameters:

* pPointAdd Point to be added to collection.

*

*/

STDMETHODIMP_(BOOL)

CPoints::Add(CPoint FAR* pPointAdd)

{

HRESULT hr;

LONG l;

VARIANT v;

VARIANT HUGEP *pvar;

int nX, nY;

BOOL bFound = FALSE;

LPDISPATCH pdispPointAdd = NULL;

CPoint FAR *pPoint;

LPDISPATCH pdispPoint;

// Is the collection full?

if (m_cElements == m_cMax)

goto error;

// Get coordinates of point to be added to collection

nX = pPointAdd->get_x();

nY = pPointAdd->get_y();

hr = SafeArrayAccessData(m_psa, (void HUGEP* FAR*)&pvar);

if (FAILED(hr))

goto error;

// Check if point to be added is already in the collection.

for (l=0; l<(long)m_cElements; l++)

{

pdispPoint = V_DISPATCH(&pvar[l]);

hr = pdispPoint->QueryInterface(IID_IPoint, (void FAR* FAR*)&pPoint);

if (FAILED(hr))

continue;

if (nX == pPoint->get_x() && nY == pPoint->get_y())

{

// Point is already in the collection. AddRef it.

// Internal AddRef keeps track of number of times this point was added to this collection. When

// this internal ref count drops to 0, this point can be removed from this collection.

pPoint->InternalAddRef();

pdispPoint->AddRef();

bFound = TRUE;

pPoint->Release();

break;

}

pPoint->Release();

}

hr = SafeArrayUnaccessData(m_psa);

if (FAILED(hr))

goto error;

// Add point if it was not in the collection.

if (!bFound)

{

l = m_lLBound+m_cElements;

hr = pPointAdd->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdispPointAdd);

if (FAILED(hr))

goto error;

VariantInit(&v);

V_VT(&v) = VT_DISPATCH;

V_DISPATCH(&v) = pdispPointAdd;

hr = SafeArrayPutElement(m_psa, &l, &v);

if (FAILED(hr))

goto error;

// Internal AddRef keeps track of number of times this point was added to this collection. When

// this internal ref count drops to 0, this point can be removed from this collection.

pPointAdd->InternalAddRef();

pdispPointAdd->Release();

m_cElements++;

}

return TRUE;

error:

if (pdispPointAdd)

pdispPointAdd->Release();

return FALSE;

}

/*

* CPoints::Remove

*

* Purpose:

* Removes specified item from collection. The points collection does not have duplicates.

*

* Parameters:

* pPointRemove Point to be removed from collection.

*

*/

STDMETHODIMP_(BOOL)

CPoints::Remove(CPoint FAR* pPointRemove)

{

HRESULT hr;

long l, lIndex;

VARIANT HUGEP *pvar;

ULONG cRef;

int nX, nY;

CPoint FAR* pPoint = NULL;

BOOL bFound = FALSE;

BOOL bRet = FALSE;

// Get coordinates of point to be removed from collection

nX = pPointRemove->get_x();

nY = pPointRemove->get_y();

hr = SafeArrayAccessData(m_psa, (void HUGEP* FAR*)&pvar);

if (FAILED(hr))

goto error;

// Check if point to be removed is in the collection.

for (l=0; l<(long)m_cElements; l++)

{

hr = V_DISPATCH(&pvar[l])->QueryInterface(IID_IPoint, (void FAR* FAR*)&pPoint);

if (FAILED(hr))

continue;

if (nX == pPoint->get_x() && nY == pPoint->get_y())

{

// Release point. Note that this collection does not have duplicates. Duplicate

// points are handled by increasing the ref count.

V_DISPATCH(&pvar[l])->Release();

cRef = pPoint->InternalRelease();

bFound = TRUE;

lIndex = l;

pPoint->Release();

break;

}

pPoint->Release();

}

// If the internal ref count of point to be removed has dropped to 0, move up the array elements

// after the element to be removed.

if (bFound && cRef == 0)

{

for (l=lIndex; l<(long)(m_cElements-1); l++)

pvar[l] = pvar[l+1];

// Remove last element.

V_VT(&pvar[l]) = VT_EMPTY;

m_cElements--;

bRet = TRUE;

}

hr = SafeArrayUnaccessData(m_psa);

if (FAILED(hr))

goto error;

return bRet;

error:

return FALSE;

}

/*

* CPoints::Clear

*

* Purpose:

* Removes all items from collection.

*

*/

STDMETHODIMP_(void)

CPoints::Clear(void)

{

SafeArrayDestroyData(m_psa);

SafeArrayAllocData(m_psa);

m_cElements = 0;

}

/*

* CPoints::GetMaxXMaxY

*

* Purpose:

* Get the maximum X and Y coordinates that are in the rectangle, prc.

*

*/

STDMETHODIMP_(void)

CPoints::GetMaxXMaxY(int FAR *pnX, int FAR *pnY, LPRECT prc)

{

HRESULT hr;

long l;

VARIANT HUGEP *pvar;

int nMaxX, nMaxY;

CPoint FAR* pPoint;

POINT pt;

hr = SafeArrayAccessData(m_psa, (void HUGEP* FAR*)&pvar);

if (FAILED(hr))

return;

nMaxX = -1; nMaxY = -1;

// Find the maximume X and Y coordinates that are in the prc rectangle.

for (l=0; l<(long)m_cElements; l++)

{

V_DISPATCH(&pvar[l])->QueryInterface(IID_IPoint, (void FAR* FAR*)&pPoint);

pt.x = pPoint->get_x();

pt.y = pPoint->get_y();

if (PtInRect(prc, pt))

{

if (pt.x > nMaxX)

nMaxX = pt.x;

if (pt.y > nMaxY)

nMaxY = pt.y;

}

pPoint->Release();

}

*pnX = nMaxX;

*pnY = nMaxY;

SafeArrayUnaccessData(m_psa);

return;

}

/*

* CPoints::RaiseException

*

* Purpose:

* Fills the EXCEPINFO structure and signal IDispatch::Invoke to return DISP_E_EXCEPTION.

* Sets ErrorInfo object for vtable-binding controllers.

*

*/

STDMETHODIMP

CPoints::RaiseException(int nID)

{

extern SCODE g_scodes[];

TCHAR szError[STR_LEN];

ICreateErrorInfo *pcerrinfo;

IErrorInfo *perrinfo;

HRESULT hr;

_fmemset(&m_excepinfo, 0, sizeof(EXCEPINFO));

m_excepinfo.wCode = nID;

if (LoadString(g_pApplication->m_hinst, nID, szError, sizeof(szError)))

m_excepinfo.bstrDescription = SysAllocString(TO_OLE_STRING(szError));

m_excepinfo.bstrSource = SysAllocString(g_pApplication->m_bstrProgID);

m_bRaiseException = TRUE;

// Set ErrInfo object so that vtable binding containers can get

// rich error information.

hr = CreateErrorInfo(&pcerrinfo);

if (SUCCEEDED(hr))

{

pcerrinfo->SetGUID(IID_IPoints);

if (m_excepinfo.bstrSource)

pcerrinfo->SetSource(m_excepinfo.bstrSource);

if (m_excepinfo.bstrDescription)

pcerrinfo->SetDescription(m_excepinfo.bstrDescription);

hr = pcerrinfo->QueryInterface(IID_IErrorInfo, (LPVOID FAR*) &perrinfo);

if (SUCCEEDED(hr))

{

SetErrorInfo(0, perrinfo);

perrinfo->Release();

}

pcerrinfo->Release();

}

return ResultFromScode(g_scodes[nID-1001]);

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun