编组Ptr到Struct的访问冲突

时间:2014-03-31 01:08:04

标签: c# c++ c struct marshalling

我的c dll中定义了以下结构:

typedef struct {
    char Name[10];
    char Flag[10];
} CountryData;

typedef struct {
    int NumElements;
    TrackData Elements[1000];
} CountryArray;

这是暴露的:

__declspec( dllexport ) CountryArray* _cdecl GetAllCountries()
{
    CountryArray fakedata;
    CountryData fakecountry = CountryData();
    strcpy_s(fakecountry.Name, "TEST");
    strcpy_s(fakecountry.Flag, "TEST");
    fakedata.Elements[0] = faketrack;
    fakedata.NumElements = 1;
    return new CountryArray(fakedata);
}

现在在c#中我定义了这些结构:

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
    public struct COUNTRY
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
        public string Name;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 10)]
        public string Flag;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct COUNTRY_ARRAY
    {
        public int NumElements;
        public IntPtr Elements;
    }

我通过此导入访问它:

    [DllImport("Countries.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "?GetAllCountries@@YAPAUCountryArray@@XZ")]
    public static extern IntPtr GetAllCountries();

最后我试图像这样整理数据:

IntPtr countryPtr = Natives.GetAllCountries();
Natives.COUNTRY_ARRAY countries = (Natives.COUNTRY_ARRAY)Marshal.PtrToStructure(countryPtr, typeof(Natives.COUNTRY_ARRAY));
for (int i = 0; i < countries.NumElements; i++)
{
    IntPtr iptr = (IntPtr)(countries.Elements.ToInt32() + (i * Marshal.SizeOf(typeof(Natives.COUNTRY))));
    try
    {
        //fails here
        Natives.COUNTRY country = (Natives.COUNTRY)Marshal.PtrToStructure(iptr, typeof(Natives.COUNTRY));                    
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }                
}

该国家的编组是我收到此错误的地方:

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Runtime.InteropServices.Marshal.PtrToStructureHelper(IntPtr ptr, Object structure, Boolean allowValueClasses)

我尝试修改COUNTRY结构的大小并更改字符集但仍然出现此错误。我完全陷入困境,这可能是什么问题?

1 个答案:

答案 0 :(得分:1)

所以你的问题是你将Elements的值视为一个指针,而实际上它是数组第一个元素的数据。在您的测试用例中,这会在32位系统上为Elements提供值0x54455354,在64位系统上提供0x5445535400000000(&#34; TEST&#34;以十六进制表示) 。你正在抵消那个指针。相反,您希望获得指向Elements字段的指针,可以这样做:

IntPtr elementsOffset = Marshal.OffsetOf(typeof(COUNTRY_ARRAY), "Elements");

然后,您将从函数中获取指针,并在计算每个元素的位置时为其添加偏移量。

另外,你应该always use IntPtr's ToInt64() method。它在32位系统上稍慢一点,但避免了在64位系统中截断内存地址的可怕边缘情况。

此外,您的入口点上的名称错误让我相信这是使用C ++编译器编译的。如果使用不同的编译器或甚至同一编译器的不同版本进行编译,则绑定将失败。你应该做的是将这些方法导出为C函数,这样它们就不会被破坏。为此,请将您要导出的方法包装在extern "C" { ... }块中。

此外,您可以完全避免此问题,让结构为您完成工作:

[StructLayout(LayoutKind.Sequential)]
public struct COUNTRY_ARRAY
{
    public int NumElements;

    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 1000)]
    public COUNTRY[] Elements;
}