CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2003
    Location
    Brazil
    Posts
    2

    Question SAFEARRAY - how to use

    I am trying to write a VB code to use a DCOM through a DLL. The application is based on OPC HDA Server and I am using OPCHDAAuto.dll. I took the code from HDA specification and it is showed bellow:
    Code:
    Sub access_values()
    
    
        Dim AnOPCHDAServer As OPCHDAServer
        Dim ARealOPCHDAServer As String
        Dim ARealOPCNodeName As String
        Dim AnOPCHDAItemCollection As OPCHDAItems
        Dim AnOPCHDAItem As OPCHDAItem
        Dim ClientHandles(100) As Long
        Dim AnOPCHDAItemIDs(100) As String
        
        Dim AnOPCHDAItemServerHandles(10) As Long
        Dim AnOPCHDAItemServerErrors(10) As Long
        Dim AddItemCount As Long
        Dim i As Integer
        
        Set AnOPCHDAServer = New OPCHDAServer
        ARealOPCHDAServer = "HDATestSvr"
        ARealOPCNodeName = "localhost"
        AnOPCHDAServer.Connect ARealOPCHDAServer, ARealOPCNodeName
        Set AnOPCHDAItemCollection = AnOPCHDAServer.OPCHDAItems
    
    
        'Dim AnOPCHDAItemIDs() As String
        'Dim AnOPCHDAItemServerHandles As Long
        'Dim AnOPCHDAItemServerErrors As Long
        AddItemCount = 2
        For i = 1 To AddItemCount
            ClientHandles(i) = i
            AnOPCHDAItemIDs(i) = "A.A.A"
        Next i
        AnOPCHDAItemIDs(2) = "A.A.B"
        AnOPCHDAItemCollection.AddItems AddItemCount, _
                                        AnOPCHDAItemIDs, _
                                        ClientHandles, _
                                        AnOPCHDAItemServerHandles, _
                                        AnOPCHDAItemServerErrors
    ' add code to process any errors that are returned from 'the method, individual errors are reported in the Errors array
    
    
    
    End Sub

    The declaration, in the IDL file, of AddItems method of the IOPCHDAItems interface is :
    Code:
            [helpstring("Adds OPCHDAItem objects to the collection")]
            HRESULT AddItems(
                [in]    LONG              NumItems,
                [in]    SAFEARRAY(BSTR) * ItemIDs,
                [in]    SAFEARRAY(LONG) * ClientHandles,
                [out]   SAFEARRAY(LONG) * ServerHandles,
                [out]   SAFEARRAY(LONG) * Errors);
    and in VB is:

    AddItems (NumItems As Long, ItemIDs() As String, ClientHandles() As Long, ByRef ServerHandles() As Long, ByRef Errors() As Long)

    When I try to run it I get the message Type mismatch.

    Someone could help me on that issue?

    I appreciate any help.
    Last edited by Shuja Ali; March 19th, 2009 at 02:24 PM. Reason: Added code tags

  2. #2
    Join Date
    Apr 2003
    Posts
    1,755

    Re: SAFEARRAY - how to use

    I'm not sure how you call the function by I believe you must be passing fixed arrays in ServerHandles and Errors. Since these 2 parameters are "out" (Byref) parameters, the runtime engine checks if the parameter is exactly the type that is needed. Dynamic arrays and fixed arrays are different and so type mismatch is generated. Your code should be like this
    Code:
    Dim arrServerHandles() As Long  ' do not use Dim arrServerHandles(100) As Long
    Dim arrErrors() As Long         ' do not use Dim arrErrors(100) As Long
    
    ...
    
    Call <object>.AddItems(<param1>, <param2>, <param3>, arrServerHandles, arrErrors)
    Hope it will help you
    Last edited by rxbagain; March 19th, 2009 at 12:03 AM.

  3. #3
    Join Date
    Dec 2003
    Location
    Brazil
    Posts
    2

    Thumbs up Re: SAFEARRAY - how to use

    Thank for your help. The piece of code I wrote is working fine now.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured