all groups > dotnet interop > august 2006 >
You're in the

dotnet interop

group:

C# Class using VB6 Interface incorrectly identifies parameter



C# Class using VB6 Interface incorrectly identifies parameter Mike Sharpe
8/31/2006 11:20:02 AM
dotnet interop: Here's the breakdown:

1. My class is written in C#
2. It inherits an interface called AlfaBVIO._BVInput which looks like this
via the Interop metadata:

[Guid("B2332A7B-42C5-4828-8B2B-9D2BE8EFA3DD")]
[TypeLibType(4304)]
public interface _BVInput
{
[DispId(1610809345)]
void GetScenarios(string iFileName, ref Array oScenarios);
[DispId(1610809344)]
void Initialize(ref DSDataType iSettings);
}

3. This interface comes from a VB6 COM DLL
4. I am trying to implement the interface and the method call GetScenarios.
The only problem is that the second parameter (oScenarios) should not be a
System.Array type. This is in fact an array of a class from another VB6 COM
DLL. It should actually be this:

void GetScenarios(string iFileName, ref DSDataType[] oScenarios);

It is interesting to note that the parameter for the Initialize() method
accurately is defined as a single DSDataType object when the array of them
cannot.

5. Unfortunately, the compiler does not allow me to change it to the
appropriate type since it would then not be the valid interface
implementation. I then found that I might need to use the MarshalAs()
attribute modifier. However nothing seems to work properly. I've tried
setting it as:

[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType =
VarEnum.VT_VARIANT)] ref System.Array oScenarios
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType =
VarEnum.VT_UNKNOWN)] ref System.Array oScenarios
[MarshalAs(UnmanagedType.SafeArray, SafeArraySubType =
VarEnum.VT_DISPATCH)] ref System.Array oScenarios

None of these seem to work.

I even tried to use the UnmanagedType.CustomMarshaler, MarshalRefType =
Typeof(DSDataType)


When I run the code, I get the following error message:
InvalidMemberDeclaration was detected
Message: The following error occurred while determining how to marshal the
parameters of member 'GetScenarios' of type 'AlfaBVIO._BVInput':
System.ArgumentException: Exception of type 'System.ArgumentException' was
thrown.
Parameter name: typeName@0 This is most likely due to an incompatible
MarshalAs attribute on one of the parameters.


I thought it might be a SafeArray because when we would do similar in C++
6.0, we had to package everything up as a COM SAFEARRAY. I am really at a
Re: C# Class using VB6 Interface incorrectly identifies parameter Mattias Sjögren
9/3/2006 12:00:00 AM
Mike,

I assume you're using an interop assembly creeated by Add Reference in
Visual Studio. If you instead create the interop assembly with
Tlbimp.exe I think you'll get the method signature you want with the
strongly typed array.

See the descrption of Tlbimp's /sysarray option for more details.
That's effectively what Visual Studio is using.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button