Home All Groups Group Topic Archive Search About
Author
7 Jun 2006 4:51 PM
Frustrated
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.dll" 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.dll" 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.currentDataFrameBytes(m_hCamera)
        gch.Alloc(lBufSz)
        Try
            gch = camera.lockdata(m_hCamera, -1)
            gch.Free()
        Catch ex As Exception
            MsgBox(ex.Message)
        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

Author
7 Jun 2006 6:29 PM
Mattias Sjögren
Mike,

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

IntPtr


>(2)  How would I get the data?

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.
Author
7 Jun 2006 6:35 PM
Ken Tucker [MVP]
Hi,

        The function should return an intptr.  You should be able to use
marshal.ptrtostructure or one of the marshal.pointertostring function to get
the data.
http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.marshal_members.aspx

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

Show quoteHide quote
"Frustrated" wrote:

> 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.dll" 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.dll" 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.currentDataFrameBytes(m_hCamera)
>         gch.Alloc(lBufSz)
>         Try
>             gch = camera.lockdata(m_hCamera, -1)
>             gch.Free()
>         Catch ex As Exception
>             MsgBox(ex.Message)
>         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
>
>