2012-05-15 59 views
-1

我在C++这些声明:转换C#和C++与元帅问题

struct objectStruct; 

int positionMemory = getPosition(); 

short size = getSize(); 

void *allocatedObject; // Originally, it is in C#: IntPtr allocatedObject { get; private set; } 

byte[] byteName = Encoding.ASCII.GetBytes("Hello There"); 

我想从C#这行代码转换为C++:

string result = Marshal.PtrToStringAnsi(new IntPtr(positionMemory), size); 

Marshal.StructureToPtr(objectStruct, new IntPtr(positionMemory), true); 

Marshal.Copy(byteName, 0, new IntPtr(positionMemory), size); 

long posInMemory = allocatedObject.Offset(size).ToInt64(); 

我不熟悉的封送处理。

+0

什么问题?你想做什么? C++声明与C#代码有什么关系(从您展示的内容中不完全清楚)? – Kiril

+0

我需要将一些C#代码转换为C++,并面临如何从C#转换为C++编译上述对象的问题 – olidev

回答

1

我不知道C++,但我知道编组所以这里的什么线都在做

//Get size number of characters of the string pointed to by the positionMemory pointer. 
string result = Marshal.PtrToStringAnsi(new IntPtr(positionMemory), size); 

//Copy the contents of objectStruct to the memory location pointed at by positionMemory 
Marshal.StructureToPtr(objectStruct, new IntPtr(positionMemory), true); 

//Copy size number of bytes from the byteName array starting at index 0 to the memory indicated by positionMemory 
Marshal.Copy(byteName, 0, new IntPtr(positionMemory), size); 

//I think offsetting the memory location indicated by allocatedObject by size number of bytes and converting the memory pointer to an Int64. 
machineNamePosInMem = allocatedObject.Offset(size).ToInt64(); 

我不明白你为什么会确实需要这个最转换为C++,点Marshalling的目标是让托管对象可用于非托管代码,并将非托管对象转换为托管对象,如果您使用C++进行全部操作,即使托管C++,也不需要执行此操作。

+0

伟大的答案!非常感谢! – olidev