解决方案 »
- 导出dataGridView 的数据到EXCEL 中
- 在线坐等.net IIS高手帮忙解决
- VS2008如果关闭自动启用脚本调试功能??
- 给个思路 没做过winform
- winform问题
- C#调用C++DLL出现未处理的“System.AccessViolationException”类型的异常出现在.exe其他信息: 尝试读取或写入受保护的内存
- 急!急!急!获取访问网站的用户的ip
- C#有关编译原理
- 如何让BackgroundImage自动调整图象的大小以适应窗体的改变
- 成功把C代码转换成C#代码的送出120分!!
- 请教一下c#如何读取用空格分开的字符串的文件
- 怎样把整数和小数放在同一个表达式中
typedef struct Student
{
char name[24]; //这是学生的姓名
int age; //这是学生的年龄
}StudentInfo;
大哥 这个StudentInfo代表一个学生更搞不懂的是下面一句,传入一个Student指针,(你传给他内存就能容纳一个学生),返回值也搞不懂,
int GetStudentInfo(StudentInfo * pStudentInfo);//返回学生结构体个数总之,你的程序乱七八糟
int GetStudentInfo(StudentInfo * pStudentInfo);这个函数是C++提供的动态库Student.dll里的函数,它返回值是学生的个数(也就是学生结构体的个数,如,返回结果为100,则说明有一个学生的信息以结构体的形式存在于这个函数中)
我现在要做的就是取出这100个学生的姓名!!!可是不知道怎么写代码,在C++里传入指针后,在循环里对指针++就可以取出了,在C#里不会弄!!!
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);
}
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);这里为什么出错啊? }
List<StudentInfo> st = MarshalPtrToStructArray<StudentInfo>(pStudentInfo, intCount);
// 这个intCount,是你统共从这个指针里面取出多少条数据,不是当前数据的编号MessageBox.Show(st[9].str);
这个是最后一条数据
list都有了,还不会取数据么
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;
}
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 )。应就可以了
[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;
}
UnmanagedType.ByValArray
UnmanagedType.ByValTStr
UnmanagedType.BStr
...
都试一下吗?