[求教]托管代码和非托管代码封送的问题~
C++中定义了一个结构,写了个计算用的函数,做成dll在C#中调用.现在要把这个结构构成的数组作为参数传送,不过参数可以传进去却传不出来...结构体的字段的值并没有被改变....
毕业设计相关,急,百分求教.......
//C++
struct TestStruct
{
float n;
};
extern "C"
_declspec(dllexport)
void test(TestStruct testStruct[3]);
{
TestStruct testStruct[3];
for (int j = 0; j < 3; j++)
{
testStruct[j].n = 88.0f;
}
}
//C#
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
struct TestStruct
{
public float n;
};
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
TestStruct[] cc = new TestStruct[3];
for (int i=0; i<3; i++)
{
cc[i].n = 1;
}
test(cc);
for (int i=0; i<3; i++)
{
System.Console.WriteLine(cc[i].n);
}
}
[DllImport("ppp.dll")]
public extern static void test(TestStruct[] testStruct);
}
}
问题点数:100、回复次数:11Top
1 楼amandag(高歌)回复于 2006-06-01 12:52:08 得分 0
关注Top
2 楼Knight94(愚翁)回复于 2006-06-01 12:56:25 得分 0
Have a try!
[DllImport("ppp.dll")]
public extern static void test([Out]TestStruct[] testStruct);Top
3 楼Knight94(愚翁)回复于 2006-06-01 12:58:46 得分 0
不过你的dll写得有问题,如果传进去的是数组,就不需要局部再定义了,即
void test(TestStruct testStruct[3]);
{
//TestStruct testStruct[3];这句是多余的
for (int j = 0; j < 3; j++)
{
testStruct[j].n = 88.0f;
}
}Top
4 楼fishtroop(楚天孤客)回复于 2006-06-01 13:59:21 得分 0
//TestStruct testStruct[3];这句去掉了....
然后我用了out....出现了下面的异常....
未处理的“System.NullReferenceException”类型的异常出现在 ConsoleApplication1.exe 中。
其他信息: 未将对象引用设置到对象的实例。
Top
5 楼Knight94(愚翁)回复于 2006-06-01 14:06:51 得分 0
如果dll发生变化,应该用如下试试
[DllImport("ppp.dll")]
public extern static void test([In]TestStruct[] testStruct);Top
6 楼Knight94(愚翁)回复于 2006-06-01 14:09:13 得分 0
你在使用参数的时候,如果是在dll进行分配空间,用out属性标示;如果只是在里面进行修改,并不在dll进行分配空间,用In属性来标示。Top
7 楼fishtroop(楚天孤客)回复于 2006-06-01 14:12:56 得分 0
输出还是三个1,struct中的值还是没有改变~~~~
Top
8 楼fishtroop(楚天孤客)回复于 2006-06-01 14:18:24 得分 0
问题解决了....下面把代码全部帖出来..
thx~~~结贴了~~~第一次问问题!开心!
//C++
struct TestStruct
{
float n;
};
extern "C"
_declspec(dllexport)
void test(TestStruct testStruct[3]);
{
for (int j = 0; j < 3; j++)
{
testStruct[j].n = 88.0f;
}
}
//C#
using System;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
struct TestStruct
{
public float n;
};
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
TestStruct[] cc = new TestStruct[3];
for (int i=0; i<3; i++)
{
cc[i].n = 1;
}
test(cc);
for (int i=0; i<3; i++)
{
System.Console.WriteLine(cc[i].n);
}
}
[DllImport("ppp.dll")]
//就是这里要用[Out]
public extern static void test([Out] TestStruct[] testStruct);
}
}
Top
9 楼Knight94(愚翁)回复于 2006-06-01 14:18:25 得分 100
可能是你不统一,你按照如下修改:
// in your dll
void test(TestStruct* testStruct);
{
testStruct = new TestStruct[3];
for (int j = 0; j < 3; j++)
{
testStruct[j].n = 88.0f;
}
}
// in your app
[DllImport("ppp.dll")]
public extern static void test([Out, MarshalAs(UnmanagedType.LPArray)]TestStruct[] testStruct);
Top
10 楼Knight94(愚翁)回复于 2006-06-01 14:20:22 得分 0
//call
TestStruct[] testStruct;
test( testStruct );Top
11 楼nan7757(骑着蚂蚁闯红灯)回复于 2006-06-01 14:24:29 得分 0
学习....Top




