Reputation: 183
My issue is with a PropertySheetExtension, but the same behavior seems present with the default file properties sheet. The issue with the following code:
// Snippet from http://stackoverflow.com/a/1936957/124721
using System.Runtime.InteropServices;
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern bool ShellExecuteEx(ref SHELLEXECUTEINFO lpExecInfo);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct SHELLEXECUTEINFO
{
public int cbSize;
public uint fMask;
public IntPtr hwnd;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpVerb;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpFile;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpParameters;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpDirectory;
public int nShow;
public IntPtr hInstApp;
public IntPtr lpIDList;
[MarshalAs(UnmanagedType.LPTStr)]
public string lpClass;
public IntPtr hkeyClass;
public uint dwHotKey;
public IntPtr hIcon;
public IntPtr hProcess;
}
private const int SW_SHOW = 5;
private const uint SEE_MASK_INVOKEIDLIST = 12;
public static bool ShowFileProperties(string Filename)
{
SHELLEXECUTEINFO info = new SHELLEXECUTEINFO();
info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
info.lpVerb = "properties";
info.lpFile = Filename;
info.nShow = SW_SHOW;
info.fMask = SEE_MASK_INVOKEIDLIST;
return ShellExecuteEx(ref info);
}
Is that it opens the file properties sheet under the calling applications process, instead of explorer.exe. Due to this the property sheet closes when the application is closed, which is not the behavior I need. I need the sheet to remain open when the application exits. Another issue is if the application opens the property sheet, and then you right click on the file through explorer and click properties it will open a second, duplicate sheet.
I have tried using ShellExecuteEx and setting the parent window handle with explorer's main window, or from GetShellWindow, but that didn't help.
Is there another way to get this property sheet to open under the explorer.exe process?
Upvotes: 4
Views: 1590
Reputation: 101756
Your process is actually supposed to stick around until all shell threads have ended.
To do this you must implement a free threaded interface that supports IUnknown and tell Windows about it by calling SHSetInstanceExplorer
.
More information and a C++ example implementation can be found in this blog post.
Upvotes: 0