473,320 Members | 1,829 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

IContextMenu InvokeCommand

I have made a shell extension dll in c# to add a menuitem to the explorer context menu

I have implemented these method
IShellExtInit.Initializ
IContextMenu.QueryContextMen
IContextMenu.GetCommandStrin
IContextMenu.InvokeComman

When I register the dll with regasm and gacutil a menuitem shows in the explorer contextmenu and
IShellExtInit.Initialize and IContextMenu.QueryContextMenu gets called. But GetCommandString and
InvokeCommand never gets called when I click the menu item

Can anyone explain whats going on

Thank
Lars
Nov 16 '05 #1
4 7830
Lars,
Can anyone explain whats going on?


It's hard to guess without seeing any code.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
a dll file consisting of the two files shellext.cs and ContextMenuHandler.c
shellext.c

using System
using System.Runtime.InteropServices

namespace ShellEx

// IUnknow
[ComImport(), GuidAttribute("00000000-0000-0000-c000-000000000046")
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )
public interface IUnknow

// IClassFactor
[ComImport(), GuidAttribute("00000001-0000-0000-c000-000000000046")
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )
public interface IClassFactor

// IShellExtIni
[ComImport(), GuidAttribute("000214e8-0000-0000-c000-000000000046")
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )
public interface IShellExtIni

[PreserveSig()
int Initialize (IntPtr pidlFolder, IntPtr lpdobj, uint /*HKEY*/ hKeyProgID)
// IContextMen
[ComImport(), GuidAttribute("000214e4-0000-0000-c000-000000000046")
InterfaceType(ComInterfaceType.InterfaceIsIUnknown )
public interface IContextMen

// IContextMenu method
[PreserveSig()
int QueryContextMenu(uint hmenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags)
[PreserveSig()
void InvokeCommand (IntPtr pici)
[PreserveSig()
void GetCommandString(int idcmd, uint uflags, int reserved, string commandstring, int cch)

ContextMenuHandler.c

using System
using System.Runtime.InteropServices
using Microsoft.Win32

using ShellExt

namespace ContextMenuHandle

[Guid("xxxxxxxx-xxxx-xxxx-xxxx-4F33EC089772"), ComVisible(true)
public class MyExtension : IShellExtInit, IContextMen

// *** Imports ************************************************** **

// MessageBo
[DllImport("user32")
static extern int MessageBox(int hWnd, string text, string caption, int type)

[DllImport("user32")
static extern bool InsertMenu( uint hMenu
uint uPosition
uint uFlags
int uIDNewItem
string lpNewIte
)

// *** Consts ************************************************** ***

const string guid = @"{xxxxxxxx-xxxx-xxxx-xxxx-4F33EC089772}"
const string regHome1 = @"my_file\shellex\ContextMenuHandlers"
const string regApproved = @"Software\Microsoft\Windows\CurrentVersion\She ll Extensions\Approved"
const int I_OK = 0
const int I_FALSE = 1

// *** Functions **************************************************

// Initializ
int IShellExtInit.Initialize (IntPtr /*LPCITEMIDLIST*/ pidlFolder, IntPtr /*LPDATAOBJECT*/ lpdobj, uint /*HKEY*/ hKeyProgID

MessageBox(0, "IShellExtInit.Initialize", "Information", 0)
return I_OK
// QueryContextMen
int IContextMenu.QueryContextMenu(uint hMenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags

MessageBox(0, "IContextMenu.QueryContextMenu", "Information", 0)
// MessageBox(0, hMenu.ToString()+" "+ iMenu.ToString()+" "+ idCmdFirst.ToString()+" "+ idCmdLast.ToString()+" "+ uFlags.ToString(), "Information", 0)
bool bResult = InsertMenu(hMenu, 0, 0, 1024, "Do &Stuff...")
// MessageBox(0, "Result = "+bResult.ToString(), "Information", 0)
return 1
// GetCommandStrin
void IContextMenu.GetCommandString(int idCmd, uint uFlags, int pwReserved, string commandString, int cchMax

MessageBox(0, "IContextMenu.GetCommandString", "Information", 0)
return
// InvokeComman
void IContextMenu.InvokeCommand(IntPtr pici

MessageBox(0, "IContextMenu.InvokeCommand", "Information", 0)
return
// *** COM Register/Unregister functions **************************

[ComRegisterFunction
public static void Register(System.Type t)

tr

RegistryKey regRoot = Registry.ClassesRoot
RegistryKey regKey

regRoot.CreateSubKey(regHome1)

regKey = regRoot.CreateSubKey(regHome1 + @"\MyCommandISE")
regKey.SetValue(string.Empty, guid)

// Add to list of approved shellextension
regRoot = Registry.LocalMachine

regKey = regRoot.OpenSubKey(regApproved, true)
regKey.SetValue(guid, "ContextMenu Extension")

// CloseKey
regKey.Close()
regRoot.Close();
}
catch(Exception ex)
{
MessageBox(0, ex.Message, "Exception Occured", 0);
}
}

[ComUnregisterFunction]
public static void UnRegister(System.Type t)
{
try
{
RegistryKey regRoot = Registry.ClassesRoot;
RegistryKey regKey;

regRoot.DeleteSubKeyTree(@"my_file\shellex");

// Remove from list of approved shellextensions
regRoot = Registry.LocalMachine;

regKey = regRoot.OpenSubKey(regApproved, true);
regKey.DeleteValue(guid);

regKey.Close();
regRoot.Close();
}
catch(Exception ex)
{
MessageBox(0, ex.Message, "Exception Occured", 0);
}
}
} // ContextMenuExtension
} // Namespace

Nov 16 '05 #3
Lars,
bool bResult = InsertMenu(hMenu, 0, 0, 1024, "Do &Stuff...");
Where did you get 1024 from? I believe you're supposed to use
idCmdFirst there rather than picking your own arbitrary command ID.

void IContextMenu.GetCommandString(int idCmd, uint uFlags, int pwReserved, string commandString, int cchMax)


I'd make commandString an IntPtr or possibly a StringBuilder.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4
found the problem! ;
int IContextMenu.QueryContextMenu(uint hMenu, uint iMenu, int idCmdFirst, int idCmdLast, uint uFlags

int idCmd = idCmdFirst
bool bResult = InsertMenu(hMenu, iMenu++, 1024, idCmd++, "Do &Stuff...")
return 1
1024 is the value of MF_BYPOSITIO
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: miles__ahead | last post by:
Hi. I like to create little utilities for Windows. It's nice to have Shell Integration, such as using the Context Menu. I've googled but haven't seen any Python example code for IContextMenu...
4
by: Lars Nielsen | last post by:
I have made a shell extension dll in c# to add a menuitem to the explorer context menu I have implemented these method IShellExtInit.Initializ IContextMenu.QueryContextMen...
1
by: shafeeque | last post by:
I am building a shell extn. project in .net ide which is converted from vC++ 6.00. During buid i am getting the following error "error C2787: 'IContextMenu' : no GUID has been associated with...
2
by: Rajko | last post by:
HRESULT QueryContextMenu( HMENU hmenu, UINT indexMenu, UINT idCmdFirst, UINT idCmdLast, UINT uFlags ); function gives me to small range of numbers !!! usually:
0
by: yxq | last post by:
Hello I have some code about shell extension, it works well, but when i choose more than a txt files and right-click, click open, system popup a dialog "InvokeCommand", system can not open the txt...
2
by: gilad | last post by:
Hi, I seem to be having a problem getting a context menu to work in Explorer. The menu item installs fine, but when I click it a message box should pop up indicating the command was received and...
0
by: lushdog | last post by:
Hi, i'm writing a shell extension in c# that will add two menu's to the right-click menu of explorer if any file is selected, i.e. it's registered in the * section of Classes in the registry. ...
0
by: lushdog | last post by:
Hi, i'm having a little problem with my context menu handler. Basically if i select multiple files and right-click OPEN or EDIT, it uses my context menu to open it. It is because i am missing this...
3
by: Poggs | last post by:
Hi everyone, I used the pattern for Shell Extensions for Context menu from Dino Esposito's article. However I implemented it in more than one application and it seems that those two right click...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.