江湖救急!!!!!有关c#调用vc写的dll的一个怪问题!!!!!!!!!!!!

weijl 2003-07-20 06:28:27
我在vc.net中定义了一个dll.其中主要有二个结构和一个函数
struct BB
{
int x;
int y;
};

struct TEST
{
int num;
BB *ss;
};

int CErrDllApp::gg(TEST dd)
{
int j = 0;
for(int i = 0; i<dd.num;i++)
{
j = dd.ss[i].x;
j = dd.ss[i].y;

}
return j;
}

在c#中我要调用这个dll函数
struct BB
{
public int x;
public int y;
};
struct TEST
{
public int num;
public BB []ss;
};

[DllImport("ErrDll.dll")]
internal static extern int gg(TEST dd);
编译没问题,但是一调用便发生“追加情報 : 型 TEST的 ss 不能合并排列 : 这种类型的结构体成员不能合并排列”的出错警告。(大致意思如上,因为公司装的是日文vs.net)
我分析是vc中ss定义的是指针,在c中指针和数组有时可通用,但是c#中ss定义的是数组,可能发生不匹配的情况,我试过unsafe模式,还是不行。请问在不能改动vc程序的情况下,如何解决该问题。
...全文
24 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
维她奶 2003-07-22
  • 打赏
  • 举报
回复
up!!!
xixigongzhu 2003-07-22
  • 打赏
  • 举报
回复
你把全部代码完整的给出来。
czlvc 2003-07-22
  • 打赏
  • 举报
回复
vup
Gao2003 2003-07-22
  • 打赏
  • 举报
回复
不过用MarshlaAs.AllocCoTaskMem分配内存后,不再用时,要用MarshlaAs.FreeCoTaskMem释放掉。
Gao2003 2003-07-22
  • 打赏
  • 举报
回复
这个调用确实有些难度,不公过我还是试出来了。更改如下:
VC:

int CErrDllApp::gg(TEST dd)
{......}
改为
int CErrDllApp::gg(TEST* dd)
{......}

C#声明如下:
[StructLayout(LayoutKind.Sequential)]
class BB
{
public int x;
public int y;
};

[StructLayout(LayoutKind.Sequential)]
class TEST
{
public int num;
public IntPtr ss;
};

[DllImport("ErrDll.dll")]
internal static extern int gg([MarshalAs (UnmanagedType.LPStruct)]TEST dd);

调用如下:
TEST test=new TEST();
test.num=3;
test.ss=MarshlaAs.AllocCoTaskMem(test.num*Marshal.SizeOf(typeof(BB)));
gg(test);
BB bb1=(BB)Marshal.PtrToStructure(test.ss,typeof(BB));
其它的bb2,bb3通过Marshal类你也可以操作。虽然这个方法有些笨,但我目前只能想到此方法,欢迎各位指教。
些方法本人已经试过可以用。
还过VC中结构声明为int的最好改为LONG,这样更明确些.
Knight94 2003-07-22
  • 打赏
  • 举报
回复
如果还不行,建议你用vc.net写一个类库,用其调用此DLL(其比C#要方便些),然后用C#引用此类库。
Knight94 2003-07-22
  • 打赏
  • 举报
回复
把Test的定义改为如下:
[StructLayout(LayoutKind.Sequential)]
struct TEST
{
public int num;
[MarshalAs(UnmanagedType.LPArray)]
public BB []ss;
};
试试。
weijl 2003-07-21
  • 打赏
  • 举报
回复
vc的dll中函数定义为
int CErrDllApp::gg(TEST* dd)
{
int j = 0;
for(int i = 0; i< dd->num;i++)
{
j = dd->ss[i].x;
j = dd->ss[i].y;

}
return j;
}
weijl 2003-07-21
  • 打赏
  • 举报
回复
好像还是不行,我在c#中改为
[StructLayout(LayoutKind.Sequential)]
struct BB
{
public int x;
public int y;
};

[StructLayout(LayoutKind.Sequential)]
struct TEST
{
public int num;
[MarshalAs (UnmanagedType.LPStruct)]
public BB []ss;
};

[DllImport("ErrDll.dll")]
internal static extern int gg(
[MarshalAs (UnmanagedType.LPStruct)]
TEST dd);
调用时,
TEST ff;

ff.ss = new BB[5];
ff.num= 5;

ff.ss[0].x = 1;
ff.ss[0].y = 1;
ff.ss[1].x = 2;
ff.ss[1].y = 2;
ff.ss[2].x = 3;
ff.ss[2].y = 3;
ff.ss[3].x = 4;
ff.ss[3].y = 4;
ff.ss[4].x = 5;
ff.ss[4].y = 5;
int r=gg(ff);
一运行还是报错:参数#1无法集成。是unmanage类型。这个值请使用struct。。(大致意思如上,因为公司装的是日文vs.net),我不知道错在那里。请大家指教!!!!

xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
出错的信息给出。
weijl 2003-07-21
  • 打赏
  • 举报
回复
大姐,不行啊
xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
你这样试试:
[DllImport("ErrDll.dll")]
internal static extern int gg(TEST[] dd);

就像这个简单的sample:
using System;
unsafe public class mcopyto
{
public static void Main()
{
mys ss = new mys();
ss.dd = 5;
mys[] sss = new mys[]{ss};
fixed (mys* p = sss) {
print(p);
}
}
struct mys
{
public int dd;
};
static void print(mys* ss) {
Console.WriteLine(ss->dd);
}
}
weijl 2003-07-21
  • 打赏
  • 举报
回复
是int CErrDllApp::gg(TEST* dd),不好意思,以前的copy错了
xixigongzhu 2003-07-21
  • 打赏
  • 举报
回复
哥哥呀,到底是int CErrDllApp::gg(TEST* dd)还是int CErrDllApp::gg(TEST dd),在c#中这有很大差别的呀。
wzs_wzs123 2003-07-20
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/1849/1849551.xml?temp=.1795465

110,567

社区成员

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

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

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