不好意思刚刚做个测试发贴,请帮忙API里的代码如下:typedef struct PINGPONG_PR
{
unsigned int rgb_address;
unsigned char flag;
} PINGPONG_PR;BOOL CIS_IOControl(DWORD hOpenContext, 
   DWORD dwCode, 
   PBYTE pBufIn, 
   DWORD dwLenIn, 
   PBYTE pBufOut, 
   DWORD dwLenOut, 
   PDWORD pdwActualOut)
{
if(dwCode == 0x50) 
   {     
      RETAILMSG(MSG_EN_1,(_T("GETDATA FUNCTION\r\n")));
      getData(pBufOut);
      return true;
   }else
   {
      return false;
   }
}void getData(U8 *pBufOut)
{
U8 *pData;
PINGPONG_PR prinfo; pData = pBufOut; prinfo.flag = 1;     //记得此处将flag设置为了1
prinfo.rgb_address = rgb_address;
memcpy(pData, &prinfo, sizeof(PINGPONG_PR));

}
我现在想要通过API传递参数,并获得值,先看我的C#代码//定义结构
[StructLayout(LayoutKind.Sequential)]
        public struct BitmapStruct{
            [MarshalAs(UnmanagedType.U4)]
            public uint rgb_address;
            [MarshalAs(UnmanagedType.U4)]
            public uint flag;
        }
        /// <summary>
       /// 获得数据
        /// </summary>
        public bool getData()
        {
              //结构指针方式                BitmapStruct BS = new BitmapStruct();
              int BSSize = Marshal.SizeOf (BS);
              IntPtr BSPtr = Marshal.AllocHGlobal(BSSize);
              Marshal.StructureToPtr(BS, BSPtr, false);
                
                              bool result = DeviceIoControl(this.hComm, this.CAM_IOCTL_GET_LATEST_FRAME, null, 0, BSPtr, BSSize, 0);
                    if (result)//这里已经返回了TRUE
                    {
                        BitmapStruct BSNew = (BitmapStruct)Marshal.PtrToStructure(BSPtr, typeof(BitmapStruct));
                        MessageBox.show(BSNew.flag.ToString()); // 问题就是这里,永远也只返回0 我希望获得的是1
                    }
                    else {
                        int errorCode = Marshal.GetLastWin32Error();
                        Exception e = Marshal.GetExceptionForHR(errorCode);
                        MessageBox.Show(e.ToString());
                    }                    Marshal.FreeHGlobal(BSPtr);            return true;
        }请大家帮忙分析一下,感激不尽

解决方案 »

  1.   

    不好意思,更正版,我想在C#里调用API
    API里的代码如下:typedef struct PINGPONG_PR
    {
    unsigned int rgb_address;
    unsigned char flag;
    } PINGPONG_PR;BOOL CIS_IOControl(DWORD hOpenContext, 
       DWORD dwCode, 
       PBYTE pBufIn, 
       DWORD dwLenIn, 
       PBYTE pBufOut, 
       DWORD dwLenOut, 
       PDWORD pdwActualOut)
    {
    if(dwCode == 0x50) 
       {     
          RETAILMSG(MSG_EN_1,(_T("GETDATA FUNCTION\r\n")));
          getData(pBufOut);
          return true;
       }else
       {
          return false;
       }
    }void getData(U8 *pBufOut)
    {
    U8 *pData;
    PINGPONG_PR prinfo; pData = pBufOut; prinfo.flag = 1;     //记得此处将flag设置为了1
    prinfo.rgb_address = rgb_address;
    memcpy(pData, &prinfo, sizeof(PINGPONG_PR));

    }
    我现在想要通过API传递参数,并获得值,先看我的C#代码private int this.CAM_IOCTL_GET_LATEST_FRAME = 0x50;//定义结构
    [StructLayout(LayoutKind.Sequential)]
            public struct BitmapStruct{
                [MarshalAs(UnmanagedType.U4)]
                public uint rgb_address;
                [MarshalAs(UnmanagedType.U4)]
                public uint flag;
            }
            /// <summary>
           /// 获得数据
            /// </summary>
            public bool getData()
            {
                  //结构指针方式                BitmapStruct BS = new BitmapStruct();
                  int BSSize = Marshal.SizeOf (BS);
                  IntPtr BSPtr = Marshal.AllocHGlobal(BSSize);
                  Marshal.StructureToPtr(BS, BSPtr, false);
                    
                                  bool result = DeviceIoControl(this.hComm, this.CAM_IOCTL_GET_LATEST_FRAME, null, 0, BSPtr, BSSize, 0);
                        if (result)//这里已经返回了TRUE
                        {
                            BitmapStruct BSNew = (BitmapStruct)Marshal.PtrToStructure(BSPtr, typeof(BitmapStruct));
                            MessageBox.show(BSNew.flag.ToString()); // 问题就是这里,永远也只返回0 我希望获得的是1
                        }
                        else {
                            int errorCode = Marshal.GetLastWin32Error();
                            Exception e = Marshal.GetExceptionForHR(errorCode);
                            MessageBox.Show(e.ToString());
                        }                    Marshal.FreeHGlobal(BSPtr);            return true;
            }请大家帮忙分析一下,感激不尽
      

  2.   

    PBYTE是这样定义的typedef BYTE near           *PBYTE;