6 个解决方案
#1
比如有结构体
struct MyStruct
{
int a;
int b;
}
dll中,比如 void SetStruct(MyStruct* s)
c#中调用时,参数为 SetStruct(ref MyStruct s)
struct MyStruct
{
int a;
int b;
}
dll中,比如 void SetStruct(MyStruct* s)
c#中调用时,参数为 SetStruct(ref MyStruct s)
#2
按照你的方法,取一条数据是没有问题的。我现在想把一个结构传过去,怎么取值。比如
MyPoint[] p = new MyPoint[iItemCount];
for (int i = 0; i < iItemCount; i++)
{
p[i].x = i;
p[i].y = i+43.21;
}
MyPoint[] p = new MyPoint[iItemCount];
for (int i = 0; i < iItemCount; i++)
{
p[i].x = i;
p[i].y = i+43.21;
}
#3
这样做只能取到第一条数据。请指点。
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);
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);
#4
给你个例子
c++
c#
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);
}
#5
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
#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
#6
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();
}
[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();
}
#1
比如有结构体
struct MyStruct
{
int a;
int b;
}
dll中,比如 void SetStruct(MyStruct* s)
c#中调用时,参数为 SetStruct(ref MyStruct s)
struct MyStruct
{
int a;
int b;
}
dll中,比如 void SetStruct(MyStruct* s)
c#中调用时,参数为 SetStruct(ref MyStruct s)
#2
按照你的方法,取一条数据是没有问题的。我现在想把一个结构传过去,怎么取值。比如
MyPoint[] p = new MyPoint[iItemCount];
for (int i = 0; i < iItemCount; i++)
{
p[i].x = i;
p[i].y = i+43.21;
}
MyPoint[] p = new MyPoint[iItemCount];
for (int i = 0; i < iItemCount; i++)
{
p[i].x = i;
p[i].y = i+43.21;
}
#3
这样做只能取到第一条数据。请指点。
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);
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);
#4
给你个例子
c++
c#
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);
}
#5
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
#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
#6
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();
}
[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();
}