0

I'm trying to preview files within my WinForms app. In this particular case, I have Office apps installed and I'm trying to preview xls or doc files.

I'm getting the previewer handler keys for all extensions by crawling the following registry keys:

"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\PreviewHandlers"
"HKEY_CLASSES_ROOT\{0}\shellex\{{8895b1c6-b41f-4c1c-a562-0d564250836f}}"
"HKEY_CLASSES_ROOT\SystemFileAssociations\{0}\shellex\{{8895b1c6-b41f-4c1c-a562-0d564250836f}}"

With {0} being the file extension.

I do find registered preview handlers for pdf, txt or rtf using the first key above. On my machine I only have 12 preview handlers registered...

enter image description here

So for example for .doc and .docx I don't have a ShellEx stub:

enter image description here

However, I can't located the preview handlers for xls or doc. Outlook can easily show previews of these files with messages. If I follow the only value that makes sense, I would go to:

enter image description here

So where exactly are the preview handlers registered?

Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
Ivan-Mark Debono
  • 15,500
  • 29
  • 132
  • 263
  • In the PreviewHandlers list, take the `CLSID` GUID of the, e.g., Microsoft Word Previewer, then you can find it's location in the `LocalServer32` key in `HKEY_CLASSES_ROOT\CLSID\{The-CLSID-GUID}\LocalServer32\(Default)` (-> `(Default)` equals to `null`). – Jimi Oct 04 '20 at 06:19
  • The same `CLSID` is the `(default)` string value of the key (`{8895b1c6-b41f-4c1c-a562-0d564250836f}` is the Proxy Server32 interface stub) that you find in, e.g., `HKEY_CLASSES_ROOT\.docx\shellex\{8895b1c6-b41f-4c1c-a562-0d564250836f}\(default)`. – Jimi Oct 04 '20 at 06:37
  • @Jimi That's what I was trying to do. But there's not even a `ShellEx` key. I amended my question with further details. – Ivan-Mark Debono Oct 04 '20 at 06:57
  • In `HKEY_CLASSES_ROOT\.docx\...` you don't have the `ShellEx` key? How come? Wait, you don't have a PreviewHandler for Microsoft Word there. Is Office installed? – Jimi Oct 04 '20 at 07:00
  • @Jimi. I wouldn't know why. That's what I have. Maybe it uses the `OpenWithProgIds` key. I have Windows 10 and Office 365. – Ivan-Mark Debono Oct 04 '20 at 07:02
  • Office 365, all right, see [Preview Handlers and Shell Preview Host](https://learn.microsoft.com/en-us/windows/win32/shell/preview-handlers), [How to Register a Preview Handler](https://learn.microsoft.com/en-us/windows/win32/shell/how-to-register-a-preview-handler) & Co. See also under, e.g., `Word.Document.X\CLSID` where that goes, you should find a `LocalServer32` there, too. – Jimi Oct 04 '20 at 07:08
  • What exactly should I look at? Should Word & Co. register themselves as preview handlers. A Word document can be previewed in Outlook or Explorer. So the registration is already available. Where and how to access it is still unknown. And I didn't find an entry for `Word.Document.X` – Ivan-Mark Debono Oct 04 '20 at 07:13
  • `Word.Document.X\CLSID\(default)` should point to another `CLSID` in (both, probably) `HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{The-CLSID-GUID}` and `HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{The-CLSID-GUID}` and also to `HKEY_CLASSES_ROOT\CLSID\{The-CLSID-GUID}`. I don't have Office 365 here so I cannot go see where its PreviewHandlers are registered. – Jimi Oct 04 '20 at 07:17
  • @Jimi `Word.Document.X` not found in the registry!! – Ivan-Mark Debono Oct 04 '20 at 07:23
  • Well, did you search `Word.Document.X` exactly? It could be `Word.Document.8`, `Word.Document.12` etc. As mentioned, I don't have Office 365 installed here and I don't know what kind of installation you have there. Search the registry for a know path (where Office is installed), you'll find it. – Jimi Oct 04 '20 at 07:26
  • @Jimi Ok I have `Word.Document.8` and `Word.Document.12`. I have found a CLSID, and when I search for it there are no keys/values related to a PreviewHandler – Ivan-Mark Debono Oct 04 '20 at 07:39
  • Don't use the registry, there's an API for that: AssocQueryString https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-assocquerystringw with a code like `AssocQueryString(ASSOCF_INIT_DEFAULTTOSTAR,ASSOCSTR_SHELLEXTENSION,".xls","{8895b1c6-b41f-4c1c-a562-0d564250836f}"...)` – Simon Mourier Oct 04 '20 at 07:43
  • See whether you have a `HKEY_CLASSES_ROOT\Word.Document\CLSID` and read the `(default)` string value of that key. As @Simon Mourier said, `AssocQueryString` can be useful (unless you really need to inspect the Registry, that is). See the notes and the simplified wrapper methods here, for example: [Process.Start a file without Extension](https://stackoverflow.com/a/47763682/7444103). The `ASSOCF` and `ASSOCSTR` enumerations are included. – Jimi Oct 04 '20 at 07:49
  • @SimonMourier I do get the CLSID when passing `AssocStr.ShellExtension` for `.pdf`. However not for `.doc` or `.docx`. Which brings us back to the original problem where the preview handler is registered somewhere else. – Ivan-Mark Debono Oct 04 '20 at 07:59
  • Does it work in Windows Explorer when you enable the Preview Pane (in the View command bar)? Is your application using the same bitness as the handler (ie 32bit vs 64bit)? – Simon Mourier Oct 04 '20 at 08:12
  • @SimonMourier Yes it works in Explorer. I've tried 32, 64 and any cpu. My WinForms app is a ClickOnce app with full trust. Preview for PDF within the app works just fine. – Ivan-Mark Debono Oct 04 '20 at 09:00
  • https://learn.microsoft.com/en-us/windows/win32/api/shlwapi/nf-shlwapi-assocquerystringw – Hans Passant Oct 04 '20 at 13:13
  • AssocQueryString works for pdf but not for office fike types. Explorer and Outlook must get their previews in some other way and with the shell api. – Ivan-Mark Debono Oct 04 '20 at 14:55
  • I can't reproduce your issue (I also have Office 365 and AssocQueryString works fine). However, it's now possible the preview is handled by an UWP/Store app. In this case, this app would be returned by SHAssocEnumHandlers and then the app manifest must declare 'desktopPreviewHandler' extension: https://learn.microsoft.com/en-us/uwp/schemas/appxpackage/uapmanifestschema/element-desktop2-desktoppreviewhandler and https://blogs.windows.com/windowsdeveloper/2017/03/31/desktop-bridge-creators-update/ you can then read the value of the CLSID from the manifest. Lots of work ahead. – Simon Mourier Oct 05 '20 at 10:53

0 Answers0