本帖最后由 lcmlhs_2005 于 2010-06-25 17:25:57 编辑

解决方案 »

  1.   

    感觉乱乱的!!!!!!!!!!1C++中有
    typedef struct Student
    {
      char name[24];          //这是学生的姓名
      int age;                //这是学生的年龄 
    }StudentInfo;
    大哥 这个StudentInfo代表一个学生更搞不懂的是下面一句,传入一个Student指针,(你传给他内存就能容纳一个学生),返回值也搞不懂,
    int GetStudentInfo(StudentInfo * pStudentInfo);//返回学生结构体个数总之,你的程序乱七八糟
      

  2.   

    不乱的:
    int GetStudentInfo(StudentInfo * pStudentInfo);这个函数是C++提供的动态库Student.dll里的函数,它返回值是学生的个数(也就是学生结构体的个数,如,返回结果为100,则说明有一个学生的信息以结构体的形式存在于这个函数中)
    我现在要做的就是取出这100个学生的姓名!!!可是不知道怎么写代码,在C++里传入指针后,在循环里对指针++就可以取出了,在C#里不会弄!!!
      

  3.   


    private void Form1_Load(object sender, EventArgs e)
            {        }        [DllImport("ddd.dll")]
            extern static int GetStudentInfo(IntPtr pStudentInfo);        [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Size = 28)]
            struct StudentInfo
            {
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24), FieldOffset(0)]
                public string str;
                [FieldOffset(24)]
                public int intId;
            }        public static List<T> MarshalPtrToStructArray<T>(IntPtr p, int count)
            {
                
                List<T> l = new List<T>();
                for (int i = 0; i < count; i++, p = new IntPtr(p.ToInt32() + Marshal.SizeOf(typeof(T))))
                {
                    T t = (T)Marshal.PtrToStructure(p, typeof(T));
                    l.Add(t);
                }
                return l;
            }        private void button1_Click(object sender, EventArgs e)
            {
                IntPtr pStudentInfo = Marshal.AllocHGlobal(280);
                int intCount = GetStudentInfo(pStudentInfo);
                List<StudentInfo> st = MarshalPtrToStructArray<StudentInfo>(pStudentInfo, intCount);
                MessageBox.Show(st[9].str);
            }
      

  4.   

    IntPtr pStudentInfo = Marshal.AllocHGlobal(280);
    int intCount = GetStudentInfo(pStudentInfo);char[] StudentName;
    StudentName=new char[24];for (int i = 0; i < Count; i++)
    {
       List<StudentInfo> st = MarshalPtrToStructArray<StudentInfo>(pStudentInfo, i+24);
    //在这里我要依次取出每个学生的信息
    这里我就把每一个学生的名字依次取出来,符给StudentName数组,这里怎么写呢?
    //st.CopyTo(StudentName,0);这里为什么出错啊?                }
      

  5.   


    List<StudentInfo> st = MarshalPtrToStructArray<StudentInfo>(pStudentInfo, intCount);
    // 这个intCount,是你统共从这个指针里面取出多少条数据,不是当前数据的编号MessageBox.Show(st[9].str);
    这个是最后一条数据
    list都有了,还不会取数据么
      

  6.   

    最天我在现场调试时发现st数组里取出了数据,可是问题是结构里的学生名字name都是乱码,这是为什么呢?
      

  7.   

    我怀疑问题出现在这儿:
    public static List<T> MarshalPtrToStructArray<T>(IntPtr p, int count)
            {
                List<T> l = new List<T>();
                for (int i = 0; i < count; i++, p = new IntPtr(p.ToInt32() + Marshal.SizeOf(typeof(T))))
                {
                    T t = (T)Marshal.PtrToStructure(p, typeof(T));//这里是什么意思啊?取出结构赋给t吗?我怀疑是在这里取学生名字的时候出现乱码的!!!
                    l.Add(t);
                }
                return l;
            }
      

  8.   

    char[] cname;
    cname=new char[24*count];for(int i=0;i<count;i++)
    {
      cname[i]=ref_student.name //这里怎样写呢?我要通过这个循环得到所有结构体里的学生名字????
    }添加一个函数(string GetStr(char[] myChar)):把char[]转换成string,在获取ref_student.name 时调用,将name装换成string
    cname不要定义成char[],定义成string[]
    然后把“ cname[i]=ref_student.name ”改成 cname[i]=GetStr(ref_student.name )。应就可以了
      

  9.   


    [StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Size = 28)]
            struct StudentInfo
            {
                // 看这里,UnmanagedType.ByValTStr 和 CharSet = CharSet.Ansi决定了你取到的字符串具体是由什么类型来的。
                // 你需要做的就是,把这个值和实际的数据匹配上,不明白就每种都试一下。
                [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24), FieldOffset(0)]
                public string str;
                [FieldOffset(24)]
                public int intId;
            }
      

  10.   

    噢,楼上的是说 UnmanagedType.AnsiBStr
            UnmanagedType.ByValArray
            UnmanagedType.ByValTStr
            UnmanagedType.BStr
             ...
    都试一下吗?