NO

Author Topic: Writing Windows Shell Context Menu Handlers in C  (Read 3749 times)

Miki

  • Guest
Writing Windows Shell Context Menu Handlers in C
« on: October 23, 2013, 03:29:26 PM »
Hi All,
I think that this post (My first post) is going to be more than just a question.

First, I'd like to say that I'm very happy to have found Pelles C. I stumbled onto it quite accidentally in my search for a free development environment for Microsoft Windows.
IMHO, it's an incredible feat of software engineering. Kudos to the developers of this fine product.

My initial objective was to learn how to write a Windows Context Menu Handler in plain C. I wanted to stay away from C++ because it would require a financial investment to purchase a version of Visual Studio that would allow me to write a shell extension in C++. I'm not creating a commercial product; I simply want to be able to create utilities that I can use, while at the same time brushing up on Windows programming concepts.

I was excited to find the "ContextMenuExt" example and the reworked 64 bit version (I am running Windows 7 64 bit). For what it was intended to do it works flawlessly.

So I decided to use it as a starting point for my shell extension. The first thing that I attempted to do is to make my context menu handler appear on the desktop right-click menu. That's where the problems began.

Upon reading through "the idiots guide to writing shell extensions" http://www.codeproject.com/Articles/441/The-Complete-Idiot-s-Guide-to-Writing-Shell-Extens
I found that controlling context menu behavior was as simple as adding registry entries (Or so I thought  :-[ )

According to the article, to make a context menu show itself on the desktop, simply create a key in HKCU\Directory\background\shellex\contextmenuhandlers and set its' default value to your GUID. However, if I do this, the handler crashes the Windows explorer.

I have tried to rework CShellInitExt_Initialize to no avail. I even tried compiling a pristeen version of contextmenuext.dll, registering it and adding the key manually; this will cause the explorer to crash as well.

I am at a loss as to why this is happening. Any insights would be welcome.

Best,
:Miki.

Miki

  • Guest
Re: Writing Windows Shell Context Menu Handlers in C
« Reply #1 on: October 23, 2013, 07:10:02 PM »
Okay I think I've got the problem all ironed out. :)

The culprit is here

Code: [Select]
STDMETHODIMP CShellInitExt_Initialize(IShellExtInit * this, LPCITEMIDLIST pidlFolder, LPDATAOBJECT lpdobj, HKEY hKeyProgID)
{
               ...
HRESULT hr = lpdobj->lpVtbl->GetData(lpdobj, &fe, &stgmed);
}

I believe that lpdobj is NULL when the user right-clicks on a directory background. This is what caused the Shell Extension to crash. :)

:Miki