Click here to Skip to main content
15,888,816 members
Articles / Programming Languages / C#

Collections Interoperability

Rate me:
Please Sign up or sign in to vote.
4.18/5 (5 votes)
23 May 20052 min read 40.9K   985   25  
This article describes how to move collections between native and managed code.
// TestSafeObject.h : Declaration of the CTestSafeObject

#pragma once
#include "resource.h"       // main symbols
#include <comdef.h>
#include <vector>
#include "comutils.h"

[export,
uuid("3DA0FCDB-BAC1-4b13-8808-AB3E43A281BA")]
struct PTZPresetsInfo
{
	 int		PTZID;
	 int		PresetID;
	 BSTR		PresetName;		
};

// ITestSafeObject
[
	object,
	uuid("A2632E1C-ADFC-4AB6-BD08-A3417CEECAA8"),
	dual,	helpstring("ITestSafeObject Interface"),
	pointer_default(unique)
]
__interface ITestSafeObject : IDispatch
{
	[id(1), helpstring("method Run")] HRESULT Run([in, satype(int)] SAFEARRAY *kk);
	[id(2), helpstring("method RunOut")] HRESULT RunOut([out, satype(int)] SAFEARRAY **kk);
	[id(3), helpstring("method RunPTZ")] HRESULT RunPTZ([out, satype(struct PTZPresetsInfo)] SAFEARRAY **kk);
	[id(4), helpstring("method RunPTZ1")] HRESULT RunPTZ1([in, out, satype(struct PTZPresetsInfo)] SAFEARRAY *kk);
};



// CTestSafeObject

[
	coclass,
	threading("apartment"),
	vi_progid("TestSafearray.TestSafeObject"),
	progid("TestSafearray.TestSafeObject.1"),
	version(1.0),
	uuid("E66B5E5D-5EEF-4988-8776-90B02FD3AAB4"),
	helpstring("TestSafeObject Class")
]
class ATL_NO_VTABLE CTestSafeObject : 
	public ITestSafeObject
{
public:
	CTestSafeObject()
	{
	}


	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return S_OK;
	}
	
	void FinalRelease() 
	{
	}

public:

	STDMETHOD(Run)(SAFEARRAY *kk);
	STDMETHOD(RunOut1)(PTZPresetsInfo *kk) {return S_OK;}
	STDMETHOD(RunOut)(SAFEARRAY **kk)
	{
		SAFEARRAY *pPresetsInfoCol;
		unsigned int ndim =  1;
		HRESULT hr = SafeArrayAllocDescriptor(ndim, &pPresetsInfoCol);
		if(FAILED(hr))
		{
			return S_OK;
		}
		pPresetsInfoCol->rgsabound[0].lLbound = 0;
		pPresetsInfoCol->rgsabound[0].cElements = 20;
		pPresetsInfoCol->cbElements = sizeof(int);
		
		hr = SafeArrayAllocData(pPresetsInfoCol);
		if( FAILED(hr)) 
		{
   			SafeArrayDestroyDescriptor(pPresetsInfoCol);
			return S_OK;
		}
		
		LONG Index = 0;
		int j = 1;
		hr = SafeArrayPutElement(pPresetsInfoCol,&Index,(void*)&j);
		Index = 1;
		j = 2;
		hr = SafeArrayPutElement(pPresetsInfoCol,&Index,(void*)&j);
		Index = 2;
		j = 3;
		hr = SafeArrayPutElement(pPresetsInfoCol,&Index,(void*)&j);
		Index = 3;
		j = 4;
		hr = SafeArrayPutElement(pPresetsInfoCol,&Index,(void*)&j);
		*kk = pPresetsInfoCol;
		return S_OK;
	}	
	STDMETHOD(RunPTZ)(SAFEARRAY **kk)
	{
		SAFEARRAY *pPresetsInfoCol;
		unsigned int ndim =  1;

		PTZPresetsInfo PresetsInfoObj;
		PresetsInfoObj.PresetID = 1;
		PresetsInfoObj.PTZID = 3;
		_bstr_t myname("meir");
		PresetsInfoObj.PresetName = myname.Detach();
		std::vector<PTZPresetsInfo> v;
		v.push_back(PresetsInfoObj);
		PresetsInfoObj.PTZID = 4;
		v.push_back(PresetsInfoObj);
		*kk = CreateUDTSafeArrayFromCol(v, NULL);
/*		USES_CONVERSION;
	    SAFEARRAYBOUND                rgbounds;
		rgbounds.lLbound = 0;
		rgbounds.cElements = 20;
        
		IRecordInfo*                  pRecInfo = NULL;

		;
		char FileName[_MAX_PATH];
	    GetModuleFileName(_AtlBaseModule.GetModuleInstance(), FileName, _MAX_PATH);

		//GetModuleFileName
		ITypeLib* pTypelib = NULL;
		HRESULT hr = LoadTypeLib(A2OLE(FileName),&pTypelib);

		ITypeInfo *pTypeInfo;
		hr = pTypelib->GetTypeInfoOfGuid(__uuidof(PTZPresetsInfo), &pTypeInfo);

		hr = GetRecordInfoFromTypeInfo(pTypeInfo, &pRecInfo);

		pPresetsInfoCol = SafeArrayCreateEx(VT_RECORD, 1, &rgbounds, pRecInfo);
		BSTR name;
		pRecInfo->GetName(&name);

		pRecInfo->Release();

		PTZPresetsInfo *PresetsInfo;
		hr = SafeArrayAccessData(pPresetsInfoCol, reinterpret_cast<PVOID*>(&PresetsInfo));
 
		PTZPresetsInfo PresetsInfoObj;
		PresetsInfoObj.PresetID = 1;
		PresetsInfoObj.PTZID = 3;
		_bstr_t myname("meir");
		PresetsInfoObj.PresetName = myname.Detach();

        PresetsInfo[0] = PresetsInfoObj;
        PresetsInfo[1] = PresetsInfoObj;

		*kk = pPresetsInfoCol;
		SafeArrayUnaccessData(pPresetsInfoCol);*/
		return S_OK;
	}	
	


	 STDMETHOD(RunPTZ1)(SAFEARRAY *kk)
	 {
		PTZPresetsInfo *PresetsInfo;
        HRESULT hr;
        hr = SafeArrayAccessData(kk, reinterpret_cast<PVOID*>(&PresetsInfo));
		PresetsInfo[0].PresetID = 9;
		PresetsInfo[1].PresetID = 11;
		PresetsInfo[1].PTZID = 12;
		_bstr_t name("meir");
		PresetsInfo[1].PresetName = name.Detach();

		return S_OK;
	}
};

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
Israel Israel
6 years C++ and .NET programer in Nice Systems

Comments and Discussions