.net互操作最麻烦,代码看上去也会复杂。如果能使用vc互操作封装一个clr的dll供调用最好。
参考:http://msdn.microsoft.com/zh-cn/library/ektebyzx.aspx

解决方案 »

  1.   

    delegate void StreamCallback(int handle, int iStreamType, IntPtr data, int size, IntPtr pUser);
    extern int Plat_VSS_PlayVideo(int iUserHandle, int iCameraID, long hWnd, StreamCallback fStreamCallback, IntPtr pUser);
      

  2.   

    +1函数指针在C#里用委托,使用的时候            IntPtr P = new IntPtr();
                Marshal.StructureToPtr(new StreamCallback(Callback).Target, P, true);
               int  n =  Plat_VSS_PlayVideo(1, 2,3,P,4);        void Callback(int handle, int iStreamType, IntPtr data, int size, IntPtr pUser)
            {
                // ...
            }
      

  3.   

    class Program
    {
    delegate void StreamCallbackHandler(int handle, int iStreamType, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] byte[] data, int size, IntPtr pUser);
    [DllImport(dllPath)]
    private static extern int Plat_VSS_PlayVideo(int iUserHandle, int iCameraID, int hWnd, StreamCallbackHandler fStreamCallback, IntPtr pUser); class PlayVideoData
    {
    public int intValue;
    public string stringValue;
    }
    private static void TEST0_Plat_VSS_PlayVideo()
    {
    var pvc = GCHandle.Alloc(new PlayVideoData
    {
    intValue = 1,
    stringValue = "MyString",
    });
    Plat_VSS_PlayVideo(0, 1, 2, (int handle, int iStreamType, byte[] data, int size, IntPtr pUser) =>
    {
    var pvd = (PlayVideoData)GCHandle.FromIntPtr(pUser).Target;
    System.Diagnostics.Debug.WriteLine(string.Format("intValue = {0},", pvd.intValue));
    System.Diagnostics.Debug.WriteLine(string.Format("stringValue = {0},", pvd.stringValue));
    System.Diagnostics.Debug.WriteLine(string.Format("handler = {0},", handle));
    System.Diagnostics.Debug.WriteLine(string.Format("iStreamType = {0},", iStreamType));
    System.Diagnostics.Debug.WriteLine(string.Format("data = {{{0}}}.", string.Join(",", data.Select(i => i.ToString("X02")))));
    }, GCHandle.ToIntPtr(pvc));
    pvc.Free();
    } class PlayVideoCallback
    {
    public void StreamCallback(int handle, int iStreamType, byte[] data)
    {
    System.Diagnostics.Debug.WriteLine(string.Format("handler = {0},", handle));
    System.Diagnostics.Debug.WriteLine(string.Format("iStreamType = {0},", iStreamType));
    System.Diagnostics.Debug.WriteLine(string.Format("data = {{{0}}}.", string.Join(",", data.Select(i => i.ToString("X02")))));
    }
    } private static void TEST1_Plat_VSS_PlayVideo()
    {
    var pvc = GCHandle.Alloc(new PlayVideoCallback());
    Plat_VSS_PlayVideo(0, 1, 2, (int handle, int iStreamType, byte[] data, int size, IntPtr pUser) =>
    {
    ((PlayVideoCallback)GCHandle.FromIntPtr(pUser).Target).StreamCallback(handle, iStreamType, data);
    }, GCHandle.ToIntPtr(pvc));
    pvc.Free();
    } class PlayVideo
    {
    public void StreamCallback(int handle, int iStreamType, byte[] data)
    {
    System.Diagnostics.Debug.WriteLine("PlayVideo callback:");
    System.Diagnostics.Debug.WriteLine(string.Format("handler = {0},", handle));
    System.Diagnostics.Debug.WriteLine(string.Format("iStreamType = {0},", iStreamType));
    System.Diagnostics.Debug.WriteLine(string.Format("data = {{{0}}}.", string.Join(",", data.Select(i => i.ToString("X02")))));
    }
    public void TEST_Plat_VSS_PlayVideo()
    {
    var pvc = GCHandle.Alloc(this);
    Plat_VSS_PlayVideo(0, 1, 2, (int handle, int iStreamType, byte[] data, int size, IntPtr pUser) =>
    {
    ((PlayVideo)GCHandle.FromIntPtr(pUser).Target).StreamCallback(handle, iStreamType, data);
    }, GCHandle.ToIntPtr(pvc));
    pvc.Free();
    }
    } private static void TEST2_Plat_VSS_PlayVideo()
    {
    new PlayVideo().TEST_Plat_VSS_PlayVideo();
    }
    }