简体   繁体   中英

How to retrieve IFileOpenDialog interface in C# from IUnknown_QueryService SID_SExplorerBrowserFrame?

i'm trying to hook into file dialogs in my custom namespace extensions project. this is done in C#.

i'm trying to follow the post: http://blogs.msdn.com/b/winsdk/archive/2015/03/24/how-to-register-for-file-dialog-notifications-from-shell-namespace-extension.aspx

in C++ everything works, and i get the IFileOpenDialog interface: this is done under SetSite method:

HRESULT hr = IUnknown_QueryService(m_pUnkSite, SID_SExplorerBrowserFrame, IID_PPV_ARGS(&m_fileOpenDialog));

where m_fileOpenDialog is IFileOpenDialog

i'm trying to do the same in C#, but it doesn't work...

i've tried several ways:

FileDialogNative.IFileOpenDialog o2 = Marshal.GetObjectForIUnknown(m_pUnkSite) as FileDialogNative.IFileOpenDialog;

o2 is null.

i've tried

IntPtr ptr;
Guid g = new Guid("000214f1-0000-0000-c000-000000000046");
int rc = Marshal.QueryInterface(m_pUnkSite, ref g, out ptr);

this succeeds, but i have no idea how to convert the "ptr" into the required interface.

any help would be appriciated.

**Update from the comment **,

i tried doing this:

[DllImport("shlwapi.dll")]
internal static extern int IUnknown_QueryService(IntPtr pUnk, ref Guid guidService, ref Guid riid, out IntPtr ppvOut);


Guid g = new Guid("000214F1-0000-0000-C000-000000000046"); // SID_SExplorerBrowserFrame
Guid g2 = new Guid("d57c7288-d4ad-4768-be02-9d969532d960"); // IFileOpenDialog

IntPtr pp;
int rrc =  Win32.IUnknown_QueryService(pUnkSite, ref g, ref g2, out pp);

FileDialogNative.IFileOpenDialog o2 = Marshal.GetObjectForIUnknown(pp) as FileDialogNative.IFileOpenDialog;

this worked!!! thanks!!

So, Thanks to Hans Passant, i understood that the "IUnknown_QueryService" is not the same as what i tried.

i've managed to catch the interface in the following way:

[DllImport("shlwapi.dll")]
internal static extern int IUnknown_QueryService(IntPtr pUnk, ref Guid guidService, ref Guid riid, out IntPtr ppvOut);


Guid g = new Guid("000214F1-0000-0000-C000-000000000046"); // SID_SExplorerBrowserFrame
Guid g2 = new Guid("d57c7288-d4ad-4768-be02-9d969532d960"); // IFileOpenDialog

IntPtr pp;
int rrc =  Win32.IUnknown_QueryService(pUnkSite, ref g, ref g2, out pp);

FileDialogNative.IFileOpenDialog dlg = Marshal.GetObjectForIUnknown(pp) as FileDialogNative.IFileOpenDialog;
Marshal.Release(pp);

Then, i was able to use dlg :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM