c ++到vb.net,回调函数的问题

时间:2010-03-15 21:16:11

标签: c++ vb.net interop callback

我在这里很难找到解决问题的方法。

我正在尝试将客户端API功能从C ++转换为VB.NET, 我认为回调函数存在一些问题。

部分C ++代码:

typedef struct {

BYTE    m_bRemoteChannel;
BYTE    m_bSendMode;
BYTE    m_nImgFormat;  // =0 cif ; = 1 qcif
char    *m_sIPAddress;
char    *m_sUserName;
char    *m_sUserPassword;
BOOL    m_bUserCheck;
HWND    m_hShowVideo;

} CLIENT_VIDEOINFO,* PCLIENT_VIDEOINFO;

CPLAYER_API LONG __stdcall MP4_ClientStart(PCLIENT_VIDEOINFO pClientinfo,void(CALLBACK * ReadDataCallBack)(DWORD nPort,UCHAR * pPacketBuffer,DWORD nPacketSize));

void CALLBACK ReadDataCallBack(DWORD nPort,UCHAR * pPacketBuffer,DWORD nPacketSize)

{     TRACE( “%d \ n” 个,nPacketSize); }

    .....
aa5.m_sUserName = "123";
aa5.m_sUserPassword="w";
aa5.m_bUserCheck = TRUE;

MP4_ClientSetTTL(64);
nn1 = MP4_ClientStart(&aa5,ReadDataCallBack);
if (nn1 == -1)
{
    MessageBox("error");
    return;
}

SDK说明:

  1. MP4_ClientStart
  2. 此功能启动连接。通话的格式是:

    LONG __stdcall MP4_ClientStart(PCLIENT_VIDEOINFO pClientinfo, void(* ReadDataCallBack)(DWORD nChannel,UCHAR * pPacketBuffer,DWORD nPacketSize))

    参数 pClientinfo保存信息。这种联系。

    nChannel持有卡片频道。

    pPacketBuffer保存指向接收缓冲区的指针。

    nPacketSize保存接收缓冲区的长度。

    返回值 如果函数成功,则返回值是此连接的上下文。 如果函数失败,则返回值为-1。

    说明

    typedef struct {

    BYTE m_bRemoteChannel;

    BYTE m_bSendMode;

    BYTE m_bImgFormat;

    char * m_sIPAddress;

    char * m_sUserName;

    char * m_sUserPassword;

    BOOL m_bUserCheck;

    HWND m_hShowVideo;

    } CLIENT_VIDEOINFO,* PCLIENT_VIDEOINFO;

    m_bRemoteChannel保存客户端想要连接的频道。

    m_bSendMode保持连接的网络模式。

    m_bImgFormat:图像格式,0是主要频道视频,1是子频道视频

    m_sIPAddress保存服务器的IP地址。

    m_sUserName保存用户名。

    m_sUserPassword保存用户的密码。

    m_bUserCheck保存是否发送用户名和密码的值。

    m_hShowVideo保留此视频窗口的句柄。

    如果m_hShowVideo保持为NULL,则客户端只能在没有解码器的情况下进行记录。

    如果m_bUserCheck为FALSE,我们将m_sUserName和m_sUserPassword发送为NULL,否则我们将发送每50个字节。

    m_sIPAddress和m_sUserName的长度必须大于50个字节。

    ReadDataCallBack:当库从服务器接收数据包时,会调用此回调。


    我的VB.Net代码:

    Imports System.Runtime.InteropServices

    Public Class Form1

    Const WM_USER = &H400
    
    Public Structure CLIENT_VIDEOINFO
        Public m_bRemoteChannel As Byte
        Public m_bSendMode As Byte
        Public m_bImgFormat As Byte
        Public m_sIPAddress As String
        Public m_sUserName As String
        Public m_sUserPassword As String
        Public m_bUserCheck As Boolean
        Public m_hShowVideo As Long 'hWnd                      
    End Structure
    
    
    Public Declare Function MP4_ClientSetNetPort Lib "hikclient.dll" (ByVal dServerPort As Integer, ByVal dClientPort As Integer) As Boolean
    Public Declare Function MP4_ClientStartup Lib "hikclient.dll" (ByVal nMessage As UInteger, ByVal hWnd As System.IntPtr) As Boolean
    
    
    <DllImport("hikclient.dll")> Public Shared Function MP4_ClientStart(ByVal Clientinfo As CLIENT_VIDEOINFO, ByRef ReadDataCallBack As CALLBACKdel) As Long
    End Function
    
    
    Public Delegate Sub CALLBACKdel(ByVal nPort As Long, <MarshalAs(UnmanagedType.LPArray)> ByRef pPacketBuffer As Byte(), ByVal nPacketSize As Long)
    
    
    Public Sub CALLBACK(ByVal nPort As Long, <MarshalAs(UnmanagedType.LPArray)> ByRef pPacketBuffer As Byte(), ByVal nPacketSize As Long)
    End Sub
    
    
    Public mydel As New CALLBACKdel(AddressOf CALLBACK)
    
    
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim Clientinfo As New CLIENT_VIDEOINFO()
        Clientinfo.m_bRemoteChannel = 0
        Clientinfo.m_bSendMode = 0
        Clientinfo.m_bImgFormat = 0
        Clientinfo.m_sIPAddress = "193.168.1.100"
        Clientinfo.m_sUserName = "1"
        Clientinfo.m_sUserPassword = "a"
        Clientinfo.m_bUserCheck = False
        Clientinfo.m_hShowVideo = Me.Handle 'Nothing
    
    
        MP4_ClientSetNetPort(850, 850)
        MP4_ClientStartup(WM_USER + 1, Me.Handle)
    
        MP4_ClientStart(Clientinfo, mydel)
    End Sub
    

    结束班


    以下是代码的其他一些示例:

    C#

    http://blog.csdn.net/nenith1981/archive/2007/09/17/1787692.aspx

    VB

    ://read.pudn.com/downloads70/sourcecode/graph/250633/MD%E5%AE%A2%E6%88%B7%E7%AB%AF%28VB%29/hikclient.bas__.htm < / p>

    ://read.pudn.com/downloads70/sourcecode/graph/250633/MD%E5%AE%A2%E6%88%B7%E7%AB%AF%28VB%29/Form1.frm__.htm < / p>

    的Delphi

    ://read.pudn.com/downloads91/sourcecode/multimedia/streaming/349759/Delphi_client/Unit1.pas__.htm

1 个答案:

答案 0 :(得分:0)

这不是解决问题的完整解决方案,但我首先将StructLayoutAttribute(Sequential)添加到您的结构中。

Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential)> _
Public Structure CLIENT_VIDEOINFO