Calling a C++ DLL function from VB.NET having structures pointer (RunTime Error : Specified Array was not of the expected type)

Locked Calling a C++ DLL function from VB.NET having structures pointer (RunTime Error : Specified Array was not of the expected type)

  • 2012년 7월 19일 목요일 오후 1:38
     
     

    Hi ,

    I have function in C++ dll which accept structure pointer I want to call this function using dllimport in VB.NET 
    Can anybody help me to do this.

    C++ structure
    struct MyStruct

    {
    int i;
    float j;
    } ;

    EXPORT bool func(struct MyStruct * lpSlot)
    {
        //some code like this

        lpSlot(0).i =10

        lpSlot(0).j =10.5

        lpSlot(1).i =20

        lpSlot(1).j =20.5

        lpSlot(2).i =30    lpSlot(2).j =10.5

    }

    VB.Net :

    Declare Function ParseRackStatus Lib "TestC.dll" Alias "_ParseRackStatus@12" (<MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_ARRAY)> ByRef lpSlot() As MyStruct) As Boolean.

    Public Function ParseRackStatusTest(ByRef lpSlot() As MyStruct) As Boolean
             Return ParseRackStatus(lpSlot)
     End Function


    I am getting Runtime error,when i calling above  function "ParseRackStatusTest".

    RunTime Error :  Specified Array was not of the expected type.


    • 편집됨 Raju Rudraraju 2012년 7월 20일 금요일 오전 6:06
    •  

모든 응답

  • 2012년 7월 19일 목요일 오후 3:33
     
     답변됨 코드 있음


    This should do it:

    Declare Function ParseRackStatus Lib "TestC.dll" Alias "_ParseRackStatus@12" (ByVal lpSlot() As MyStruct) As Boolean

    I don't know how you can have two functions with the same signature. The second function calls itself recursively.

    Don't forget to initialize the array before passing it to the function. According to the C code, it must have at least three items.

    EDIT: Could a moderator please move this to the Interop forum? Thanks.


    Armin


  • 2012년 7월 19일 목요일 오후 3:46
     
     
    Addendum: Safe arrays are "COM style": Default Marshaling for Arrays

    Armin

  • 2012년 7월 20일 금요일 오전 6:01
     
      코드 있음

    HI ,

    Declare Function ParseRackStatus Lib "TestC.dll" Alias "_ParseRackStatus@12" (ByVal lpSlot() As MyStruct) As Boolean

    I tried above function  also .I am getting error message like this "Attempt to read or write protected memory".

    Declare Function ParseRackStatus Lib "TestC.dll" Alias "_ParseRackStatus@12" (<MarshalAs(UnmanagedType.SafeArray, SafeArraySubType:=VarEnum.VT_ARRAY)> ByRef lpSlot() As MyStruct) As Boolean.

    Public Function ParseRackStatusTest(ByRef lpSlot() As MyStruct) As Boolean
             Return ParseRackStatus(lpSlot)
     End Function

    Before calling the ParseRackStatusTest function,Ititilized the array.


    • 편집됨 Raju Rudraraju 2012년 7월 20일 금요일 오전 6:32
    •  
  • 2012년 7월 20일 금요일 오후 12:31
     
      코드 있음

    IMO, the signatures match:

       bool func(struct MyStruct * lpSlot)

       Declare Function ParseRackStatus Lib "TestC.dll" Alias "_ParseRackStatus@12" (ByVal lpSlot() As MyStruct) As Boolean

    If someone sees a difference, please let us know.

    What's missing is the Structure declaration. How does it look like? It should be...:

    Imports System.Runtime.InteropServices

    <StructLayout(LayoutKind.Sequential)> _ Structure MyStruct Public i As Integer Public float As Single End Structure



    Armin


    • 편집됨 Armin Zingler 2012년 7월 20일 금요일 오후 12:31
    •  
  • 2012년 7월 20일 금요일 오후 1:15
     
     

    Hi ,

    I declared like this :

    <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Ansi, Pack:=1)> _
       Structure MyStruct
       <MarshalAs(UnmanagedType.I4)> _
       Dim i As Integer
       <MarshalAs(UnmanagedType.R4)> _
      Dim float As Single
     End Structure


    • 편집됨 Raju Rudraraju 2012년 7월 20일 금요일 오후 1:16
    •  
  • 2012년 7월 20일 금요일 오후 1:28
     
     

    And without pack:=1 ?

    The type is blittable, i.e you don't need to apply further attributes.


    Armin