如何在C#中定义C++中的结构体?

时间:2021-08-07 04:14:49
typedef struct _SetInhibitItem  
{  
char acCountry[3]; // 国家信息,人民币为”RM”.  
double iValue; // 钞票面额,以元为单位   
char cBanknoteType; // 版本信息,可能为’A’,’B’,…等。  
unsigned char ucWay; // 吸钞方向,可取值1至4。  
unsigned int uiLabelOption; // 指定操作的钞票范围  
unsigned char ucOperateType; // 操作方式,0禁用,1激活  
}tSetInhibitItem;
---------------------------------------------------------------------------
typedef struct _SetInhibitList  
{  
int iNumOfItems; // 以下参数psSetInhibitItem的数目  
tSetInhibitItem *psSetInhibitItem; // 禁用信息  
}tSetInhibitList;  
---------------------------------------------------------------------------
函数名称:
BNA_ SetPrevalue(tSetInhibitList *p_psSetInhibitList, tDevReturn *p_psStatus)
输入参数:
  p_psSetInhibitList 设置钞票可用信息结构体
我想问的是如何在C#中定义要求的结构体?

7 个解决方案

#1



struct tSetInhibitItem
{  
byte acCountry[3]; // 国家信息,人民币为”RM”.  
double iValue; // 钞票面额,以元为单位  
byte cBanknoteType; // 版本信息,可能为’A’,’B’,…等。  
byte ucWay; // 吸钞方向,可取值1至4。  
uint uiLabelOption; // 指定操作的钞票范围  
byte ucOperateType; // 操作方式,0禁用,1激活  
}

#2



struct tSetInhibitList
{  
int iNumOfItems; // 以下参数psSetInhibitItem的数目  
IntPtr psSetInhibitItem; // 禁用信息  
}

#3



    tSetInhibitList setInhibitList;

    IntPtr p_psSetInhibitList = Marshal.AllocHGlobal(Marshal.SizeOf(setInhibitList));  
     
    Marshal.StructureToPtr(setInhibitList, p_psSetInhibitList , true);  
     
    //在此发送intptr指针给目的方  
    SetPrevalue(psSetInhibitList , p_psStatus);
     
    Marshal.FreeHGlobal(psSetInhibitList );//释放分配的非托管内存。 

#4


对了我如何给这个IntPtr psSetInhibitItem; // 禁用信息  赋值呢?望指教

#5


引用 1 楼 lihanbing 的回复:
C# code

struct tSetInhibitItem
{  
byte acCountry[3]; // 国家信息,人民币为”RM”.  
double iValue; // 钞票面额,以元为单位  
byte cBanknoteType; // 版本信息,可能为’A’,’B’,…等。  
byte ucWay; // 吸钞方向,可取值1至4。  
uint uiLabe……

+1

#6


    public struct _SetInhibitItem
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public byte[] acCountry;
        [MarshalAs(UnmanagedType.U4)]
        public Double iValue;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] cBanknoteType;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] ucWay;
        [MarshalAs(UnmanagedType.U4)]
        public Int32 uiLabelOption;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] ucOperateType;
    };

    public struct _SetInhibitList
    {
        [MarshalAs(UnmanagedType.U4)]
        public Int32 iNumOfItems;
        [MarshalAs(UnmanagedType.LPStruct)]
        public IntPtr psSetInhibitItem;
    };
---------------------------------------------------------------------------------   
private void btn2_Click(object sender, EventArgs e)
        {
            _SetInhibitList setInhibitList=new _SetInhibitList();
            setInhibitList.iNumOfItems = 6;
            IntPtr _set = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(_SetInhibitList)));
            //setInhibitList.psSetInhibitItem = _set;

            //IntPtr p_psSetInhibitList = Marshal.AllocHGlobal(Marshal.SizeOf(_set));

            //Marshal.StructureToPtr(setInhibitList, p_psSetInhibitList, true);

            tDevReturn[] devReturn = new tDevReturn[8];
            for (int i = 0; i < devReturn.Length; i++)
            {
                devReturn[i] = new tDevReturn();
                devReturn[i].iHandle = 0;
                devReturn[i].iLogicCode = 0;
                devReturn[i].iPhyCode = 0;
                devReturn[i].iType = 0;
                devReturn[i].acDevReturn = new byte[128];
                devReturn[i].acReserve = new byte[128];
            }

            IntPtr[] ptArray = new IntPtr[1];
            ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(tDevReturn)) * 10);
            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(tDevReturn)));
            Marshal.Copy(ptArray, 0, pt, 1);
            //在此发送intptr指针给目的方     
            int count = BNA_SetPrevalue(_set, pt);
            if (count != 0)
            {
                MessageBox.Show("IO初始化失败!");
            }
            if (count == 0)
            {
                MessageBox.Show("成功!");
            }
            this.txt1.Text = count.ToString();
            for (int j = 0; j < 8; j++)
            {
                devReturn[j] = (tDevReturn)Marshal.PtrToStructure((IntPtr)((UInt32)pt + j * Marshal.SizeOf(typeof(tDevReturn))), typeof(tDevReturn));

                string ll = System.Text.Encoding.GetEncoding("gb2312").GetString(devReturn[j].acDevReturn);

                this.txt2.Text += " " + ll;// devReturn[j].acDevReturn.ToString();

                ll = System.Text.Encoding.Default.GetString(devReturn[j].acReserve);
                this.txt3.Text += " " + ll;
                this.txt4.Text += " " + devReturn[j].iLogicCode.ToString();
                this.txt5.Text += " " + devReturn[j].iPhyCode.ToString();
                this.txt6.Text += " " + devReturn[j].iHandle.ToString();
                this.txt7.Text += " " + devReturn[j].iType.ToString();

            }
            Marshal.FreeHGlobal(_set);//释放分配的非托管内存。
        }

    }

运行代码后报:类型“WindowsFormsApplication1._SetInhibitList”不能作为非托管结构进行封送处理;无法计算有意义的大小或偏移量。
能帮我看看吗?

#7


  [MarshalAs(UnmanagedType.U4)]
  public Double iValue;

Double应该用R8

#1



struct tSetInhibitItem
{  
byte acCountry[3]; // 国家信息,人民币为”RM”.  
double iValue; // 钞票面额,以元为单位  
byte cBanknoteType; // 版本信息,可能为’A’,’B’,…等。  
byte ucWay; // 吸钞方向,可取值1至4。  
uint uiLabelOption; // 指定操作的钞票范围  
byte ucOperateType; // 操作方式,0禁用,1激活  
}

#2



struct tSetInhibitList
{  
int iNumOfItems; // 以下参数psSetInhibitItem的数目  
IntPtr psSetInhibitItem; // 禁用信息  
}

#3



    tSetInhibitList setInhibitList;

    IntPtr p_psSetInhibitList = Marshal.AllocHGlobal(Marshal.SizeOf(setInhibitList));  
     
    Marshal.StructureToPtr(setInhibitList, p_psSetInhibitList , true);  
     
    //在此发送intptr指针给目的方  
    SetPrevalue(psSetInhibitList , p_psStatus);
     
    Marshal.FreeHGlobal(psSetInhibitList );//释放分配的非托管内存。 

#4


对了我如何给这个IntPtr psSetInhibitItem; // 禁用信息  赋值呢?望指教

#5


引用 1 楼 lihanbing 的回复:
C# code

struct tSetInhibitItem
{  
byte acCountry[3]; // 国家信息,人民币为”RM”.  
double iValue; // 钞票面额,以元为单位  
byte cBanknoteType; // 版本信息,可能为’A’,’B’,…等。  
byte ucWay; // 吸钞方向,可取值1至4。  
uint uiLabe……

+1

#6


    public struct _SetInhibitItem
    {
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
        public byte[] acCountry;
        [MarshalAs(UnmanagedType.U4)]
        public Double iValue;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] cBanknoteType;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] ucWay;
        [MarshalAs(UnmanagedType.U4)]
        public Int32 uiLabelOption;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
        public byte[] ucOperateType;
    };

    public struct _SetInhibitList
    {
        [MarshalAs(UnmanagedType.U4)]
        public Int32 iNumOfItems;
        [MarshalAs(UnmanagedType.LPStruct)]
        public IntPtr psSetInhibitItem;
    };
---------------------------------------------------------------------------------   
private void btn2_Click(object sender, EventArgs e)
        {
            _SetInhibitList setInhibitList=new _SetInhibitList();
            setInhibitList.iNumOfItems = 6;
            IntPtr _set = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(_SetInhibitList)));
            //setInhibitList.psSetInhibitItem = _set;

            //IntPtr p_psSetInhibitList = Marshal.AllocHGlobal(Marshal.SizeOf(_set));

            //Marshal.StructureToPtr(setInhibitList, p_psSetInhibitList, true);

            tDevReturn[] devReturn = new tDevReturn[8];
            for (int i = 0; i < devReturn.Length; i++)
            {
                devReturn[i] = new tDevReturn();
                devReturn[i].iHandle = 0;
                devReturn[i].iLogicCode = 0;
                devReturn[i].iPhyCode = 0;
                devReturn[i].iType = 0;
                devReturn[i].acDevReturn = new byte[128];
                devReturn[i].acReserve = new byte[128];
            }

            IntPtr[] ptArray = new IntPtr[1];
            ptArray[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(tDevReturn)) * 10);
            IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(tDevReturn)));
            Marshal.Copy(ptArray, 0, pt, 1);
            //在此发送intptr指针给目的方     
            int count = BNA_SetPrevalue(_set, pt);
            if (count != 0)
            {
                MessageBox.Show("IO初始化失败!");
            }
            if (count == 0)
            {
                MessageBox.Show("成功!");
            }
            this.txt1.Text = count.ToString();
            for (int j = 0; j < 8; j++)
            {
                devReturn[j] = (tDevReturn)Marshal.PtrToStructure((IntPtr)((UInt32)pt + j * Marshal.SizeOf(typeof(tDevReturn))), typeof(tDevReturn));

                string ll = System.Text.Encoding.GetEncoding("gb2312").GetString(devReturn[j].acDevReturn);

                this.txt2.Text += " " + ll;// devReturn[j].acDevReturn.ToString();

                ll = System.Text.Encoding.Default.GetString(devReturn[j].acReserve);
                this.txt3.Text += " " + ll;
                this.txt4.Text += " " + devReturn[j].iLogicCode.ToString();
                this.txt5.Text += " " + devReturn[j].iPhyCode.ToString();
                this.txt6.Text += " " + devReturn[j].iHandle.ToString();
                this.txt7.Text += " " + devReturn[j].iType.ToString();

            }
            Marshal.FreeHGlobal(_set);//释放分配的非托管内存。
        }

    }

运行代码后报:类型“WindowsFormsApplication1._SetInhibitList”不能作为非托管结构进行封送处理;无法计算有意义的大小或偏移量。
能帮我看看吗?

#7


  [MarshalAs(UnmanagedType.U4)]
  public Double iValue;

Double应该用R8