Marshalling STRING from COM dll to Managed code?

A

awk

Hi All

I have a com dll written in VB6 (it's a User Function Library for my crystal
reports - this allows me to write custom functions for Crystal
which can be applied in Crystal formulas - none of this is relevant to the
problem though (((I think)))).
Anyway, the VB dll has one method that takes a string as an argument and
returns a string. It passes the string arg to a C# library
which is where the problem lies. The string arg contains unicode data and
when it passes it to the c# managed code the unicode is lost
and turns into ???????????????? characters which means the c# code doesnt
know what to do with it (it needs the unicode string). Therefore I think
that the marshalling process of the string from vb dll to c# dll is my
issue. What do I have to do to marshall this string properly? Should I be
marshalling it as a string or perhaps a byte[].

Any help greatly appreciated

S
 
C

Cor

Hi AWK,

Did you know that you did post to the VB.net language news group.

The C# language newsgroup is one floor higher, above the JavaScript
newsgroup.

Cor
 
H

Herfried K. Wagner [MVP]

* "Cor said:
Did you know that you did post to the VB.net language news group.

The C# language newsgroup is one floor higher, above the JavaScript
newsgroup.

The solution will be similar for VB.NET and C#. But maybe the interop
newsgroup is the better place to post the question
(microsoft.public.dotnet.framework.interop).
 
M

Mike Labosh

Anyway, the VB dll has one method that takes a string as an argument and
returns a string. It passes the string arg to a C# library
which is where the problem lies. The string arg contains unicode data and
when it passes it to the c# managed code the unicode is lost
and turns into ???????????????? characters which means the c# code doesnt
know what to do with it (it needs the unicode string). Therefore I think
that the marshalling process of the string from vb dll to c# dll is my
issue. What do I have to do to marshall this string properly? Should I be
marshalling it as a string or perhaps a byte[].

I have stumbled across something similar while doing API work in VB 6.0:
String data is internally stored in Unicode format, but any time it passes
out of it's code block (calling an automation object or an API), string data
is converted to ANSI automatically by the runtime.

That's why it's *so hard* to call unicode API functions -- because even
though your VB6 string is unicode in memory, it is always passed as ANSI. I
bet you're having something similar happen here.

I like Herfried's answer -- go sniffing around the Runtime.Interop people
and namespace. The solution that I implemented is not really relevant (it
was strictly VB6 / API) and it was a huge heap of scary code that hacked
bits and stuff.

You mentioned using a Byte[]. If you do try something like this, remember
that ANSI characters are 1 byte each and Unicode characters could be 2, 3 or
even 4 bytes each, depending on the encoding.
--
Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com
"Escriba coda ergo sum." -- vbSensei
 

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