How to pass an array of objects from C# to C++? (newB)

G

gchandran

Hello,

I am developing a DLL using C# . I am using VS 2005 for this. The
component will expose few API's. One of the API performs certain
operations and needs to return an array of objects (class defined in
the C# dll). I have a C++ executable which instantiates the C# DLL and
invokes the API's.

I am using SAFEARRAY to pass the array of object from the DLL to C++
executable. I am able to successfuly successfully invoke the method and
get a filled array.

Problem:
Now I am not sure how to get the required object from the SAFEARRAY?

Source code snippet:

C# DLL:
------------------------------------------------------------------------------------------------------------------------------
public interface ICSharp2CPlus
{
void Interface_getResourceData(IdentificationSpecifier
ParentIdentificationSpecifier, SelectedMetadata
SelectedMetadata,[In,Out, MarshalAs(UnmanagedType.SafeArray,
SafeArraySubType =
VarEnum.VT_USERDEFINED)] ref ResourceSpecifier[] res);

}

public class MyClass : ICSharp2CPlus
{
public ContentRepositoryService service;

public MyClass()
{
service = new ContentRepositoryService();
}

public void Interface_getResourceData(IdentificationSpecifier
ParentIdentificationSpecifier, SelectedMetadata
SelectedMetadata,[In,Out, MarshalAs(UnmanagedType.SafeArray,
SafeArraySubType =
VarEnum.VT_USERDEFINED)] ref ResourceSpecifier[] res)
{
// service.getChildren successfully returns an array of
ResourceSpecifier
res = service.getChildren(ParentIdentificationSpecifier,
SelectedMetadata);
}

}

public partial class ResourceSpecifier
{
private Resource resourceField;

/// <remarks/>
public Resource Resource
{
get
{
return this.resourceField;
}
set
{
this.resourceField = value;
}
}
}
--------------------------------------------------------------------------------------------------------------------------------------
C++ Invocation
// The Main Interface
ICSharp2CPlus* CSharpInterface = NULL;

// Initializes the COM library on the current thread and identifies
the
// concurrency model as single-thread apartment (STA).
CoInitialize(NULL);


//Instantiate the COM object in the appropriate ServiceObject
apartment
if
(FAILED(CoCreateInstance(CLSID_MyClass,NULL,CLSCTX_INPROC_SERVER,IID_ICSharp2CPlus,reinterpret_cast<void**>(&CSharpInterface))))
return 0;

// Create the pointer to the SAFEARRAY
SAFEARRAY *pSafeArray = NULL;

// Finally call the getchildren method to get the children
if(FAILED(CSharpInterface->Interface_getResourceData(IIDSpecifier,NULL,&pSafeArray)))
return 0;
----------------------------------------------------------------------------------------------------------------------------------

Now how do I get ResourceSpecifier objects from the pSafeArray?

Please give me some pointers to resolve this!

Girish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top