I have yet to find a good reference on this topic. For this example we will take some C++ code that I’m trying to port over to C#.
In C++ land we have the following prototype for an external library function:
extern 'C' DWORD EXPORT FILES_GetMemoryMapping( PSTR pPathFile, PWORD Size, PSTR MapName, PWORD PacketSize, PMAPPING pMapping, PBYTE PagesPerSector);
and in it is used like such:
FILES_GetMemoryMapping((LPSTR)(LPCTSTR)MapFile, &Size, (LPSTR)MapName, &PacketSize, pMapping, &PagePerSector);
Now I’m trying to port the first line over to C# and here is where I’m presented with no clear path.
This is what I’ve got so far:
[DllImport('Files.DLL')] public static extern uint FILES_GetMemoryMapping( [MarshalAs(UnmanagedType.LPStr)] string pPathFile, out ushort Size, [MarshalAs(UnmanagedType.LPStr)] string MapName, out ushort PacketSize, IntPtr pMapping, out byte PagesPerSector);
Now the question is: Is there any good guide out there that tells me a ‘PSTR pPathFile’ should be ‘[MarshalAs(UnmanagedType.LPStr)] string pPathFile’ … Assuming that is correct?
Or, a that a pointer to a ‘PMAPPING’ struct becomes a ‘out IntPtr pMapping’ … assuming that is correct?
Any help on this one guys?
Here you go.
Oddly, the site is down at the moment, but it’s a very good resource for marshalling problems. In the mean time, you can use Archive.net’s archive of pinvoke.net at http://web.archive.org/web/20080202041852/http://www.pinvoke.net