COM interop, marshaling

R

RP

I have the following methon on a COM interface:
HRESULT GetProperty([in] REFASAKEY key, [in] EDSPROPERTY property, [in]
ULONG count, [out,size_is(count)] LPWSTR name);

Which is exposed to COM (via RCW) as:
void GetProperty(ref RSEDSLib.ASAKEY key, RSEDSLib.EDSPROPERTY property,
uint Count, string name)

It's not able to marshal the last parameter which is an out parameter
correctly, how do I solve this ?
 
N

Nicholas Paldino [.NET/C# MVP]

RP,

You will have to change the signature of the method to this (which means
you might have to define the interface yourself in code, or modify the RCW):

void GetProperty(
ref RSEDSLib.ASAKEY key,
REDSLib.EDSPROPERTY property,
uint Count,
[MarshalAs(UnmanagedType.LPWStr)]
StringBuilder name);

You would have to pre-allocate the StringBuilder, of course. If that
doesn't work, you might be able to get away with this:

void GetProperty(
ref RSEDSLib.ASAKEY key,
REDSLib.EDSPROPERTY property,
uint Count,
[Out, MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)]
char[] name);


And then change the character array to a string when the call returns.
You will also have to make sure that the character array is pre-allocated.
 

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