如何正确对齐SHELLSTATE结构的字段?

时间:2016-05-18 01:35:55

标签: c# .net vb.net winapi pinvoke

有人可以帮我为 SHELLSTATE 结构编写正确的定义(在C#或VB.NET中)吗?

我自己的定义无法正常工作,有些成员无法按预期工作,例如fShowAllObjects工作正常,但fShowExtensions没有,我无法将字段设置为{{ 1}},所以我认为是因为现场打包是错误的。

我不确定是否应该指定True属性,或者我需要指定StructLayoutAttribute.Pack然后为每个字段指定正确的字段偏移量,或者编组LayoutKind.Explicit字段对于Boolean的1字节,这些都是我尝试过但没有成功的事情,我需要帮助才能正确完成。

C#:

<MarshalAs(UnmanagedType.I1)>

VB.NET:

[StructLayout(LayoutKind.Sequential)]
public struct ShellState
{
    public bool ShowAllObjects;
    public bool ShowExtensions;
    public bool NoConfirmRecycle;
    public bool ShowSysFiles;
    public bool ShowCompColor;
    public bool DoubleClickInWebView;
    public bool DesktopHtml;
    public bool Win95Classic;
    public bool DontPrettyPath;
    public bool Unused1;
    public bool MapNetDrvBtn;
    public bool ShowInfoTip;
    public bool HideIcons;
    public bool WebView;
    public bool Unused2;
    public bool ShowSuperHidden;
    public bool NoNetCrawling;
    public int Unused3;
    public uint Unused4;
    public long ParamSort;
    public int SortDirection;
    public uint Unused5;
    public uint Unused6;
    public bool SepProcess;
    public bool WinXpStartPanelOn;
    public bool Unused7;
    public bool AutoCheckSelect;
    public bool IconsOnly;
    public bool ShowTypeOverlay;
    public bool ShowStatusBar;
    public uint Unused8;
}

我还将提供 SHGetSetSettings 函数,以进一步测试结果修改:

<StructLayout(LayoutKind.Sequential)>
Public Structure ShellState
    Public ShowAllObjects As Boolean
    Public ShowExtensions As Boolean
    Public NoConfirmRecycle As Boolean
    Public ShowSysFiles As Boolean
    Public ShowCompColor As Boolean
    Public DoubleClickInWebView As Boolean
    Public DesktopHtml As Boolean
    Public Win95Classic As Boolean
    Public DontPrettyPath As Boolean
    Public Unused1 As Boolean
    Public MapNetDrvBtn As Boolean
    Public ShowInfoTip As Boolean
    Public HideIcons As Boolean
    Public WebView As Boolean
    Public Unused2 As Boolean
    Public ShowSuperHidden As Boolean
    Public NoNetCrawling As Boolean
    Public Unused3 As Integer
    Public Unused4 As UInteger
    Public ParamSort As Long
    Public SortDirection As Integer
    Public Unused5 As UInteger
    Public Unused6 As UInteger
    Public SepProcess As Boolean
    Public WinXpStartPanelOn As Boolean
    Public Unused7 As Boolean
    Public AutoCheckSelect As Boolean
    Public IconsOnly As Boolean
    Public ShowTypeOverlay As Boolean
    Public ShowStatusBar As Boolean
    Public Unused8 As UInteger
End Structure

SSF 枚举:

<DllImport("Shell32.dll", SetLastError:=False)>
Public Shared Sub SHGetSetSettings(<[In]> <Out> ByRef refState As ShellState,
                  <MarshalAs(UnmanagedType.U4)> ByVal mask As ShellStateFlags,
                <MarshalAs(UnmanagedType.Bool)> ByVal applyState As Boolean)
End Sub

1 个答案:

答案 0 :(得分:3)

这是一个丑陋的结构。从实验中我确定字段是4字节对齐的,并且整体结构大小(MSVC编译器)是32字节。鉴于此,以下结构应该有效。 (后记)

[StructLayout(LayoutKind.Sequential)]
public struct SHELLSTATE // int (4 byte) alignment
{
    public uint firstFlags; // includes alignment padding
    public uint dwWin95Unused;
    public uint uWin95Unused;
    public int lParamSort;
    public int iSortDirection;
    public uint version;
    public uint uNotUsed;
    public uint secondFlags; // includes spares at end to prevent access violation

    public bool fShowAllObjects => (this.firstFlags & 0x00000001) == 0x00000001;
    public bool fShowExtensions => (this.firstFlags & 0x00000002) == 0x00000002;
    public bool fNoConfirmRecycle => (this.firstFlags & 0x00000004) == 0x00000004;
    public bool fShowSysFiles => (this.firstFlags & 0x00000008) == 0x00000008;
    public bool fShowCompColor => (this.firstFlags & 0x00000010) == 0x00000010;
    public bool fDoubleClickInWebView => (this.firstFlags & 0x00000020) == 0x00000020;
    public bool fDesktopHTML => (this.firstFlags & 0x00000040) == 0x00000040;
    public bool fWin95Classic => (this.firstFlags & 0x00000080) == 0x00000080;
    public bool fDontPrettyPath => (this.firstFlags & 0x00000100) == 0x00000100;
    public bool fShowAttribCol => (this.firstFlags & 0x00000200) == 0x00000200;
    public bool fMapNetDrvBtn => (this.firstFlags & 0x00000400) == 0x00000400;
    public bool fShowInfoTip => (this.firstFlags & 0x00000800) == 0x00000800;
    public bool fHideIcons => (this.firstFlags & 0x00001000) == 0x00001000;
    public bool fWebView => (this.firstFlags & 0x00002000) == 0x00002000;
    public bool fFilter => (this.firstFlags & 0x00004000) == 0x00004000;
    public bool fShowSuperHidden => (this.firstFlags & 0x00008000) == 0x00008000;
    public bool fNoNetCrawling => (this.firstFlags & 0x00010000) == 0x00010000;
    public bool fSepProcess => (this.secondFlags & 0x00000001) == 0x00000001;
    public bool fStartPanelOn => (this.secondFlags & 0x00000002) == 0x00000002;
    public bool fShowStartPage => (this.secondFlags & 0x00000004) == 0x00000004;
    public bool fAutoCheckSelect => (this.secondFlags & 0x00000008) == 0x00000008;
    public bool fIconsOnly => (this.secondFlags & 0x00000010) == 0x00000010;
    public bool fShowTypeOverlay => (this.secondFlags & 0x00000020) == 0x00000020;
    public bool fShowStatusBar => (this.secondFlags & 0x00000040) == 0x00000040;

    public override string ToString() => $"SHELLSTATE Version: {this.version}";
}

由于我写了原始答案,我使用一些C ++ / CLI重新检查了标志是否已正确分解:

static IntPtr GetShellState()
{
    SHELLSTATE* state = (SHELLSTATE*)calloc(1, sizeof(SHELLSTATE));
    state->fShowAllObjects = TRUE;
    return IntPtr((void*)state);
}

static String^ SetNext(IntPtr value, int% flags)
{
    String^ result;

    SHELLSTATE* state = (SHELLSTATE*)value.ToPointer();
    if (state->fShowAllObjects)
    {
        state->fShowAllObjects = FALSE;
        state->fShowExtensions = TRUE;
        result = gcnew String("fShowExtensions");
        flags = 0;
    }
    else if (state->fShowExtensions)
    {
        // Removed other flag toggling
    return result;
}

static void FreeShellState(IntPtr value)
{
    if (value != IntPtr::Zero)
    {
        void* ptr = value.ToPointer();
        free(ptr);
    }
}

然后使用以下C#生成标志:

IntPtr s = Class1.GetShellState();
string itemName = "fShowAllObjects";
int flags = 0;

Func<IntPtr, int, uint> ReadFlags = (IntPtr ptr, int item) => (uint)Marshal.ReadInt32(ptr, item == 0 ? 0 : 28);

uint val = ReadFlags(s, flags);
System.Diagnostics.Debug.Print($"public bool {itemName} => (this.{(flags == 0 ? "first" : "second")}Flags & 0x{val.ToString("X8")}) == 0x{val.ToString("X8")};");

while (flags < 2)
{
    itemName = Class1.SetNext(s, ref flags);
    val = ReadFlags(s, flags);
    Debug.Print($"public bool {itemName} => (this.{(flags == 0 ? "first" : "second")}Flags & 0x{val.ToString("X8")}) == 0x{val.ToString("X8")};");
}

Class1.FreeShellState(s);

为了测试它,我使用了SHGetSetSettings P / Invoke:

[DllImport("Shell32.dll")]
public static extern void SHGetSetSettings(ref SHELLSTATE lpss, 
                                           UInt32 dwMask, 
                                           [MarshalAs(UnmanagedType.Bool)] bool bSet);

并调用函数:

SHELLSTATE state = new SHELLSTATE();
SHGetSetSettings(ref state, 0xFFFFFFFF, false);