本人手里有一设备,有SDK,有VC的DEMO程序,现欲整合到C#中去,在设备头文件中有这样的申明:
BOOL __declspec(dllimport) bAPI4_GetImage(BYTE *picture, int timeout, int iResolution, int* piWidth, int* piHeight);
本人是这样在C#中声明的:
[DllImport("HM130SDK4_SS500A_HMFV.dll", CallingConvention = CallingConvention.StdCall)]
public unsafe static extern bool bAPI4_GetImage(out IntPtr picture, int timeout, int iResolution, out IntPtr piWidth, out IntPtr piHeight);问题在于我调用该函数的时候,参数不知道怎么填,比如:
byte[] m_pPicture = new byte[280 * 320];
IntPtr IntPtrPicture = &m_pPicture;这里报错:无法获得托管类型地址大小,或无法申明指向它的指针

解决方案 »

  1.   


    [DllImport("HM130SDK4_SS500A_HMFV.dll")] 
    static extern bool bAPI4_GetImage(byte[] picture, int timeout, int iResolution, ref int piWidth, ref int piHeight); 
      

  2.   


    感谢楼上的回答,可是,对方要求的是给一个指针啊,而不是一个byte[]数组
      

  3.   


    unsafe可以使用嘛,就是非托管方式
      

  4.   


    byte[] will pass in a pointer.Most likely the caller is responsible for creating a buffer. So you create a byte array and pass in the buffer pointer.
    int width = 320;
    int height = 280;
    byte[] picture = new byte[ width * height ];
    bool succeeded = bAPI4_GetImage( picture, timeout, iResolution, ref width, ref height);
      

  5.   

    兄弟,你是对的,但我不明白 c++头文件中描述BOOL __declspec(dllimport) bAPI4_GetImage(BYTE *picture, int timeout, int iResolution, int* piWidth, int* piHeight); *picture也是一个指针,并且会在函数过程中改变值,为什么不用ref申明和使用?
      

  6.   

    picture也是一个指针,并且会在函数过程中改变值,为什么不用ref申明和使用? byte[] picture本身就是按引用传递(类似C++中传递一个指针),在函数过程中数组内容被改变并不奇怪。
    ref byte[] picture则类似C++中的指针的地址(指针的指针),可以用来传出一个新的数组。