ASCII string for interop?

B

Bill Cohagan

I'm trying to call an ActiveX dll via interop, but it expects a ref string
argument. Of course I can call it foo(ref strData), but this passed a
unicode string if strData is of type "string". I can convert to byte [],
but can't pass that as foo expects ref string.

So, how do I go about converting from unicode to ASCII (or UTF8), but still
pass something of string type?

Thanks in advance,
Bill
 
C

Chris A. R.

Something along these lines,

([MarshalAs(UnmanagedType.LPStr)]
string strData;

Check out Marshalling, MarshalAs, and the UnmanagedType Enumeration for more
details. Also check out the SizeConst parameter for fixed size strings.

<From Microsoft: "Default Marshaling for Strings">
UnmanagedType.BStr A COM-style BSTR with a prefixed length and Unicode
characters.
UnmanagedType.LPStr A pointer to a null-terminated array of ANSI
characters.
UnmanagedType.LPTStr A pointer to a null-terminated array of
platform-dependent characters.
UnmanagedType.LPWStr A pointer to a null-terminated array of Unicode
characters.
UnmanagedType.ByValTStr A fixed-length array of characters; the array's type
is determined by the character set of the containing structure.
</From Microsoft: "Default Marshaling for Strings">

Chris A.R.
 
J

Joe Mayo [C# MVP]

Bill Cohagan said:
I'm trying to call an ActiveX dll via interop, but it expects a ref string
argument. Of course I can call it foo(ref strData), but this passed a
unicode string if strData is of type "string". I can convert to byte [],
but can't pass that as foo expects ref string.

So, how do I go about converting from unicode to ASCII (or UTF8), but still
pass something of string type?


Hi Bill,

I believe a StringBuilder should work in place of the string.

Joe
 
W

Willy Denoyette [MVP]

ActiveX dll in general use BSTR as strings and these are unicode strings,
just like CLR strings, so the COM interop marshaler should be happy to
handle this automaticaly.

Willy.
 

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