关于c#调用c++DLL问题

Sylarzgb 2011-04-22 02:51:00
大家好!
小弟现在有一个问题,希望大家帮忙。
我用c#调用c++的DLL(dll自己写的),在调用时要传递一个赋值过的结构体到DLL里。应该怎么做。高手们帮帮忙。最好给出例子。在DLL里如何取结构体的数据。函数怎么写????
...全文
185 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
mjp1234airen4385 2011-04-22
  • 打赏
  • 举报
回复
C#代码少了定义了:
[DllImport("DllTest.dll")]
unsafe static extern int testfunc(MyPoint* pp, int nItemCount);

struct MyPoint
{
public int x;
public int y;
}
public Form1()
{
InitializeComponent();
}

unsafe void test()
{
MyPoint[] p = new MyPoint[50];
for (int i = 0; i < 50; i++)
{
p[i].x = i;
p[i].y = i;
}
fixed ( MyPoint* ptr = &p[0] )
{
testfunc(ptr, 50);
}
}

private void button1_Click(object sender, EventArgs e)
{
test();
}
mjp1234airen4385 2011-04-22
  • 打赏
  • 举报
回复
C++代码:
#include "stdafx.h"
#include "DllTest.h"
#include <iostream>

using namespace std;


typedef struct MyPoint
{
int x;
int y;
} *PMyPoint;

extern "C" _declspec(dllexport) int __stdcall testfunc(MyPoint *pp,int nItemCount)
{
for(int i=0;i<nItemCount;i++)
{
cout<<pp[i].x<<", "<<pp[i].y<<endl;
}
return 0;
}

C#代码:
unsafe void test()
{
MyPoint[] p = new MyPoint[50];
for (int i = 0; i < 50; i++)
{
p[i].x = i;
p[i].y = i;
}
fixed ( MyPoint* ptr = &p[0] )
{
testfunc(ptr, 50);
}
}

记得修改:项目->XXX属性->生成->允许不安全代码 (打勾)。

调用结果:
线程 0x146c 已退出,返回值为 0 (0x0)。
线程 0xfc0 已退出,返回值为 0 (0x0)。
“WindowsFormsApplication1.vshost.exe”(托管): 已加载“C:\Documents and Settings\Administrator\桌面\DllTest\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe”,符号已加载。
0, 0
1, 1
2, 2
3, 3
4, 4
5, 5
6, 6
7, 7
8, 8
9, 9
10, 10
11, 11
12, 12
13, 13
14, 14
15, 15
16, 16
17, 17
18, 18
19, 19
20, 20
21, 21
22, 22
23, 23
24, 24
25, 25
26, 26
27, 27
28, 28
29, 29
30, 30
31, 31
32, 32
33, 33
34, 34
35, 35
36, 36
37, 37
38, 38
39, 39
40, 40
41, 41
42, 42
43, 43
44, 44
45, 45
46, 46
47, 47
48, 48
49, 49
bdmh 2011-04-22
  • 打赏
  • 举报
回复
给你个例子
c++

struct MyPoint
{
int x;
int y;
};


#include"stdio.h"

void _declspec(dllexport) TestFunc(MyPoint *p,int nItemCount)
{
FILE* f = fopen("c:\\mypoint.txt","w");
for(int i=0;i<nItemCount;i++)
{
fwrite(&p[i].x,sizeof(int),1,f);
fwrite(":",sizeof(char),1,f);
fwrite(&p[i].y,sizeof(int),1,f);
}
fclose(f);
}


c#

struct Mypoint
{
public int x;
public int y;
}


[DllImport(@"E:\个人文件\Source\VS\test\Test_c_Console\debug\Test_c_Dll.dll")]
private static extern void TestFunc([In, Out] Mypoint[] p, int i);
private void button1_Click(object sender, EventArgs e)
{
Mypoint[] p = new Mypoint[50];
for (int i = 0; i < 50; i++)
{
p[i].x = i;
p[i].y = i ;
}
TestFunc(p, 50);

}
Sylarzgb 2011-04-22
  • 打赏
  • 举报
回复

这样做只能取到第一条数据。请指点。
extern "C" _declspec(dllexport) int __stdcall testfunc(MyPoint *pp,int nItemCount)
{
for(int i=0;i<nItemCount;i++)
{
CString sql;
cout<<pp[i].x<<", "<<pp[i].y<<endl;

}
return 0;
}
c#调用。
int iItemCount = 50;

MyPoint[] p = new MyPoint[iItemCount];
for (int i = 0; i < iItemCount; i++)
{
p[i].x = i;
p[i].y = i+43.21;
}
testfunc(p, iItemCount);
Sylarzgb 2011-04-22
  • 打赏
  • 举报
回复
那我在c++里怎么取值啊。pp[i].x 这样不行啊。
bdmh 2011-04-22
  • 打赏
  • 举报
回复
c++中肯定是 MyPoint* p 了
Sylarzgb 2011-04-22
  • 打赏
  • 举报
回复
高手大哥,留个联系方式 。我找你。QQ msn都可以谢谢。
Sylarzgb 2011-04-22
  • 打赏
  • 举报
回复
dll原型是我自己写的。不知道怎么写。帮忙写一个。
bdmh 2011-04-22
  • 打赏
  • 举报
回复
MyPoint[] p = new MyPoint[iItemCount];
传给dll时,用 ref p,或者out p,要看具体的dll原型
Sylarzgb 2011-04-22
  • 打赏
  • 举报
回复
int iItemCount = 50;

MyPoint[] p = new MyPoint[iItemCount];
for (int i = 0; i < iItemCount; i++)
{
p[i].x = i;
p[i].y = i+43.21;
}
把这个传过去。在DLL里怎么进行取值。请高手留个联系方式 。
bdmh 2011-04-22
  • 打赏
  • 举报
回复
传结构体指针,C# 用ref 或者out 修饰
Sylarzgb 2011-04-22
  • 打赏
  • 举报
回复
我按照这里的方法试过了。这是传递一条数据吧。我是想传一个结体,里面包含多条数据。请指点。

110,574

社区成员

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

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

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