.Net COM interop

G

Guest

In COM I have an interface method that takes in
IMyInterface::MyMethod([in] ULONG ulCount, LPCWSTR* ppwszStrings);

So the client can do something like:
LPWSTR lpwszNet[2] ={L"String1" , L"String2"};
pMyInterface->MyMethod(2,&lpwszNet[0]);

Now if I wanna use the COM component from .Net LCWSTR* gets converted to ref
string.
So .Net C# code sees IMyInterface::MyMethod(uint ulCount,ref string
ppwszStrings);
How do I pass in an array of strings via this paramter.
 
N

Nicholas Paldino [.NET/C# MVP]

RP,

You will have to change the interop assembly (by decompiling to IL
changing, and then recompiling the IL, or by defining the interface
yourself) and define the parameter as a string array, and set the
appropriate MarshalAs attribute on the parameter.

If the method alters the content of the strings, then you will more than
likely have to marshal the strings manually.
 
G

Guest

Nicholas,

So I changed the .il
From:
MyMethod([in] unit32 ulCount,
[in] string& marshal(lpwstr) ppwszCats);

To:
MyMethod([in] uint32 ulCount,
[in] string[] marshal([]) ppwszCats);

Now the Com server I am using is complaining, my guess is (don't have source
for COM server) that the String array I am passing in is not getting
converted to LPWCSTR*, Is there another way to do this ?

Nicholas Paldino said:
RP,

You will have to change the interop assembly (by decompiling to IL
changing, and then recompiling the IL, or by defining the interface
yourself) and define the parameter as a string array, and set the
appropriate MarshalAs attribute on the parameter.

If the method alters the content of the strings, then you will more than
likely have to marshal the strings manually.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

RP said:
In COM I have an interface method that takes in
IMyInterface::MyMethod([in] ULONG ulCount, LPCWSTR* ppwszStrings);

So the client can do something like:
LPWSTR lpwszNet[2] ={L"String1" , L"String2"};
pMyInterface->MyMethod(2,&lpwszNet[0]);

Now if I wanna use the COM component from .Net LCWSTR* gets converted to
ref
string.
So .Net C# code sees IMyInterface::MyMethod(uint ulCount,ref string
ppwszStrings);
How do I pass in an array of strings via this paramter.
 
M

Mattias Sjögren

So I changed the .il
From:
MyMethod([in] unit32 ulCount,
[in] string& marshal(lpwstr) ppwszCats);

To:
MyMethod([in] uint32 ulCount,
[in] string[] marshal([]) ppwszCats);

Now the Com server I am using is complaining, my guess is (don't have source
for COM server) that the String array I am passing in is not getting
converted to LPWCSTR*, Is there another way to do this ?

Try

MyMethod([in] uint32 ulCount,
[in] string[] marshal(lpwstr[]) ppwszCats);


Mattias
 
G

Guest

Nope: Same Thing I get an error in COM server,

Mattias Sjögren said:
So I changed the .il
From:
MyMethod([in] unit32 ulCount,
[in] string& marshal(lpwstr) ppwszCats);

To:
MyMethod([in] uint32 ulCount,
[in] string[] marshal([]) ppwszCats);

Now the Com server I am using is complaining, my guess is (don't have source
for COM server) that the String array I am passing in is not getting
converted to LPWCSTR*, Is there another way to do this ?

Try

MyMethod([in] uint32 ulCount,
[in] string[] marshal(lpwstr[]) ppwszCats);


Mattias
 
G

Guest

I wrote a test dll and the interop method you suggested works just fine, so I
think there might be a problem with the COM server.

Thanks a lot

RP said:
Nope: Same Thing I get an error in COM server,

Mattias Sjögren said:
So I changed the .il
From:
MyMethod([in] unit32 ulCount,
[in] string& marshal(lpwstr) ppwszCats);

To:
MyMethod([in] uint32 ulCount,
[in] string[] marshal([]) ppwszCats);

Now the Com server I am using is complaining, my guess is (don't have source
for COM server) that the String array I am passing in is not getting
converted to LPWCSTR*, Is there another way to do this ?

Try

MyMethod([in] uint32 ulCount,
[in] string[] marshal(lpwstr[]) ppwszCats);


Mattias
 

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