Unmanaged C++ Void*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Frustrated

    #1

    Unmanaged C++ Void*

    I have a C++ DLL that I need to access from VB.NET. The function I am
    trying to call is declared like this:

    void* PASCAL iidc_lockdata( HIIDC, long frame = -1 )

    This function returns a pointer to unsigned chars

    I am able to find the size of the array out. It is usually 128800 to
    use as an example if it helps.

    I have declared the interface for my VB.NET as follows:

    Private Declare Function _iidc_lockdata Lib "iidcapi_SONY.d ll" Alias
    "#37" (ByVal HIIDC As Int32, ByVal frame As Int32) As
    System.Runtime. InteropServices .GCHandle

    and also I have tried it as

    Private Declare Function _iidc_lockdata Lib "iidcapi_SONY.d ll" Alias
    "#37" (ByVal HIIDC As Int32, ByVal frame As Int32) As IntPtr

    (1) My first question is, how do I declare the function, should I use
    GCHandle or IntPtr or something else?

    As far as calling the function goes, I have tried this:

    Dim gch As System.Runtime. InteropServices .GCHandle
    gch = New System.Runtime. InteropServices .GCHandle
    Dim lBufSz As Int32 = camera.currentD ataFrameBytes(m _hCamera)
    gch.Alloc(lBufS z)
    Try
    gch = camera.lockdata (m_hCamera, -1)
    gch.Free()
    Catch ex As Exception
    MsgBox(ex.Messa ge)
    End Try

    FYI the function camera.lockdata is a mapped function and it is as
    follows:

    Public Function lockdata(ByVal HIIDC As Int32, ByVal frame As Int32)
    As System.Runtime. InteropServices .GCHandle
    Return _iidc_lockdata( HIIDC, frame)
    End Function


    and I get "Object reference not set to an instance of an object"

    (2) How would I get the data?

    I have been really struggling with this.

    Thanks,
    Mike Dixon

  • Mattias Sjögren

    #2
    Re: Unmanaged C++ Void*

    Mike,
    [color=blue]
    >(1) My first question is, how do I declare the function, should I use
    >GCHandle or IntPtr or something else?[/color]

    IntPtr

    [color=blue]
    >(2) How would I get the data?[/color]

    System.Runtime. InteropServices .Marshal.Copy



    Mattias

    --
    Mattias Sjögren [C# MVP] mattias @ mvps.org
    http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
    Please reply only to the newsgroup.

    Comment

    • Ken Tucker [MVP]

      #3
      RE: Unmanaged C++ Void*

      Hi,

      The function should return an intptr. You should be able to use
      marshal.ptrtost ructure or one of the marshal.pointer tostring function to get
      the data.

      http://msdn2.microsoft.com/en-us/lib...l_members.aspx

      Ken
      ---------------

      "Frustrated " wrote:
      [color=blue]
      > I have a C++ DLL that I need to access from VB.NET. The function I am
      > trying to call is declared like this:
      >
      > void* PASCAL iidc_lockdata( HIIDC, long frame = -1 )
      >
      > This function returns a pointer to unsigned chars
      >
      > I am able to find the size of the array out. It is usually 128800 to
      > use as an example if it helps.
      >
      > I have declared the interface for my VB.NET as follows:
      >
      > Private Declare Function _iidc_lockdata Lib "iidcapi_SONY.d ll" Alias
      > "#37" (ByVal HIIDC As Int32, ByVal frame As Int32) As
      > System.Runtime. InteropServices .GCHandle
      >
      > and also I have tried it as
      >
      > Private Declare Function _iidc_lockdata Lib "iidcapi_SONY.d ll" Alias
      > "#37" (ByVal HIIDC As Int32, ByVal frame As Int32) As IntPtr
      >
      > (1) My first question is, how do I declare the function, should I use
      > GCHandle or IntPtr or something else?
      >
      > As far as calling the function goes, I have tried this:
      >
      > Dim gch As System.Runtime. InteropServices .GCHandle
      > gch = New System.Runtime. InteropServices .GCHandle
      > Dim lBufSz As Int32 = camera.currentD ataFrameBytes(m _hCamera)
      > gch.Alloc(lBufS z)
      > Try
      > gch = camera.lockdata (m_hCamera, -1)
      > gch.Free()
      > Catch ex As Exception
      > MsgBox(ex.Messa ge)
      > End Try
      >
      > FYI the function camera.lockdata is a mapped function and it is as
      > follows:
      >
      > Public Function lockdata(ByVal HIIDC As Int32, ByVal frame As Int32)
      > As System.Runtime. InteropServices .GCHandle
      > Return _iidc_lockdata( HIIDC, frame)
      > End Function
      >
      >
      > and I get "Object reference not set to an instance of an object"
      >
      > (2) How would I get the data?
      >
      > I have been really struggling with this.
      >
      > Thanks,
      > Mike Dixon
      >
      >[/color]

      Comment

      Working...