我对基于c的DLL中的函数进行了以下定义(没有可用的源代码):
uint MakeReference(MemBlock* ipReferenceMemBlock);
MemBlock定义为:
typedef struct {
HANDLE_TAG dTag;
long dLength;
BYTE* dpReference;
} MemBlock;
HANDLE_TAG定义为:
typedef char HANDLE_TAG[10 + 1];
我在C#Wrapper类中对此的看法是:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MemBlock
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
public string dTag;
public uint dLength;
[MarshalAs(UnmanagedType.LPStr)]
public string dpReference;
}
[DllImport("lib.dll", CharSet = CharSet.Ansi, EntryPoint = "MakeReference@24", CallingConvention = CallingConvention.StdCall)]
public static extern uint MakeReference(ref IntPtr pDDipMemBlock);
public static uint FAPIMakeReference(ref MemBlock ipMemBlock)
{
uint error = 0;
IntPtr pDDipMemBlock = Marshal.AllocHGlobal(Marshal.SizeOf(ipMemBlock));
try
{
Marshal.StructureToPtr(ipMemBlock, pDDipMemBlock, false);
error = MakeReferenceEx(ref pDDipMemBlock);
ipMemBlock = (MemBlock)Marshal.PtrToStructure(pDDipMemBlock, typeof(MemBlock));
}
finally
{
Marshal.FreeHGlobal(pDDipMemBlock);
}
return error;
}
用以下方式调用:
MemBlock refMemBlock = new MemBlock();
uint error = MakeReferenceEx(ref refMemBlock);
导致访问冲突期望