Does anybody know how to add options to the context menu of an specific EXE file?
Printable View
Does anybody know how to add options to the context menu of an specific EXE file?
anyone?
anyone?
You might already know this, but it's easy to add options to the context menu for a given extension, or for all extensions, using the Registry
But to add options for specific individual files? I don't know, I suspect not
Dr Memory :cool:
Yes... I knew that. I guess it cannot be done.
I know now that it CAN be done - still interested?
Yes, please.
Hi McBrain!
Just letting you know I'm still on the case - right now I'm in the middle of of a few other things - I'll have to put you on hold for maybe a couple of hours.
Meanwhile, can you describe the context? What would you actually like to happen when the user right-clicks one of your designated files in the Explorer window?
Cheers
Dr Memory :cool:
I want to have custom options for one EXE only... not for all EXEs, just for my app“s exefile.
hey, buddy, your signature block tends to hide your messages!!!!
Where were we, oh yes, I know we are just interested in a single exe, and you want to insert a number of options, either in the main body of the context menu, or perhaps a submenu would look cooler.....
I'll just make a fresh coffee, and write up my solution description, should be about 30 mins .... sorry for the delay, but hey, Rome wasn't built in a day (nor was Uki!)
Dr Memory :cool:
Serves me right for complaining! Now I get dizzy looking at the revolving chook!!!!!
OK, I've got a rather lengthy, but I hope, informative, analysis of the topic "Context Menus You Can Prepare At Home, in 67 easy steps!"
I hope it's not to big to post - I like to be thorough!!!!!
Here we go ......
When you first posted the question, I was just starting to get interested in context menus. I wanted to be able to open files with my own "Toolbox" programs.
So I learned about how I could get an option into the context menu which would run a program which would show a menu for the Toolbox.
This primitive but effective first step simply added a shell/command entry into the "*" and "folder" registry entries.
One of my tools was a directory searcher, and I wanted to be able to right-click on the files in its ListView window, and invoke the same context menu I would get if I was in Explorer.
I kinda hoped there would be a magic API function called something like ShellInvokeContextMenu(filename$).
Silly me! No such animal of course (although there's no reason why they couldn't give us one!)
So I asked myself, how does something like WinZip not only get an entry in the menu, but have a popup submenu of its own?
Looking in the registry, I see that WinZip's entry is not a shell/command but a shellex/ContextMenuHandlers and it's entry is a thing looking awfully like a Class GUID!
This was a bit intimidating at first, but I pursued it, searching for a clue as to whether a VB program could be a ContextMenuHandler.
OK, I'm coming to the point, trust me!!!!
About ContextMenuHandlers
What's a ContextMenuHandler, and how does it work? (Please excuse my clumsy descriptions and terms, I'm not a COM/OLE/TypeLib/Interface guru!)
1 It's an ActiveX DLL, and exposes a class called cMenu
2 The class Implements 2 interfaces, IContextMenu and IShellExtInit
3 When Explorer is building the context menu and finds ContextMenuHandlers in the registry, it uses the GUID value to create a cMenu object which activates the DLL.
4 It invokes the DLL's IContextMenu::QueryContextMenu method, which is basically asking our DLL, "Hey, do you want to insert some menu items?", and very kindly passes us the menu handle for the context menu it's building.
Cool stuff, eh? But how do we attach commands to the menu options we insert?
5 If the user selects one of our inserted options, Explorer invokes our IContextMenu::InvokeCommand method!
If it's that simple (well, relatively speaking, anyway!) why isn't it more commonly used by VB programmers?
One reason could be that the way OLE passes parameters and return values to/from OLE object methods is NOT the way VB normally does! For reasons to do with the Type Libraries, the interface methods in our DLL have to be declared as Sub's, but sometimes are also expected to return a result!
In the cMenu class, the IContextMenu::QueryContextMenu method has to tell Explorer how many items it inserted into the menu. This presents the VB ContextMenuhandler a problem, and one which has no obvious solution, which might explain why the VB forums aren't cluttered with VB examples!
Searching the Web, I found an article where some (cool) guy had been down this path, and he somehow learned that the main guy responsible for the design and development of OLE (chap named Curzon) let it be known that there WAS a way to get around this impasse.
The method, in a nutshell, is that our DLL includes a Function version of QueryContextmenu, which is written to return the correct result.
In the cMenu.Initialize procedure, the DLL goes to the object's VTable - this is a table of AddressOf(Interface Method)'s - and simply replaces the entry for the QueryContextMenu method, with the address of the replacement Function!!
To summarise: Yes, we CAN implement a ContextMenuHandler class in VB.
==================================================================================================== ===============
OK, but how does this solve McBrain's problem?
How can this be used to insert options only for specific files?
Here's the answer - I left out one item in the description above (for dramatic effect)
6 Before invoking our IContextMenu::InvokeCommand method, Explorer first invokes our IShellExtInit::Initialize method, and passes in all the object references we need to identify the file(s) that the user clicked!!!
Beautiful! If the object clicked was our special exe, we set a global flag so that IContextMenu::QueryContextMenu knows to insert our special option!
==================================================================================================== ===============
Now, McBrain, I have a 90%-complete VB ContextmenuHandler, which can be easily adapted to do just what you want. Sometime over the weekend I'll be able to send you a demo - but I'll need an email-address, OK?
Cheers to all readers of this spiel, I hope it's interesting, and please, if any of my comments are inaccurate, post your comments, and I'll update the notes.
Dr Memory :cool:
Boy, do I need a coffee!!!!!
I would love to get the demo... the email could be me at emilianomartin.com.ar
(Edit: I changed the email for harvesting hazards)
Wouldn't that still be for all files of a given extension not one particular file of an extension?
Thanks, Edneeis, you've reminded me that I have not yet kept my promise to McBrain to send him a demo ..... I must extract the digit and deliver....
Your question is answered above, but I forgive you if it isn't that clear - a case of technical-verbal-diaorrhea could be made...
The point is that the context-menu handler does not actually have to insert anything at all (or it can insert a menu option but have it disabled), and it can identify the file that was right-clicked.
So it looks at the filename, and if it's the target, it inserts an option, otherwise it does nothing (i.e. says "Pass").
Dr Memory :cool:
Actually I see that now, thanks :D .
Hi... I got the sample an manage to add two options (with their captions and icons) to my specific EXE. What I need to know is how can I get to detect which option was that the user clicked. Any ideas?
Mc Brain, you wouldn't happen to be able to attach the sample would you?
I'm able to do it... but since, MathImagics sent it to me I guess he should be the one who uploads it.
MathImagics, I need your help here. I can neither differenciate between each new option I added nor can the system (since both items shows the same "help" on the status bar) Is there any way to get rid of it.
Another thing, I tried to use to InsertMenuItem to create a parent menu (which would have all my options as their children) but it seems that the hwnd that it returns is not "good" (I get "1"), hence... it doesn't work as it should.
Please... I need a hand over here.
OK, everybody! My apologies, first of all - when I prepared the demo, I had to kind of strip it out from my own version, which does multiple options, and has a submenu, etc...
I thought, "I'll keep it simple" and assumed you'd only need a single option!
I will enhance the demo to show how multiple options (and multiple help texts) can be handled - I'll do it over the weekend, OK?
Dr Memory :cool:
OK... whenever you have time. I'll kkep fighting myself as well.
In the meantime, so that anybody can start "playing" with it:
Quote:
Originally posted by Mc Brain
I'm able to do it... but since, MathImagics sent it to me I guess he should be the one who uploads it.
OK guys, here's the all-singing, all-dancing demo...
It shows how to handle multiple options (including submenus), and gives explicit instructions on how to detect which option was clicked...
Dr Memory :cool:
Please, forgive me for the lack of comments on this, but I will only say one word about it: SUPERB!!!
Excellent work, mate!
yes awesome.....
Who, me? :eek:
Well, thanks... (blush) :rolleyes:
Dr Memory :cool:
Hmm, First off I would like to thank Jim White for the kick ass example.
Secondly, I changed the project name, class module name and module name edited a bunch of the code and compiled eth0.dll, then I made a batch withOnce I ran that I searched my registry for "eth0.CMain" (what I named project and class module) and found HKEY_CLASSES_ROOT\CLSID\{8CF8416D-386D-4194-878D-2A3D4179CFE5}Code:copy eth0.dll %systemroot%\eth0.dll
regsvr32 %systemroot%\eth0.dll
So then I made a .reg with the following in itRan the .reg, right clicked on e.png and saw no new menu.Code:[HKEY_CLASSES_ROOT\.jpg\shellex\ContextMenuHandlers\eth0]
@="{8CF8416D-386D-4194-878D-2A3D4179CFE5}"
[HKEY_CLASSES_ROOT\.jpeg\shellex\ContextMenuHandlers\eth0]
@="{8CF8416D-386D-4194-878D-2A3D4179CFE5}"
[HKEY_CLASSES_ROOT\.png\shellex\ContextMenuHandlers\eth0]
@="{8CF8416D-386D-4194-878D-2A3D4179CFE5}"
[HKEY_CLASSES_ROOT\.gif\shellex\ContextMenuHandlers\eth0]
@="{8CF8416D-386D-4194-878D-2A3D4179CFE5}"
When I first downloaded the example I did as the readme said and got the demo working but now that I have edited it I can't get it to work! :(
Want to say a very nice job by MathImagic.
When I run the attached zip file to this thread, it works. I do have 2 questions.
The image turns out fine when I do a right click on the folder listbox of Window Explorer but the image turns out BLACK when I do a right click on the file listbox of Window Explorer.
I believe this happens at the LoadImage function. Also, can I LoadImage from a file instead of a resource file?
Huh?
Reason for this is because in the code, there are couple lines that does Bolding. Comment that out and everything will be fine.Quote:
Originally Posted by Liquid Metal
Anyone know this one?Quote:
Originally Posted by Liquid Metal