microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2.07k stars 87 forks source link

SystemParametersInfo cannot read the path of the desktop background image #1256

Closed LcFireRabbit closed 1 month ago

LcFireRabbit commented 1 month ago

image

unsafe void GetDesktopBackgroundImage()
{
    FixedSizeCharStruct wallPaperPath = new FixedSizeCharStruct();
    bool isSucc = PInvoke.SystemParametersInfo(Windows.Win32.UI.WindowsAndMessaging.SYSTEM_PARAMETERS_INFO_ACTION.SPI_GETDESKWALLPAPER
            , 200
            , &wallPaperPath
            , 0);
    BackgroundImage = BitConverter.ToString(wallPaperPath.Name);
}

public struct FixedSizeCharStruct
{
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 200)]
    public byte[]? Name;

    public FixedSizeCharStruct()
    {

    }
}

I want to know how to construct parameters for this situation and how to use this method effectively