使用Coredll.dll检索有关存储的信息

本文关键字:存储 信息 Coredll dll 检索 使用 | 更新日期: 2023-09-27 18:24:00

我正试图在C#中使用Coredll.dll来检索已装载存储的数据。

以下是我的结构,它们应该模仿本机库(STORAGEDEVICEINFO,STORAGEINFO)中的结构

[StructLayout(LayoutKind.Sequential)]
public unsafe struct STORAGEDEVICEINFO
{
    public DWORD cbSize;
    public fixed System.UInt16 szProfile[32];
    public DWORD dwDeviceClass;
    public DWORD dwDeviceType;
    public DWORD dwDeviceFlags;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct STOREINFO
{
    public DWORD cbSize;
    public fixed System.UInt16 szDeviceName[8];
    public fixed System.UInt16 szStoreName[32];
    public DWORD dwDeviceClass;
    public DWORD dwDeviceType;
    public STORAGEDEVICEINFO sdi;
    public DWORD dwDeviceFlags;
    public SECTORNUM snNumSectors;
    public DWORD dwBytesPerSector;
    public SECTORNUM snFreeSectors;
    public SECTORNUM snBiggestPartCreatable;
    public DWORD ftCreated; /* ? */
    public DWORD ftLastModified;
    public DWORD dwAttributes;
    public DWORD dwPartitionCount;
    public DWORD dwMountCount;
}

这是我的FindStoreInfo调用(HANDLE只是IntPtr):

    [DllImport("Coredll.dll", SetLastError = true)]
    public static extern HANDLE FindFirstStore(STOREINFO *info/*PSTOREINFO pStoreInfo*/);

函数调用时没有出现错误,但它没有更改info。除了cbSize之外,它将其保留为一个空白结构(但这是从sizeof调用更改的)。这就是我所说的。

        STOREINFO info;
        info.cbSize = (uint)Marshal.SizeOf(typeof(STOREINFO));
        Store.StorageManager.FindFirstStore(&info);

调用GetLastError返回0x57,即"ERROR_INVALID_PARAMETER"。我不知道它为什么会返回这个,因为我所要找的只是指针。

使用Coredll.dll检索有关存储的信息

看起来您的cbSize可能是错误的。尝试更改public fixed char szProfile[32];public fixed System.UInt16 szProfile[32];

此外,FILETIME是一个双DWORD结构,而不是一个DWORD。

我知道这是一个老问题,但也许它可以帮助其他人:

以下结构适用于我,请注意编组:

[StructLayout(LayoutKind.Sequential)]
public unsafe struct STORAGEDEVICEINFO
{
    public DWORD cbSize;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string szProfile;
    public DWORD dwDeviceClass;
    public DWORD dwDeviceType;
    public DWORD dwDeviceFlags;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct STOREINFO
{
    public DWORD cbSize;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 8)]
    public string szDeviceName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string szStoreName;
    public DWORD dwDeviceClass;
    public DWORD dwDeviceType;
    public STORAGEDEVICEINFO sdi;
    public DWORD dwDeviceFlags;
    public SECTORNUM snNumSectors;
    public DWORD dwBytesPerSector;
    public SECTORNUM snFreeSectors;
    public SECTORNUM snBiggestPartCreatable;
    public FILETIME ftCreated;
    public FILETIME ftLastModified;
    public DWORD dwAttributes;
    public DWORD dwPartitionCount;
    public DWORD dwMountCount;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct PARTINFO
{
    public DWORD cbSize;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string szPartitionName;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string szFileSys;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
    public string szVolumeName;
    public SECTORNUM snNumSectors;
    public FILETIME ftCreated;
    public FILETIME ftLastModified;
    public DWORD dwAttributes;
    public BYTE bPartType;
}

我还了解到,您可以通过引用p/Invoke调用来传递结构:

    [DllImport("Coredll.dll", SetLastError = true)]
    public static extern HANDLE FindFirstStore(ref STOREINFO storeInfo);
    [DllImport("Coredll.dll", SetLastError = true)]
    public static extern bool FindNextStore(HANDLE hSearch, ref STOREINFO storeInfo);

电话:

STOREINFO si = new STOREINFO();
HANDLE hSearch = INVALID_HANDLE_VALUE;
si.cbSize = (uint)Marshal.SizeOf(typeof(STOREINFO));
// enumerate first store
hSearch = StorageManager.FindFirstStore(ref si);