例如C++中定义的以下结构体:
struct RCEStruct {
int Event;
int Flag;
char User[40];
};
int Event;
int Flag;
char User[40];
};
extern "C" __declspec WORD CALLBACK GetStruct(RCEStruct* pEventStruc);
我们将它编译为 MyCppDll.DLL
那么我们在C#中可以直接定义相同的结构体和引用GetStruct:
[StructLayout(LayoutKind.Sequential)]
public struct RCEStruct {
public int Event;
public int Flag;
public char[40] User;
}
[DllImport("MyCppDll.dll", CharSet=CharSet.Auto)]
public static extern int GetStruct(RCEStruct rce);
public struct RCEStruct {
public int Event;
public int Flag;
public char[40] User;
}
[DllImport("MyCppDll.dll", CharSet=CharSet.Auto)]
public static extern int GetStruct(RCEStruct rce);
注意C#里定义的结构体应该和C++里定义的一样。这里如果是public string User就有可能出错(具体我没试过,不知道C#是否会自动将char[]转变为string,另外还要注意,在C#中为User赋值时,长度不应超过40)。
通过这种方式我们就可以向C++传递或者获得结构体。但一个限制就是必须在C#端主动调用GetStruct()
还有一种情况,与上一种相反,就是我们不是希望在C#中调用C++类库,而是想在C++类库中调用我们已经写好的C#类库。这在托管C++里是可以实现的。其中一个应用案例就是在为第三方系统写C++插件的时候,我们必须在插件端主动调用C#类库(前提是我们需要使用它,除非我们完全用C++代码来写这个插件)。
这样的话我们应该是在C#类库公开方法,例如:
public struct RCEStruct {
public int Event;
public int Flag;
public string User;
}
public void DoSomething(RCEStruct rce){
rce.Flag++;
}
public int Event;
public int Flag;
public string User;
}
public void DoSomething(RCEStruct rce){
rce.Flag++;
}
C++端代码如下:
#using <mscorlib.dll>
#using <CuteSuProc.dll>
void SomeMethod(RCEStruct* pEventStruc){
// 将C++结构体赋值到C#结构体
MyCSharpDll::RCEStruct* csStruct;
csStruct->Event = pEventStruc.Event;
csStruct->Flag = pEventStruc.Flag;
// csStruct->User ?? 将char转换成string,在C++里如何处理?
MyCSharpDll::DoSomething(csStruct);
// 将C#结构体赋值到C++结构体
// 因为 pEventStruc 由外界传入,被 DoSomething 方法修改后,可能仍需要外界知道
pEventStruc->Event = csStruct.Event;
pEventStruc->Flag = csStruct.Flag;
// pEventStruc->User ?? 将string转换成char[]
}
#using <CuteSuProc.dll>
void SomeMethod(RCEStruct* pEventStruc){
// 将C++结构体赋值到C#结构体
MyCSharpDll::RCEStruct* csStruct;
csStruct->Event = pEventStruc.Event;
csStruct->Flag = pEventStruc.Flag;
// csStruct->User ?? 将char转换成string,在C++里如何处理?
MyCSharpDll::DoSomething(csStruct);
// 将C#结构体赋值到C++结构体
// 因为 pEventStruc 由外界传入,被 DoSomething 方法修改后,可能仍需要外界知道
pEventStruc->Event = csStruct.Event;
pEventStruc->Flag = csStruct.Flag;
// pEventStruc->User ?? 将string转换成char[]
}
有没有办法直接让C#操作原始的结构体呢?就像C#中操作C++一样,不需要通过一个中间人?能不能直接这样:
#using <mscorlib.dll>
#using <CuteSuProc.dll>
void SomeMethod(RCEStruct* pEventStruc){
MyCSharpDll::DoSomething(pEventStruc);
}
#using <CuteSuProc.dll>
void SomeMethod(RCEStruct* pEventStruc){
MyCSharpDll::DoSomething(pEventStruc);
}
那么还剩一种方法,就是直接对内存进行操作。因为是结构体,他们肯定是保存在连续内存空间中的。
我们先来看看C#中如何操作内存,也就是非托管的数据。这需要引用System.Runtime.InteropServices命名空间。该命名空间下的Marshal的一些静态方法提供了这样的功能:
Marshal.ReadInt32() //从指定内存地址读取4位
Marshal.PtrToStringAnsi() //从指定内存地址读取字符串
Marshal.WriteInt32() //将整数写到指定内存地址
Marshal.WriteByte() //将字符串写到指定内存地址
Marshal.PtrToStringAnsi() //从指定内存地址读取字符串
Marshal.WriteInt32() //将整数写到指定内存地址
Marshal.WriteByte() //将字符串写到指定内存地址
using System;
using System.Text;
using System.Runtime.InteropServices;
internal sealed class RCEvent {
public int Event;
public int Flag;
public string User;
};
internal sealed class RCEventAgent {
internal static RCEvent Read(IntPtr ptr){
RCEvent Event = new RCEvent();
Event.Event = ReadEvent(ptr);
Event.Flag = ReadFlag(ptr);
Event.User = ReadUser(ptr);
return Event;
}
internal static int ReadEvent(IntPtr basePtr) {
return Marshal.ReadInt32(basePtr);
}
internal static int ReadFlag(IntPtr basePtr) {
return Marshal.ReadInt32(basePtr,4);
}
internal static string ReadUser(IntPtr basePtr) {
return Marshal.PtrToStringAnsi(new IntPtr(basePtr.ToInt32() + 8));
}
internal static void Write(ClientEvent Event,IntPtr ptr) {
WriteEvent(ptr,Event.Event);
WriteFlag(ptr,Event.Flag);
WriteUser(ptr,Event.User);
}
internal static void WriteEvent(IntPtr basePtr,int value) {
Marshal.WriteInt32(basePtr,value);
}
internal static void WriteFlag(IntPtr basePtr,int flag) {
Marshal.WriteInt32(basePtr,4,flag);
}
internal static void WriteUser(IntPtr basePtr,string user) {
WriteString(basePtr,user,8,40);
}
private static void WriteString(IntPtr basePtr,string value,int offset,int length) {
int pos = 0;
byte[] bytes = Encoding.Default.GetBytes(value);
while(pos < length) {
if (pos < bytes.Length)
Marshal.WriteByte(basePtr,offset,bytes[pos]);
else
Marshal.WriteByte(basePtr,offset,0);
pos ++;
offset ++;
}
}
}
using System.Text;
using System.Runtime.InteropServices;
internal sealed class RCEvent {
public int Event;
public int Flag;
public string User;
};
internal sealed class RCEventAgent {
internal static RCEvent Read(IntPtr ptr){
RCEvent Event = new RCEvent();
Event.Event = ReadEvent(ptr);
Event.Flag = ReadFlag(ptr);
Event.User = ReadUser(ptr);
return Event;
}
internal static int ReadEvent(IntPtr basePtr) {
return Marshal.ReadInt32(basePtr);
}
internal static int ReadFlag(IntPtr basePtr) {
return Marshal.ReadInt32(basePtr,4);
}
internal static string ReadUser(IntPtr basePtr) {
return Marshal.PtrToStringAnsi(new IntPtr(basePtr.ToInt32() + 8));
}
internal static void Write(ClientEvent Event,IntPtr ptr) {
WriteEvent(ptr,Event.Event);
WriteFlag(ptr,Event.Flag);
WriteUser(ptr,Event.User);
}
internal static void WriteEvent(IntPtr basePtr,int value) {
Marshal.WriteInt32(basePtr,value);
}
internal static void WriteFlag(IntPtr basePtr,int flag) {
Marshal.WriteInt32(basePtr,4,flag);
}
internal static void WriteUser(IntPtr basePtr,string user) {
WriteString(basePtr,user,8,40);
}
private static void WriteString(IntPtr basePtr,string value,int offset,int length) {
int pos = 0;
byte[] bytes = Encoding.Default.GetBytes(value);
while(pos < length) {
if (pos < bytes.Length)
Marshal.WriteByte(basePtr,offset,bytes[pos]);
else
Marshal.WriteByte(basePtr,offset,0);
pos ++;
offset ++;
}
}
}
public void DoSomething(IntPtr ptr){
RCEvent Event = RCEventAgent.Read(ptr);
Event.Flag ++;
RCEventAgent.Write(ptr, Event);
// 或者以下代码
// RCEventAgent.WriteFlag( ptr, RCEventAgent.ReadFlag(ptr) + 1 );
}
RCEvent Event = RCEventAgent.Read(ptr);
Event.Flag ++;
RCEventAgent.Write(ptr, Event);
// 或者以下代码
// RCEventAgent.WriteFlag( ptr, RCEventAgent.ReadFlag(ptr) + 1 );
}
#using <mscorlib.dll>
#using <CuteSuProc.dll>
void SomeMethod(RCEStruct* pEventStruc){
MyCSharpDll::DoSomething(pEventStruc);
}
#using <CuteSuProc.dll>
void SomeMethod(RCEStruct* pEventStruc){
MyCSharpDll::DoSomething(pEventStruc);
}
原文地址: http://www.cnblogs.com/kriss/archive/2005/02/16/104700.html