C# – P/Invoke for shell32.dll’s SHMultiFileProperties

cpinvokeshell32

I'm not very good with P/Invoke. Can anyone tell me how to declare and use the following shell32.dll function in .NET?

From http://msdn.microsoft.com/en-us/library/bb762230%28VS.85%29.aspx:

HRESULT SHMultiFileProperties(      
    IDataObject *pdtobj,
    DWORD dwFlags
);

It is for displaying the Windows Shell Properties dialog for multiple file system objects.

I already figured out how to use SHObjectProperties for one file or folder:

[DllImport("shell32.dll", SetLastError = true)]
static extern bool SHObjectProperties(uint hwnd, uint shopObjectType, [MarshalAs(UnmanagedType.LPWStr)] string pszObjectName, [MarshalAs(UnmanagedType.LPWStr)] string pszPropertyPage);

public static void ShowDialog(Form parent, FileSystemInfo selected)
{
    SHObjectProperties((uint)parent.Handle, (uint)ObjectType.File, selected.FullName, null));
}

enum ObjectType
{
    Printer = 0x01,
    File = 0x02,
    VoumeGuid = 0x04,
}

Can anyone help?

Best Answer

There's an IDataObject interface and a DataObject class in the .NET Framework.

[DllImport("shell32.dll", SetLastError = true)]
static extern int SHMultiFileProperties(IDataObject pdtobj, int flags);

public static void Foo()
{
    var pdtobj = new DataObject();

    pdtobj.SetFileDropList(new StringCollection { @"C:\Users", @"C:\Windows" });

    if (SHMultiFileProperties(pdtobj, 0) != 0 /*S_OK*/)
    {
        throw new Win32Exception();
    }
}

EDIT: I've just compiled and tested this and it works (pops up some dialog with folder appearance settings).