Cute
Cute

Reputation: 14021

How to pass 2 string lists to c++ from c# through COM?

I need to pass string lists to unmanaged c++ how can I do this ?

I have used IDictionary as method return type and send through com but it doesn't work. How to achieve this?

What I have written in C# is as follows -

IDictionary<string,string> postNames()
{
  IDictionary<string,string> post=Dictionary<string,string>();
  post.Add("Raj");
  post.Add("Mahesh");
  post.Add("john steek");

  return post;  
}

Then I have created a dll for that class contains this method.

Now how can i access these values in unmanaged c++....

I am worried about two things

1) About the return type to carry these values in c++

2) Is it possible to use like this way ..

Any help in this regard?

Upvotes: 3

Views: 904

Answers (3)

Remus Rusanu
Remus Rusanu

Reputation: 294487

Probably the simplest way is to pass a string[] and use it in the unmanaged side as an SAFEARRAY(BSTR) type. You can also pass a string[] and consume it in COM as a LPStr[] by using the MarshalAs (this is the sample on MSDN):

void FunctionCall([MarshalAs(UnmanagedType.LPARRAY, 
   ArraySubType= UnmanagedType.LPStr, SizeParamIndex=1)] 
   String [,] ar, int size );

See Default Marshaling for Arrays for more details.

Upvotes: 0

sharptooth
sharptooth

Reputation: 170569

It's essentially the same problem as in this question. You can implement perfect Earwicker's answer here - declare a set of interfaces and accessor classes that wrap the Dictionary and expose them to COM.

Upvotes: 0

J-16 SDiZ
J-16 SDiZ

Reputation: 26930

A quick, dirty method is write a C++/CLI wrapper.

Upvotes: 1

Related Questions