简体   繁体   English

错误遍历IntPtr以构建结构数组

[英]Error looping through IntPtr to build array of struct

I'm having a problem building an array of struct s from an IntPtr in another struct . 我在从另一个structIntPtr构建一个struct数组时遇到问题。

This structure is returned by a Windows API I'm using: 我正在使用的Windows API返回此结构:

public struct DFS_INFO_9 {
    [MarshalAs(UnmanagedType.LPWStr)]
    public string EntryPath;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string Comment;
    public DFS_VOLUME_STATE State;
    public UInt32 Timeout;
    public Guid Guid;
    public UInt32 PropertyFlags;
    public UInt32 MetadataSize;
    public UInt32 SdLengthReserved;
    public IntPtr pSecurityDescriptor;
    public UInt32 NumberOfStorages;
    public IntPtr Storage;
}

[DllImport("netapi32", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int NetDfsEnum([MarshalAs(UnmanagedType.LPWStr)]string DfsName, int Level, int PrefMaxLen, out IntPtr Buffer, [MarshalAs(UnmanagedType.I4)]out int EntriesRead, [MarshalAs(UnmanagedType.I4)]ref int ResumeHandle);

I'm trying to get the array of DFS_STORAGE_INFO_1 s referenced by IntPtr Storage . 我正在尝试获取IntPtr Storage引用的DFS_STORAGE_INFO_1数组。

Here's that structure (if it matters): 这就是这种结构(如果重要的话):

public struct DFS_STORAGE_INFO_1 {
    public DFS_STORAGE_STATE State;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ServerName;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ShareName;
    public IntPtr TargetPriority;
}

So far this code has been working to get the DFS_INFO_9 s with only one storage, but fails when trying to marshal the second item in the array. 到目前为止,此代码一直在努力仅使用一个存储来获取DFS_INFO_9 ,但是在尝试DFS_INFO_9数组中的第二项时失败。

DFS_INFO_9 info = GetInfoFromWinApi();
List<DFS_STORAGE_INFO_1> Storages = new List<DFS_STORAGE_INFO_1>();
for (int i = 0; i < info.NumberOfStorages; i++) {
    IntPtr pStorage = new IntPtr(info.Storage.ToInt64() + i * Marshal.SizeOf(typeof(DFS_STORAGE_INFO_1)));
    DFS_STORAGE_INFO_1 storage = (DFS_STORAGE_INFO_1)Marshal.PtrToStructure(pStorage, typeof(DFS_STORAGE_INFO_1));
    Storages.Add(storage);
}

I'm getting a FatalExecutionEngineError that spits out error code 0x0000005 (Access Denied). 我收到一个FatalExecutionEngineError ,它吐出错误代码0x0000005(拒绝访问)。 I'm assuming the size of the DFS_STORAGE_INFO_1 is being miscalculated causing the marshal to attempt to access memory outside that allocated for the array. 我假设DFS_STORAGE_INFO_1的大小计算错误,导致元帅试图访问分配给该数组的内存之外的内存。 But this is happening on i = 1 , when there might be 7 storages to get through. 但这发生在i = 1 ,当时可能有7个存储要通过。 Maybe my thinking is flawed, but I have no idea how to rectify this. 也许我的想法是有缺陷的,但我不知道该如何纠正。

The actual definition of DFS_STORAGE_INFO_1 is this: DFS_STORAGE_INFO_1的实际定义是这样的:

public struct DFS_STORAGE_INFO_1 {
    public DFS_STORAGE_STATE State;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ServerName;
    [MarshalAs(UnmanagedType.LPWStr)]
    public string ShareName;
    DFS_TARGET_PRIORITY TargetPriority;
}

TargetPriority is a structure, defined here : TargetPriority是一个结构, 在此定义:

public struct DFS_TARGET_PRIORITY {
    public DFS_TARGET_PRIORITY_CLASS TargetPriorityClass;
    public UInt16 TargetPriorityRank;
    public UInt16 Reserved;
}

public enum DFS_TARGET_PRIORITY_CLASS {
    DfsInvalidPriorityClass = -1,
    DfsSiteCostNormalPriorityClass = 0,
    DfsGlobalHighPriorityClass = 1,
    DfsSiteCostHighPriorityClass = 2,
    DfsSiteCostLowPriorityClass = 3,
    DfsGlobalLowPriorityClass = 4
}

As for the FatalExecutionEngineError , I believe that the size of the structure DFS_STORAGE_INFO_1 was being miscalculated since it was defined incorrectly. 至于FatalExecutionEngineError ,我认为结构DFS_STORAGE_INFO_1的大小计算不正确,因为定义不正确。 When trying to convert the pointers to the structures they referenced, the next index was wrong because the size was off. 当尝试将指针转换为它们所引用的结构时,下一个索引是错误的,因为大小已关闭。 When converting the block of memory, it presumably referenced a block that shouldn't have been accessible, throwing the "Access Denied (0x0000005)" error. 转换内存块时,它大概引用了不应访问的块,并引发“访问被拒绝(0x0000005)”错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM