Hi

I need to handle events raised by a COM object in a .Net client. I use
tlbimp to create interop marshaling assembly. To my surprise some of the
events are not marshaled correctly. To nail the problem down I have created
simple VB ActiveX object with four events as in the code below. Each of the
events has one or more arrays in the argument list. Events 1, 3, 4 are OK
but 2 throws an exception. Using /sysarray key (or simply referencing COM
object in VS.Net) does not make any difference. Note that event 4 has the
same arguments as 2 (troubled one) but in a different order.

Here is VB6 server code

Public Event Event1(ByVal Scalar1 As Long, IntVector() As Long)
Public Event Event2(ByVal Scalar1 As Long, IntVector() As Long, StrVector()
As String)
Public Event Event3(ByRef Scalar1 As Long, IntVector() As Long, StrVector()
As String)
Public Event Event4(IntVector() As Long, StrVector() As String, ByVal
Scalar1 As Long)

Public Sub FireEvent1()

    Dim lngScalar As Long
    Dim alngVector1() As Long
    Dim astrVector2() As String
    lngScalar = 1
    ReDim alngVector1(0 To 0)
    ReDim astrVector2(0 To 0)

    RaiseEvent Event1(lngScalar, alngVector1)

End Sub

Public Sub FireEvent2()

    Dim lngScalar As Long
    Dim alngVector1() As Long
    Dim astrVector2() As String
    lngScalar = 1
    ReDim alngVector1(0 To 0)
    ReDim astrVector2(0 To 0)

    RaiseEvent Event2(lngScalar, alngVector1, astrVector2)

End Sub

Public Sub FireEvent3()

    Dim lngScalar As Long
    Dim alngVector1() As Long
    Dim astrVector2() As String
    lngScalar = 1
    ReDim alngVector1(0 To 0)
    ReDim astrVector2(0 To 0)

    RaiseEvent Event3(lngScalar, alngVector1, astrVector2)

End Sub

Public Sub FireEvent4()

    Dim lngScalar As Long
    Dim alngVector1() As Long
    Dim astrVector2() As String
    lngScalar = 1
    ReDim alngVector1(0 To 0)
    ReDim astrVector2(0 To 0)

    RaiseEvent Event4(alngVector1, astrVector2, lngScalar)

End Sub

Here is c# clinet code

using System;

namespace InterOops
{
        class Class1
        {
                [STAThread]
                static void Main(string[] args)
                {
                        InterOops.StrangeEvents pServer = new
StrangeEvents();
                        pServer.Event1 += new
__StrangeEvents_Event1EventHandler( OnEvent1 );
                        pServer.Event2 += new
__StrangeEvents_Event2EventHandler( OnEvent2 );
                        pServer.Event3 += new
__StrangeEvents_Event3EventHandler( OnEvent3 );
                        pServer.Event4 += new
__StrangeEvents_Event4EventHandler( OnEvent4 );

                        pServer.FireEvent1();   // OK
                        pServer.FireEvent3();   // OK
                        pServer.FireEvent4();   // OK
                        pServer.FireEvent2();   // Exception

                        Console.ReadLine();
                }

                static void OnEvent1( int Scalar, ref int[] IntVector )
                {
                        Console.WriteLine( "Event 1 Fired" );
                }

                static void OnEvent2( int Scalar, ref int[] IntVector, ref
string[] StrVector )
                {
                        Console.WriteLine( "Event 2 Fired" );
                }

                static void OnEvent3( ref int Scalar, ref int[] IntVector,
ref string[] StrVector )
                {
                        Console.WriteLine( "Event 3 Fired" );
                }

                static void OnEvent4( ref int[] IntVector, ref string[]
StrVector, int Scalar )
                {
                        Console.WriteLine( "Event 4 Fired" );
                }
        }
}

If COM object is referenced in VS then Array should be used instead of int[]
and string[]

I know it sounds very bizarre but I failed to find an explanation for this.

Mikhail

_____________________________________________________________________
This e-mail has been scanned for viruses by the WorldCom Internet Managed Scanning 
Service - powered by MessageLabs. For further information visit http://www.worldcom.com

Reply via email to