C#调用C++动态库结构体函数得到所有结构体信息,旷世难题,高手请进!!!!!!!!!1

lcmlhs_2005 2010-06-25 05:24:43
C++中有
typedef struct Student
{
char name[24];
int age;
}StudentInfo;

int GetStudentInfo(StudentInfo * pStudentInfo);//返回学生结构体个数

上面的函数存在于Student.dll中,下面要在C#里调用Student.dll中的这个函数:

public struct Student
{
public char[] name;
public int age;
}

[DllImport("Student.dll",CharSet = CharSet.Ansi)]
public static extern int(ref Student refStudent);

Student ref_student = new Student();
int count = GetStudentInfo(ref ref_student);//这里取得学生结构体的个数
char[] cname;
cname=new char[24*count];

for(int i=0;i<count;i++)
{
cname[i]=ref_student.name //这里怎样写呢?我要通过这个循环得到所有结构体里的学生名字????
}
...全文
400 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
lcmlhs_2005 2010-07-06
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 kolosi 的回复:]
C# code

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi, Size = 28)]
struct StudentInfo
{
// 看这里,UnmanagedType.ByValTStr 和 CharSet = CharSet.Ansi决定了你取到的字符串具……
[/Quote]

这个怎么老出现 偏移位置处包含一个对象字段,该字段已由一个非对象字段不正确地对齐或重叠。
lcmlhs_2005 2010-06-30
  • 打赏
  • 举报
回复
噢,楼上的是说 UnmanagedType.AnsiBStr
UnmanagedType.ByValArray
UnmanagedType.ByValTStr
UnmanagedType.BStr
...
都试一下吗?
kolosi 2010-06-30
  • 打赏
  • 举报
回复

[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;
}

weixiaodeshui 2010-06-30
  • 打赏
  • 举报
回复
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 )。应就可以了
lcmlhs_2005 2010-06-30
  • 打赏
  • 举报
回复
我怀疑问题出现在这儿:
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;
}
lcmlhs_2005 2010-06-30
  • 打赏
  • 举报
回复
最天我在现场调试时发现st数组里取出了数据,可是问题是结构里的学生名字name都是乱码,这是为什么呢?
kolosi 2010-06-29
  • 打赏
  • 举报
回复

List<StudentInfo> st = MarshalPtrToStructArray<StudentInfo>(pStudentInfo, intCount);
// 这个intCount,是你统共从这个指针里面取出多少条数据,不是当前数据的编号。。。

MessageBox.Show(st[9].str);
这个是最后一条数据。。。
list都有了,还不会取数据么。。。
lcmlhs_2005 2010-06-29
  • 打赏
  • 举报
回复
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);这里为什么出错啊?



}
kolosi 2010-06-26
  • 打赏
  • 举报
回复
旷世难题才40分。。
昵称测试 2010-06-26
  • 打赏
  • 举报
回复
顶顶
太短了还!
lcmlhs_2005 2010-06-26
  • 打赏
  • 举报
回复
急切地顶!
lcmlhs_2005 2010-06-26
  • 打赏
  • 举报
回复
顶顶顶顶顶
lcmlhs_2005 2010-06-26
  • 打赏
  • 举报
回复
不乱的:
int GetStudentInfo(StudentInfo * pStudentInfo);这个函数是C++提供的动态库Student.dll里的函数,它返回值是学生的个数(也就是学生结构体的个数,如,返回结果为100,则说明有一个学生的信息以结构体的形式存在于这个函数中)
我现在要做的就是取出这100个学生的姓名!!!可是不知道怎么写代码,在C++里传入指针后,在循环里对指针++就可以取出了,在C#里不会弄!!!
zhu_jiang 2010-06-26
  • 打赏
  • 举报
回复
感觉乱乱的!!!!!!!!!!1

C++中有
typedef struct Student
{
char name[24]; //这是学生的姓名
int age; //这是学生的年龄
}StudentInfo;
大哥 这个StudentInfo代表一个学生

更搞不懂的是下面一句,传入一个Student指针,(你传给他内存就能容纳一个学生),返回值也搞不懂,
int GetStudentInfo(StudentInfo * pStudentInfo);//返回学生结构体个数

总之,你的程序乱七八糟
lcmlhs_2005 2010-06-26
  • 打赏
  • 举报
回复
顶一个
kolosi 2010-06-26
  • 打赏
  • 举报
回复

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);
}
xy325432 2010-06-25
  • 打赏
  • 举报
回复
不会,帮顶。。

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧