可以定义的用那个什么Marshas什么的

解决方案 »

  1.   

    Red说的是这样么?
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
                  public RGBCOLOR[] m_Color;
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;/*
     * 产品配置信息定义
     * 请将getProdInfo(config)返回的产品配置信息数据区按照下列结构解析
     typedef struct{
    unsigned char rgbRed;
    unsigned char rgbGreen;
    unsigned char rgbBlue;
    unsigned char rgbReserved;
    }RGBCOLOR;typedef struct tagProdParam{
    char name[40];
    float value;
    }PRODPARAM;typedef struct tagConfigInfo{
    char          m_Product[80]; //产品名称
    long          m_ID;       //产品标识
    RGBCOLOR      m_Color[MAXCOLOR];   //产品色表
    float         m_Value[MAXCOLOR];   //色表阈值
    PRODPARAM     m_Param[MAXPARAM];
    char          m_ProcMod[40];
    char          m_Unit[20];
    char          Reserved[140];
    }CONFIGINFO;
     */
    namespace radarprocessor
    {
        public partial class Form1 : Form
        {
            [StructLayout(LayoutKind.Sequential)]
            public struct RGBCOLOR
            {
                public byte rgbRed;
                public byte rgbGreen;
                public byte rgbBlue;
                public byte rgbReserved;
            };
            [StructLayout(LayoutKind.Sequential)]
            public struct PRODPARAM
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst =40)]
                public string name;
                public float value;
            };
            [StructLayout(LayoutKind.Sequential)]
            public struct CONFIGINFO
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
                public string m_Product; //产品名称
                public long m_ID;       //产品标识
                public RGBCOLOR[] m_Color;   //产品色表 //怎么定义嵌套结构????????xw
                //[MarshalAs(UnmanagedType.by, SizeConst = 80)]
                public float[] m_Value;   //色表阈值
                public PRODPARAM[] m_Param;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 40)]
                public string m_ProcMod;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
                public string m_Unit;
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst =140)]
                public string Reserved;
            };
            public Form1()
            {
                InitializeComponent();
            }        [DllImport("processor.dll", EntryPoint = "initLoader", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern  string initLoader(string path);
            [DllImport("processor.dll", EntryPoint = "clearLoader", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern void clearLoader();
            [DllImport("processor.dll", EntryPoint = "readData", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern void readData(string filename);
            [DllImport("processor.dll", EntryPoint = "setProduct", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern void setProduct(int ID);
            [DllImport("processor.dll", EntryPoint = "setParam", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern void setParam(int ID,float param);
            [DllImport("processor.dll", EntryPoint = "doPreProc", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern void doPreProc(int ID);
            [DllImport("processor.dll", EntryPoint = "doProc", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern void doProc();
            [DllImport("processor.dll", EntryPoint = "getProdData", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern void* getProdData();
            [DllImport("processor.dll", EntryPoint = "getProdDataSize", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern int getProdDataSize();
            [DllImport("processor.dll", EntryPoint = "getProdInfo", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern void* getProdInfo();
            [DllImport("processor.dll", EntryPoint = "hasData", CallingConvention = CallingConvention.StdCall)]
            unsafe public static extern bool hasData();
            //public byte[] data;
            //public byte[] config; 
            unsafe public void* config;
            unsafe public void* data;
            private void btnTest_Click(object sender, EventArgs e)
            {
                int datasize=0;
                int sss;
                bool datastatus = false;
                //IntPtr buffer; 
                unsafe {
                //    CONFIGINFO* infos;
                    //初始化路径
                string ss=initLoader("D:\\熊伟\\radarc#\\bin");
                    //读取数据
                readData("2005053101.07A");
                    //预处理
                doPreProc(0);
                    //设置产品编号
                setProduct(0);
                    //设置产品参数
                setParam(0, 0.0f);
                    //判断是否数据有效
                datastatus = hasData();
                    //处理产品
                    doProc();
                    //获取产品数据区大小
                    datasize = getProdDataSize();
                    //得到产品指针
                    data = getProdData();
                    //得到产品配置信息
                    config=getProdInfo();
                   // sss = (int*)config[0];
                    
                   
                }
               
                clearLoader();
                datastatus = hasData();
                
            }
        }
    }
    编译config为:0x0......
    请问怎么将getProdInfo(config)返回的产品配置信息数据区按照c中的结构解析?
    真心求助,希望得到大家的帮助,谢谢!
      

  3.   

    用unsafe fixed模式可能会解决些问题