本帖最后由 tpu01yzx 于 2010-04-10 20:06:03 编辑

解决方案 »

  1.   

    [code=C#]            IntPtr ii = Marshal.AllocHGlobal(bsize);
                FillBuffer(ii, bsize,255);
                Marshal.FreeHGlobal(ii);code]这样调用
      

  2.   

                IntPtr ii = Marshal.AllocHGlobal(bsize);
                FillBuffer(ii, bsize,255);
                Marshal.FreeHGlobal(ii);
      

  3.   

    C++...
    你声明dll函数时调用规则填的什么?
    DllImport默认的是WinApi (StdCall)
    而VC++不明确指出的话默认的就是cdecl.
    如果声明错了除了无参数的函数外肯定是要出错的.
      

  4.   

    另外建议用fixed语句替换GCHandle.
      

  5.   

    试过以下三种方法了:
    //IntPtr ubuffer = Win32.GlobalAlloc(Win32.GMEMFlag.GMEM_FIXED | Win32.GMEMFlag.GMEM_ZEROINIT, bsize);
    //IntPtr ubuffer = Marshal.AllocHGlobal(bsize);
    GCHandle hbuffer = GCHandle.Alloc(mbuffer, GCHandleType.Pinned);
      

  6.   


    那就是DLL导出的方式不是StdCall了,是cdecl,你既然有DLL源码,调试DLL的时候就会发现DLL中函数得到的参数恰好是倒序的
      

  7.   

    void *buffer可以直接写为byte[] buffer的,不用什么IntPtr
      

  8.   

    改为WINAPI就出现这样的错误:
    无法在 DLL“DynamicDLL.dll”中找到名为“FillBuffer”的入口点。
      

  9.   

    我没dll的源码,我只是自己写了一个做测试。
      

  10.   

    改为:
     // 保存快照数据到相应的缓冲区
            [DllImport("Sa7134Capture.dll", EntryPoint = "VCASaveBitsToBuf")]
            //public static extern bool VCASaveBitsToBuf(Int32 dwCard, IntPtr pDestBuf, ref Int32 dwWidth, ref Int32 dwHeight);
            public static extern bool VCASaveBitsToBuf(Int32 dwCard, byte[] pDestBuf, ref Int32 dwWidth, ref Int32 dwHeight);
    调用这样写:
    byte[] mbuffer = new byte[bsize];
    ret = VideoAPI.VCASaveBitsToBuf(frm.DevCur, mbuffer, ref Width, ref Height);仍然出现同样的错误。
      

  11.   

            [DllImport("Sa7134Capture.dll", EntryPoint = "VCASaveBitsToBuf",CallingConvention=CallingConvention.Cdecl)]
            //public static extern bool VCASaveBitsToBuf(Int32 dwCard, IntPtr pDestBuf, ref Int32 dwWidth, ref Int32 dwHeight);
            public static extern bool VCASaveBitsToBuf(Int32 dwCard, byte[] pDestBuf, ref Int32 dwWidth, ref Int32 dwHeight);
    这样才是Cdecl的,你有这样试过没?
      

  12.   

    测试中……
    我是这样写的:
    byte[] mbuffer = new byte[bsize];
    unsafe
    {
       fixed (byte* ubuffer = &mbuffer[0])
       {
          ret = VideoAPI.VCASaveBitsToBuf(frm.DevCur, ubuffer, ref Width, ref Height);
        }                            
     }
    希望能通过……都快要绝望了……
      

  13.   

    VCASaveBitsToBuf的c++原型是什么?
      

  14.   

    BOOL  WINAPI VCASaveBitsToBuf( DWORD dwCard, PVOID pDestBuf, DWORD& dwWidth, DWORD& dwHeight );
      

  15.   

    C++我是这样做的:DWORD dwWidth = dlgVC->m_dwWidth;
    DWORD dwHeight = dlgVC->m_dwHeight;
    unsigned char *pbuffer = new unsigned char[dwWidth * dwHeight * 3];//RGB24
    ret = VCASaveBitsToBuf(dlgVC->m_iCard, pbuffer,dwWidth, dwHeight);
      

  16.   


    // 保存快照数据到相应的缓冲区
            [DllImport("Sa7134Capture.dll", EntryPoint = "VCASaveBitsToBuf", CallingConvention=CallingConvention.Cdecl)]        
            public static extern bool VCASaveBitsToBuf(Int32 dwCard, IntPtr pDestBuf, ref Int32 dwWidth, ref Int32 dwHeight);
    还是同样的错误。
      

  17.   

    确定给pDestBuf的空间足够大吗?
      

  18.   

    [DllImport("Sa7134Capture.dll", EntryPoint = "VCASaveBitsToBuf")]        
    public static extern bool VCASaveBitsToBuf(int dwCard, byte[] pDestBuf, ref int dwWidth, ref int dwHeight);
    试试.
    上面注明WINAPI了,使用默认的StdCall
      

  19.   

    如果空间不够的话应该会返回错误吧?参数里的Width和Height已经指定了这个空间的大小。在C++下也是分配这样的空间的。
      

  20.   

    [DllImport("Sa7134Capture.dll", EntryPoint = "VCASaveBitsToBuf")]        
    public static extern bool VCASaveBitsToBuf(uint dwCard, byte[] pDestBuf, ref uint dwWidth, ref uint dwHeight);
    换成无符号的调试时你将dwWidth,dwHeight都设为1,看pDestBuf的长度要为多少才不报错?确定下是不是要为3*1*1?