Home   Index   About
Ultimate Pack


Custom Search
FUNCTION.CPP (BROWSEH OLE Sample)

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

  • OLE Automation TypeLibrary Browse Helper Sample

  • function.cpp

  • CFunction implementation

  • Written by Microsoft Product Support Services, Windows Developer Support

  • (c) Copyright Microsoft Corp. 1994 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 "browseh.h"

/*

* CFunction::Create

*

* Purpose:

* Creates an instance of the Function automation object and initializes it.

*

* Parameters:

* ptinfo TypeInfo of which this Function is an element of.

* nIndex Index of function in containing TypeInfo.

* ppFunction Returns Function automation object.

*

* Return Value:

* HRESULT

*

*/

HRESULT

CFunction::Create(LPTYPEINFO ptinfo, unsigned short nIndex, CFunction FAR* FAR* ppFunction)

{

HRESULT hr;

CFunction FAR* pFunction = NULL;

CTypeDesc FAR* pTypeDesc = NULL;

*ppFunction = NULL;

// Create object.

pFunction = new CFunction();

if (pFunction == NULL)

{

hr = ResultFromScode(E_OUTOFMEMORY);

goto error;

}

// Load type information for the application object from type library.

hr = pFunction->LoadTypeInfo(IID_IFunction);

if (FAILED(hr))

goto error;

hr = ptinfo->GetFuncDesc(nIndex, &pFunction->m_pfuncdesc);

if (FAILED(hr))

goto error;

// Function return type.

hr = CTypeDesc::Create(ptinfo, &pFunction->m_pfuncdesc->elemdescFunc.tdesc, &pTypeDesc);

if (FAILED(hr))

goto error;

pTypeDesc->QueryInterface(IID_IDispatch, (LPVOID FAR*)&pFunction->m_pdispTypeDesc);

hr = ptinfo->GetDocumentation(pFunction->m_pfuncdesc->memid, &pFunction->m_bstrName, &pFunction->m_bstrDocumentation,

&pFunction->m_ulHelpContext, &pFunction->m_bstrHelpFile);

if (FAILED(hr))

goto error;

ptinfo->AddRef();

pFunction->m_ptinfoFunction = ptinfo;

#ifdef _DEBUG

lstrcpyn(pFunction->m_szClassName, TEXT("Function"), 100);

#endif

*ppFunction = pFunction;

return NOERROR;

error:

if (pFunction == NULL) return ResultFromScode(E_OUTOFMEMORY);

if (pFunction->m_pfuncdesc) ptinfo->ReleaseFuncDesc(pFunction->m_pfuncdesc);

if (pFunction->m_ptinfoFunction) pFunction->m_ptinfoFunction->Release();

if (pFunction->m_bstrName) SysFreeString(pFunction->m_bstrName);

if (pFunction->m_bstrDocumentation) SysFreeString(pFunction->m_bstrDocumentation);

if (pFunction->m_bstrHelpFile) SysFreeString(pFunction->m_bstrHelpFile);

// Set to NULL to prevent destructor from attempting to free again

pFunction->m_ptinfoFunction = NULL;

pFunction->m_pfuncdesc = NULL;

pFunction->m_bstrName = NULL;

pFunction->m_bstrDocumentation = NULL;

pFunction->m_bstrHelpFile = NULL;

delete pFunction;

return hr;

}

/*

* CFunction::CFunction

*

* Purpose:

* Constructor for CFunction object. Initializes members to NULL.

*

*/

CFunction::CFunction()

{

m_bstrName = NULL;

m_bstrDocumentation = NULL;

m_bstrHelpFile = NULL;

m_pdispParameters = NULL;

m_ptinfoFunction = NULL;

m_pfuncdesc = NULL;

m_pdispTypeDesc = NULL;

}

/*

* CFunction::~CFunction

*

* Purpose:

* Destructor for CFunction object.

*

*/

CFunction::~CFunction()

{

if (m_bstrName) SysFreeString(m_bstrName);

if (m_bstrDocumentation) SysFreeString(m_bstrDocumentation);

if (m_bstrHelpFile) SysFreeString(m_bstrHelpFile);

if (m_pdispParameters) m_pdispParameters->Release();

if (m_pfuncdesc && m_ptinfoFunction) m_ptinfoFunction->ReleaseFuncDesc(m_pfuncdesc);

if (m_ptinfoFunction) m_ptinfoFunction->Release();

if (m_pdispTypeDesc) m_pdispTypeDesc->Release();

}

STDMETHODIMP_(REFCLSID)

CFunction::GetInterfaceID()

{

return IID_IFunction;

}

STDMETHODIMP_(BSTR)

CFunction::get_Name()

{

return SysAllocString(m_bstrName);

}

STDMETHODIMP_(BSTR)

CFunction::get_Documentation()

{

return SysAllocString(m_bstrDocumentation);

}

STDMETHODIMP_(long)

CFunction::get_HelpContext()

{

return (long)m_ulHelpContext;

}

STDMETHODIMP_(BSTR)

CFunction::get_HelpFile()

{

return SysAllocString(m_bstrHelpFile);

}

STDMETHODIMP_(ITypeDesc FAR*)

CFunction::get_ReturnType()

{

m_pdispTypeDesc->AddRef();

return (ITypeDesc FAR*)m_pdispTypeDesc;

}

STDMETHODIMP_(ICollection FAR*)

CFunction::get_Parameters()

{

HRESULT hr;

CParameter FAR* pParameter;

CCollection FAR* pCollection = NULL;

LPDISPATCH pdisp;

BSTR FAR* rgbstrNames = NULL;

unsigned int cNames;

unsigned short n = 0;

if (m_pdispParameters == NULL)

{

// Create a collection of parameters and return it.

rgbstrNames = new BSTR[m_pfuncdesc->cParams+1];

if (rgbstrNames == NULL)

{RaiseException(IDS_OutOfMemory); return NULL;}

hr = m_ptinfoFunction->GetNames(m_pfuncdesc->memid, rgbstrNames, m_pfuncdesc->cParams+1, &cNames);

if (FAILED(hr))

{RaiseException(IDS_Unexpected); goto error;}

SysFreeString(rgbstrNames[0]);

hr = CCollection::Create(cNames-1, 0, &pCollection);

if (FAILED(hr))

{RaiseException(IDS_Unexpected); goto error;}

for (n=0; n<cNames-1; n++)

{

hr = CParameter::Create(m_ptinfoFunction, rgbstrNames[n+1],

&m_pfuncdesc->lprgelemdescParam[n].tdesc,

&m_pfuncdesc->lprgelemdescParam[n].idldesc,

&pParameter);

if (FAILED(hr))

{RaiseException(IDS_Unexpected); goto error;}

SysFreeString(rgbstrNames[n+1]);

pParameter->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);

pCollection->Add(pdisp);

pdisp->Release();

}

pCollection->QueryInterface(IID_IDispatch, (void FAR* FAR*)&pdisp);

m_pdispParameters = pdisp;

delete rgbstrNames;

}

m_pdispParameters->AddRef();

return (ICollection FAR*)m_pdispParameters;

error:

if (pCollection) delete pCollection;

if (rgbstrNames)

for (; n<cNames-1; n++)

SysFreeString(rgbstrNames[n+1]);

return NULL;

}

STDMETHODIMP_(MEMBERID)

CFunction::get_Memberid()

{

return m_pfuncdesc->memid;

}

STDMETHODIMP_(CALLCONV)

CFunction::get_CallConvention()

{

return m_pfuncdesc->callconv;

}

STDMETHODIMP_(FUNCKIND)

CFunction::get_FuncKind()

{

return m_pfuncdesc->funckind;

}

STDMETHODIMP_(INVOKEKIND)

CFunction::get_InvocationKind()

{

return m_pfuncdesc->invkind;

}

STDMETHODIMP_(OBJTYPE)

CFunction::get_Kind()

{

return TYPE_FUNCTION;

}


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