Marshal strings from COM 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
 
N

Nicholas Paldino [.NET/C# MVP]

awk,

I assume you are using COM interop to access the C# method in VB6. Your
method needs to be attributed like this:

[return:MarshalAs(UnmanagedType.BStr)]
public string MyMethod([MarshalAs(UnmanagedType.BStr)] string param1);

While I think that it does this by default (if you are using com
interop), this should explicitly set it.

Hope this helps.
 
A

awk

Thanks Nicholas

I gave it a try but still I get ????????????????????? as the value of the
methods parameter.
Here is the VB6 Method that makes the call :

Public Function Reconstruct(data As String) As String

Dim reco
Set reco =
CreateObject("IconGlobal.CarelinkPlus.ReportsUtilities.CrystalReportsUtility
")
Reconstruct = reco.ReconstructData(data)

End Function

Is that all I need to do to make a call to the managed code (it was in the
example I have - using late binding).
Also I should mention that the string being passed to the VB method is being
passed from
a Crystal Reports formula (the vb method is part of a Crystal UFL).

Thanks

S


Nicholas Paldino said:
awk,

I assume you are using COM interop to access the C# method in VB6. Your
method needs to be attributed like this:

[return:MarshalAs(UnmanagedType.BStr)]
public string MyMethod([MarshalAs(UnmanagedType.BStr)] string param1);

While I think that it does this by default (if you are using com
interop), this should explicitly set it.

Hope this helps.


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

awk said:
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
 

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