如何将IntPtr转换回对象

时间:2022-04-18 07:33:33

All, this is a follow up from a previous question here: C# formatting external Dll function parameters

全部,这是前一个问题的后续跟进:C#格式化外部Dll函数参数

Here specifically is the code that I am trying to convert to C#:

这里特别是我试图转换为C#的代码:

FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pMapping, &PagePerSector);
// Allocate the mapping structure memory
pMapping = (PMAPPING)malloc(sizeof(MAPPING));
pMapping->NbSectors = 0;
pMapping->pSectors = (PMAPPINGSECTOR) malloc((Size) * sizeof(MAPPINGSECTOR));

// Get the mapping info
FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)(LPCTSTR)MapName, &PacketSize, pMapping, &PagePerSector);

The function "FILES_GetMemoryMapping" gets called twice, I'm guessing the first time to get the size of the struct, and the second to actually fill it.

函数“FILES_GetMemoryMapping”被调用两次,我猜第一次得到结构的大小,第二次实际填充它。

the "pMapping" is a pointer to a struct in C++, In my C# code, I have pMapping as type IntPtr. The next line I can converted to :

“pMapping”是指向C ++中结构的指针,在我的C#代码中,我将pMapping作为IntPtr类型。下一行我可以转换为:

pMapping = Marshal.AllocHGlobal(Marshal.SizeOf(new UM0516.Mapping()));

With (UM0516.Mapping) being the struct. Cool, so I've just allocated some space that IntPtr is pointing to. Now for the next line... "pMapping->NbSectors = 0;"

(UM0516.Mapping)是结构。很酷,所以我刚刚分配了一些IntPtr指向的空间。现在为下一行......“pMapping-> NbSectors = 0;”

How am I suppose to go into the now allocated unmanaged memory space, type cast it as a (UM0516.Mapping) struct, and set one of its members? Then make sure i didn't screw with it too much so that the second time I call "FILES_GetMemoryMapping", it can now use this struct??

我怎么想进入现在分配的非托管内存空间,键入将其转换为(UM0516.Mapping)结构,并设置其中一个成员?然后确保我没有过多地使用它,以便第二次调用“FILES_GetMemoryMapping”时,它现在可以使用这个结构吗?

-- Ok, I've taken some advice and now have this:

- 好的,我已经采取了一些建议,现在有了这个:

I tried this and I get a "AccessViolationException was unhandled" exception on the first "FILES_GetMemoryMapping" call

我尝试了这个,并在第一个“FILES_GetMemoryMapping”调用中得到“AccessViolationException未处理”异常

Here is what I have:

这是我有的:

string filepath = @"C:\blah.blah";
string MapFile = @"D:\blah.blah";
UM0516.Mapping myMapping = new UM0516.Mapping();
IntPtr pMapping = Marshal.AllocHGlobal(Marshal.SizeOf(myMapping));
Marshal.StructureToPtr(myMapping, pMapping, false);
ushort PacketSize = 0;
ushort size = 0;
string MapName = String.Empty;
byte PagePerSector = 0;

uint b7 = UM0516.FILES_GetMemoryMapping(MapFile, out size, MapName, out PacketSize, pMapping, out PagePerSector);

Do you think this exception is coming from the "pMapping" parameter? Could this come from anything else that I've passed in?

您是否认为此异常来自“pMapping”参数?这可能来自我传入的任何其他内容吗?

2 个解决方案

#1


In order to get the IntPtr, what you'll want to do is create your structure, set any options that you may need to, allocate the memory like you already have, then call..

为了获得IntPtr,您要做的是创建您的结构,设置您可能需要的任何选项,像您已经拥有的那样分配内存,然后调用..

System.Runtime.InteropServices.Marshal.StructureToPtr(yourStructVariable, pMapping, false);

This will copy the data from your populated structure into your allocated memory.

这会将填充结构中的数据复制到分配的内存中。

To copy data from the memory into a new structure named 'mapping', call...

要将内存中的数据复制到名为“mapping”的新结构中,请调用...

UM0516.Mapping mapping = (UM0516.Mapping)System.Runtime.InteropServices.Marshal.PtrToStructure(pMapping, typeof(UM0516.Mapping))

#2


To convert an IntPtr back to an object, I used a method that does this:

要将IntPtr转换回对象,我使用了一个执行此操作的方法:

if (ptrToUnwrap == IntPtr.Zero)
    return null;
GCHandle handle = (GCHandle)ptrToUnwrap;
object handledObj = handle.Target;
if (handles.unfreed.Contains(handle))
    handles.unfreed.Remove(handle);
handle.Free();
return handledObj;

(handles.unfreed is a list of unfreed GCHandles that are automatically freed when handles is disposed or finialized)

(handles.unfreed是一个未处理的GCHandles列表,当句柄被处理或最终化时会自动释放)

#1


In order to get the IntPtr, what you'll want to do is create your structure, set any options that you may need to, allocate the memory like you already have, then call..

为了获得IntPtr,您要做的是创建您的结构,设置您可能需要的任何选项,像您已经拥有的那样分配内存,然后调用..

System.Runtime.InteropServices.Marshal.StructureToPtr(yourStructVariable, pMapping, false);

This will copy the data from your populated structure into your allocated memory.

这会将填充结构中的数据复制到分配的内存中。

To copy data from the memory into a new structure named 'mapping', call...

要将内存中的数据复制到名为“mapping”的新结构中,请调用...

UM0516.Mapping mapping = (UM0516.Mapping)System.Runtime.InteropServices.Marshal.PtrToStructure(pMapping, typeof(UM0516.Mapping))

#2


To convert an IntPtr back to an object, I used a method that does this:

要将IntPtr转换回对象,我使用了一个执行此操作的方法:

if (ptrToUnwrap == IntPtr.Zero)
    return null;
GCHandle handle = (GCHandle)ptrToUnwrap;
object handledObj = handle.Target;
if (handles.unfreed.Contains(handle))
    handles.unfreed.Remove(handle);
handle.Free();
return handledObj;

(handles.unfreed is a list of unfreed GCHandles that are automatically freed when handles is disposed or finialized)

(handles.unfreed是一个未处理的GCHandles列表,当句柄被处理或最终化时会自动释放)