使用非托管C ++ DLL的AccessViolationException

时间:2018-08-17 15:09:05

标签: c# pinvoke

我第一次尝试在C#应用程序中使用不受管理的C ++ DLL(“ res_lib”)。我已经使用cppsharp来生成PInvoke代码:例如,我要调用的函数/方法之一是get_system_snapshot。在.h文件中,此文件定义为

SYS_INT SYS_ERR get_system_snapshot(SNAPSHOT_PARMS* snapshotp);

SYS_INTSYS_ERR等同于int32_tSNAPSHOT_PARMS

typedef struct SNAPSHOT_PARMS
{
    SYS_ULONG   size;
    SYS_UINT    count;
    SYS_CHAR    serial_no[600];
} SYS_PACK_DIRECTIVE SYS_SNAPSHOT_PARMS;

cppsharp已将其转换为以下代码段:

DllImport

[SuppressUnmanagedCodeSecurity]
[DllImport("res_lib", CallingConvention = CallingConvention.StdCall,
                EntryPoint="get_system_snapshot")]       
   internal static extern int GetSystemSnapshot(IntPtr snapshotp);  

对象

 public unsafe partial class SNAPSHOT_PARMS : IDisposable
    {
 [StructLayout(LayoutKind.Explicit, Size = 608)]
  public partial struct __Internal
    {
        [FieldOffset(0)]
        internal uint size;

        [FieldOffset(4)]
        internal uint count;

        [FieldOffset(8)]
        internal fixed sbyte serial_no[600];

        [SuppressUnmanagedCodeSecurity]
        [DllImport("res_lib", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
            EntryPoint="??0SNAPSHOT_PARMS@@QAE@ABU0@@Z")]
        internal static extern global::System.IntPtr cctor(global::System.IntPtr instance, global::System.IntPtr _0);
    }
}

 public SNAPSHOT_PARMS()
        {
             __Instance = Marshal.AllocHGlobal(sizeof(global::res_lib.SNAPSHOT_PARMS.__Internal));         
            __ownsNativeInstance = true;
            NativeToManagedMap[__Instance] = this;
        }

主要代码

  static void Main(string[] args)
        {
            SNAPSHOT_PARMS p = new SNAPSHOT_PARMS();
            var result = res_lib.res_lib.GetSystemSnapshot(p);
        }

 public static unsafe int GetSystemSnapshot(global::res_lib.SNAPSHOT_PARMS snapshotp)
        {         
            var __arg0 = ReferenceEquals(snapshotp, null) ? global::System.IntPtr.Zero : snapshotp.__Instance;
            var __ret = __Internal.GetSystemSnapshot(out __arg0);
            return __ret;
        }

调用该函数时,我臭名昭著:

  

试图读取或写入受保护的内存。这通常表明其他内存已损坏。

我尝试将CallingConventionStdCall更改为Cdecl,将[In][Out]引入DllImport等,但是所有无济于事。任何人都可以看到代码中明显有问题的地方吗?-显而易见,这对我来说是新的,也许我要求cppsharp产生一些不需要调整的代码。

编辑原始的C ++文档中有一个示例,其中该结构是通过以下方式初始化的:

#define INIT_STRUCT(struct_p) { memset(struct_p, 0, sizeof(*(struct_p))); (struct_p)->size = sizeof(*(struct_p)); }

并使用

 SNAPSHOT_PARMS snapshot_parms;
 SYS_ERR result;

 INIT_STRUCT(&snapshot_parms);
result = get_system_snapshot(&snapshot_parms); 

1 个答案:

答案 0 :(得分:3)

从C ++声明中,就足够了:

[StructLayout(LayoutKind.Sequential)]
unsafe struct SNAPSHOT_PARMS {
    public int size;
    public int count;
    public fixed byte serial_no[600];
}

[DllImport("res_lib", EntryPoint = "get_system_snapshot")]
static extern int GetSystemSnapshot(ref SNAPSHOT_PARMS snapshot);

用作

var s = new SNAPSHOT_PARMS { size = Marshal.SizeOf<SNAPSHOT_PARMS>() };
int result = GetSystemSnapshot(ref s);
// check result, use s