|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionIn my work, I am using a COM object as an interface between a managed server and a HW device that has a C DLL interface. Last week, I encountered the need to pass a collection from the HW device to the server. To do that, I had the COM read it from the device and pass it to the server. The problem was that I couldn’t return a collection of my UDT (User Defined Type) from COM to the managed server. The codeSo how do we return a UDT from native to managed code? The easiest way to achieve interoperability between native and managed code is by putting the unmanaged code in a COM object. So what is left is to return the collection from COM to the managed code. COM has two ways to return collections:
The first way does not work with .NET. The interop proxy that will be created for it will look like: void Foo(int Length, ref SUDTStruct Col)
which means that only one To do that we need a few simple stages:
A few remarks: if you want to pass a collection of a UDT to COM all you need to do is define a UDT with a GUID and define a function like so: HRESULT Bar(SAFEARRAY([in] SUDTStruct) *Col)
This function will appear in the .NET interop like so: void Bar(System.Array Col)
When calling it, pass a In order to ease the process of creating the safe array I created a simple template function that receives a STL collection that has the UDT as its value type and create a safe array out of it with the same data. The source is attached to this article. For example: std::vector<SUDTStruct> Col; // init the collection with the data SAFEARRAY *pSafeArrayCol = CreateUDTSafeArrayFromCol(Col); The The demo project attached is a COM object and a .NET assembly that sends and gets collections from it. Points of InterestMicrosoft has done a lot of work to make COM a mechanism for interoperability between managed and unmanaged areas, but not all the COM capability is exported through the .NET proxy created for COM.
|
||||||||||||||||||||||||||||||||||||||||