I have C++ functions with the following prototypes:
extern "C" __declspec(dllexport) HFSUniStr255 GetFolderList(char *path);
extern "C" __declspec(dllexport) void GetFile(char *path, UInt64* offset, UInt64* length);
The HFSUniStr255 typedef is the following struct:
struct HFSUniStr255 {
UInt16 length;
UInt16 unicode[255];
};
Where a UInt16 is an unsigned 16-bit integer. The two functions take a pointer to a char array and the former returns the above struct and the latter writes 64-bit integer values to the locations indicated by the parameter pointers offset and length.
My question is this: how on Earth do I work with these in managed C# code? I'm trying to follow examples like
this to no avail. In C# I have defined the following:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct HFSUniStr255
{
[MarshalAs(UnmanagedType.U2)]
public UInt16 length;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=255)]
public UInt16[] unicode;
}
[DllImport("llio2.dll")]
public static extern void GetFile([MarshalAs(UnmanagedType.LPStr)] char[] path, [MarshalAs(UnmanagedType.U8)] UIntPtr offset, [MarshalAs(UnmanagedType.U8)] UIntPtr length);
[DllImport("llio2.dll")]
[return : MarshalAs(UnmanagedType.Struct)]
public static extern HFSUniStr255 GetFolderList([MarshalAs(UnmanagedType.LPStr)] char[] path);
Despite this, I get errors telling me that I've tried reading or writing protected memory and that the method's signature is not compatible with the PInvoke type. Any gurus in this area?
posted by 0xFCAF at 10:06 PM on December 28, 2007