c#调用c++写的dll并如何把结构体地址作为参数传递(急!)

tayir82 2008-10-10 11:08:24
请高手帮忙,
如下是c++中的结构体以及函数原型:
typedef struct _struct1
{
char Str[16];
int iScore;
}struct1;



typedef struct _struct2
{
int Score;
int Num;
struct1* pSt1;
}struct2;

int myFunction(struct2* result);

我在c#中的声明如下:
[DllImport("test.dll", EntryPoint = "myFunction", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int myFunction(IntPtr result);

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Auto,Size=8)]
public struct struct1
{
[FieldOffset(0)]
public byte[] Str;
[FieldOffset(4)]
public int iScore;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct struct2
{
public int Score;
public int lNum;
public IntPtr pSt1;
}

下面这段代码是c++中的实现,没有任何问题。
int retval=0;
int num=5;
struct2 testresult;
testresult.pSt1 = new struct1[num];
testresult.Score = 0;
testresult.Num = num;
retval = myFunction(&testresult);
问题是如何在c#中实现上面在c++中实现的代码?请高手帮忙。先谢了。
...全文
524 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tigerleq 2008-10-11
  • 打赏
  • 举报
回复
给写一个
不如
给你说怎么做
列子,中有错误
自己理解
tigerleq 2008-10-11
  • 打赏
  • 举报
回复
主要有如下几种转换:


将string转为IntPtr:IntPtr System.Runtime.InteropServices.Marshal.StringToCoTaskMemAuto(string)

将IntPtr转为string:string System.Runtime.InteropServices.MarshalPtrToStringAuto(IntPtr)



类型对照:

BSTR --------- StringBuilder

LPCTSTR --------- StringBuilder

LPCWSTR --------- IntPtr

handle---------IntPtr

hwnd-----------IntPtr

char *----------string

int * -----------ref int

int &-----------ref int

void *----------IntPtr

unsigned char *-----ref byte

Struct需要在C#里重新定义一个Struct

CallBack回调函数需要封装在一个委托里,delegate static extern int FunCallBack(string str);

注意在每个函数的前面加上public static extern +返回的数据类型,如果不加public ,函数默认为私有函数,调用就会出错。


在C#调用C++ DLL封装库时会出现两个问题:


1. 数据类型转换问题
2. 指针或地址参数传送问题

首先是数据类型转换问题。因为C#是.NET语言,利用的是.NET的基本数据类型,所以实际上是将C++的数据类型与.NET的基本数据类型进行对应。

例如C++的原有函数是:

int __stdcall FunctionName(unsigned char param1, unsigned short param2)

其中的参数数据类型在C#中,必须转为对应的数据类型。如:

[DllImport(“ COM DLL path/file ”)]
extern static int FunctionName(byte param1, ushort param2)

因为调用的是__stdcall函数,所以使用了P/Invoke的调用方法。其中的方法FunctionName必须声明为静态外部函数,即加上extern static声明头。我们可以看到,在调用的过程中,unsigned char变为了byte,unsigned short变为了ushort。变换后,参数的数据类型不变,只是声明方式必须改为.NET语言的规范。

我们可以通过下表来进行这种转换:

Win32 Types
CLR Type

char, INT8, SBYTE, CHAR
System.SByte

short, short int, INT16, SHORT
System.Int16

int, long, long int, INT32, LONG32, BOOL , INT
System.Int32

__int64, INT64, LONGLONG
System.Int64

unsigned char, UINT8, UCHAR , BYTE
System.Byte

unsigned short, UINT16, USHORT, WORD, ATOM, WCHAR , __wchar_t
System.UInt16

unsigned, unsigned int, UINT32, ULONG32, DWORD32, ULONG, DWORD, UINT
System.UInt32

unsigned __int64, UINT64, DWORDLONG, ULONGLONG
System.UInt64

float, FLOAT
System.Single

double, long double, DOUBLE
System.Double


之后再将CLR的数据类型表示方式转换为C#的表示方式。这样一来,函数的参数类型问题就可以解决了。

现在,我们再来考虑下一个问题,如果要调用的函数参数是指针或是地址变量,怎么办?

对于这种情况可以使用C#提供的非安全代码来进行解决,但是,毕竟是非托管代码,垃圾资源处理不好的话对应用程序是很不利的。所以还是使用C#提供的ref以及out修饰字比较好。

同上面一样,我们也举一个例子:

int __stdcall FunctionName(unsigned char ¶m1, unsigned char *param2)

在C#中对其进行调用的方法是:

[DllImport(“ file ”)]
extern static int FunctionName(ref byte param1, ref byte param2)

看到这,可能有人会问,&是取地址,*是传送指针,为何都只用ref就可以了呢?一种可能的解释是ref是一个具有重载特性的修饰符,会自动识别是取地址还是传送指针。

在实际的情况中,我们利用参数传递地址更多还是用在传送数组首地址上。
如:byte[] param1 = new param1(6);

在这里我们声明了一个数组,现在要将其的首地址传送过去,只要将param1数组的第一个元素用ref修饰。具体如下:

[DllImport(“ file ”)]
extern static int FunctionName(ref byte param1[1], ref byte param2)
tigerleq 2008-10-11
  • 打赏
  • 举报
回复
总规则如下
常量用out
引用用ref
下面给出
C#与C++参数对照表

110,538

社区成员

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

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

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