none
P/Invoke into SHGetSetFolderCustomSettings RRS feed

  • Question

  • Hi developers,

     

    I am trying to P/Invoke into SHGetSetFolderCustomSettings from my C# application. The call, unfortunately, appears to be not working and I'm not sure why.

     

    SHGetSetFolderCustomSettings is documented at http://msdn2.microsoft.com/en-us/library/bb762199(VS.85).aspx.

     

    It looks like this:

    HRESULT SHGetSetFolderCustomSettings(      
        LPSHFOLDERCUSTOMSETTINGS pfcs,
        LPCTSTR pszPath,
        DWORD dwReadWrite
    );

     

    SHFOLDERCUSTOMSETTINGS  looks like this:

    typedef struct {
        DWORD dwSize;
        DWORD dwMask;
        SHELLVIEWID *pvid;
        LPTSTR pszWebViewTemplate;
        DWORD cchWebViewTemplate;
        LPTSTR pszWebViewTemplateVersion;
        LPTSTR pszInfoTip;
        DWORD cchInfoTip;
        CLSID *pclsid;
        DWORD dwFlags;
        LPTSTR pszIconFile;
        DWORD cchIconFile;
        int iIconIndex;
        LPTSTR pszLogo;
        DWORD cchLogo;
    } SHFOLDERCUSTOMSETTINGS, *LPSHFOLDERCUSTOMSETTINGS;

     

    I've implemented the P/Invoke like this:

    [DllImport("shell32.dll")]

    static extern IntPtr SHGetSetFolderCustomSettings([In] ref SHFOLDERCUSTOMSETTINGS pSettings,

    string pszPath, SHFOLDERCUSTOMSETTINGS_READWRITE dwReadWrite);

     

    SHFOLDERCUSTOMSETTINGS looks like this:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]

    struct SHFOLDERCUSTOMSETTINGS

    {

    public uint dwSize;

    public FOLDERCUSTOMSETTINGSMASK dwMask;

    public IntPtr pvid;

    [MarshalAs(UnmanagedType.LPWStr)]

    public string pszWebViewTemplate;

    public uint cchWebViewTemplate;

    [MarshalAs(UnmanagedType.LPWStr)]

    public string pszWebViewTemplateVersion;

    [MarshalAs(UnmanagedType.LPWStr)]

    public string pszInfoTip;

    public uint cchInfoTip;

    public IntPtr pclsid;

    public uint dwFlags;

    [MarshalAs(UnmanagedType.LPWStr)]

    public string pszIconFile;

    public uint cchIconFile;

    public uint iIconIndex;

    [MarshalAs(UnmanagedType.LPWStr)]

    public string pszLogo;

    public uint cchLogo;

    }

     

    And SHFOLDERCUSTOMSETTINGS_READWRITE looks like this:

    [Flags]

    enum SHFOLDERCUSTOMSETTINGS_READWRITE : uint

    {

    FCS_READ = 0x0001,

    FCS_FORCEWRITE = 0x0002,

    FCS_WRITE = FCS_READ | FCS_FORCEWRITE,

    }

     

    Finally, FOLDERCUSTOMSETTINGSMASK looks like this:

    [Flags]

    enum FOLDERCUSTOMSETTINGSMASK : uint

    {

    FCSM_VIEWID = 0x0001,

    FCSM_WEBVIEWTEMPLATE = 0x0002,

    FCSM_INFOTIP = 0x0004,

    FCSM_CLSID = 0x0008,

    FCSM_ICONFILE = 0x0010,

    FCSM_LOGO = 0x0020,

    FCSM_FLAGS = 0x0040,

    }

     

    I do the following when I make the call:

    SHFOLDERCUSTOMSETTINGS settings = new SHFOLDERCUSTOMSETTINGS();

    settings.dwSize = (uint)Marshal.SizeOf(typeof(SHFOLDERCUSTOMSETTINGS));

    settings.dwMask = FOLDERCUSTOMSETTINGSMASK.FCSM_ICONFILE | FOLDERCUSTOMSETTINGSMASK.FCSM_INFOTIP;

    settings.pszIconFile = @"C:\Windows\notepad.exe";

    settings.pszInfoTip = "A text folder.";

    settings.iIconIndex = 0;

    IntPtr ptr = DesktopIniManager.SHGetSetFolderCustomSettings(ref settings, @"C:\test", SHFOLDERCUSTOMSETTINGS_READWRITE.FCS_FORCEWRITE);

     

    ptr ends up being 0. A desktop.ini file does not get generated in C:\test.

     

    I am wondering if I made a mistake converting the API call to a P/Invokable .NET call. Is there anything wrong with this declaration?

     

    Thank you,

    Don

    Tuesday, March 11, 2008 11:32 AM

Answers

  • You should add CharSet=CharSet.Auto to the DllImport attribute.

     

    Also, the HRESULT return type maps to uint in C#, not IntPtr. And if the return value is zero (S_OK) it means the functon succeeds.

     

     

    Tuesday, March 11, 2008 11:41 AM
    Moderator

All replies

  • You should add CharSet=CharSet.Auto to the DllImport attribute.

     

    Also, the HRESULT return type maps to uint in C#, not IntPtr. And if the return value is zero (S_OK) it means the functon succeeds.

     

     

    Tuesday, March 11, 2008 11:41 AM
    Moderator
  • That was it!! Thanks so much!
    Tuesday, March 11, 2008 11:54 AM
  • Hi
    I Use Your code and work correctly to change folder setting .but when i use this code with paremeter 

    SHFOLDERCUSTOMSETTINGS_READWRITE.FCS_READ to run the SHGetSetFolderCustomSettings for get GUID of Foders like "C:\Test" , no fodersetting return . please help me,to get guid of folders.
    thanks a lot.

    Thursday, December 25, 2008 10:19 PM