Skip to main content
 首页 » 编程设计

c#之使用 Coredll.dll 检索商店信息

2024年02月06日12yxwkf

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

这是我的结构,它们应该模仿 native 库中的结构(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”。我不确定它为什么会返回这个,因为我要找的只是指针。

请您参考如下方法:

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

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