Pinvoke from vb.net using structures in call

I am really struggling with the next pinvoke call. I have tried many different ways to do this, but there is still no joy.

The call is made, but I am returning a "Bad parameter" message with hints that something is wrong with the design, since I ran the C ++ example of this code and all the parameters are correct.

I would be so grateful for any help, I have already gone crazy!

in the C ++ header file:

int __stdcall DVSNET_OpenChannel(HANDLE hServer,unsigned long nChannel,DVSNET_CHANNEL_INFO *pChannelInfo,HANDLE *phChannel);

typedef struct tagDVSNET_CHANNEL_INFO
{
    unsigned long lStructSize;
    unsigned long dwStreamNo;
    unsigned long nProtocol;
    HWND          hWndDisplay;
    unsigned long bPlayStart;
    unsigned long dwBackFrameCount;
    unsigned long dwFlag;
} DVSNET_CHANNEL_INFO;

My definitions for import are:

<DllImport("DVSNETClient.dll")> _
Public Shared Function DVSNET_OpenChannel(ByVal hServer As System.IntPtr, ByVal nChannel As UInteger, ByRef pChannelInfo As IntPtr, ByRef phChannel As IntPtr) As Integer
End Function

<StructLayout(LayoutKind.Sequential)> _
Public Structure tagDVSNET_CHANNEL_INFO
    Public lStructSize As UInteger
    Public dwStreamNo As UInteger
    Public nProtocol As UInteger
    Public hWndDisplay As IntPtr
    Public bPlayStart As UInteger
    Public dwBackFrameCount As UInteger
    Public dwFlag As UInteger
End Structure

My code is:

Private Sub OpenChannel()
    Dim intRet As Integer
    Dim ChannelInfo As New tagDVSNET_CHANNEL_INFO

    Dim HWD As New System.IntPtr

    ChannelInfo.lStructSize = System.Runtime.InteropServices.Marshal.SizeOf(ChannelInfo)
    ChannelInfo.nProtocol = 0
    ChannelInfo.dwStreamNo = 0
    ChannelInfo.dwBackFrameCount = 10
    ChannelInfo.hWndDisplay = HWD
    ChannelInfo.bPlayStart = 0 ' dont display
    'Channelinfo.dwFlag = 

    ' Initialize unmanged memory to hold the struct.
    Dim ptr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(ChannelInfo))
    System.Runtime.InteropServices.Marshal.StructureToPtr(ChannelInfo, ptr, True)

    Dim nChannel As UInteger = 1
    intRet = TotemPoleLib.DVSNET_OpenChannel(hServer, nChannel, ptr, hChannel)

    '... snip ...
End Sub

Thank you very much in advance!

+3
source share
1 answer

I don't know the syntax of VB.Net, but if you understand C #, then here are the correct P / Invoke declarations for DVSNET_CHANNEL_INFOand DVSNET_OpenChannel:

[StructLayout(LayoutKind.Sequential)]
struct DVSNET_CHANNEL_INFO
{
    uint   lStructSize;
    uint   dwStreamNo;
    uint   nProtocol;
    IntPtr hWndDisplay;
    uint   bPlayStart;
    uint   dwBackFrameCount;
    uint   dwFlag;
}

static class DVSNETClient
{
    [DllImport("DVSNETClient.dll")]
    public static extern int DVSNET_OpenChannel(
        IntPtr hServer,
        uint nChannel,
        ref DVSNET_CHANNEL_INFO pChannelInfo,
        ref IntPtr phChannel
    );
}

DVSNET_CHANNEL_INFO , DVSNET_OpenChannel ref DVSNET_CHANNEL_INFO; , Marshal. , ChannelInfo.lStructSize Marshal.SizeOf(typeof(DVSNET_CHANNEL_INFO)) DVSNET_OpenChannel. , , DVSNET_CHANNEL_INFO, , , , , .

, , pChannelInfo / phChannel out, ref, DVSNET_OpenChannel. ref , , out, out .

+1

All Articles