473,408 Members | 2,734 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,408 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 7837
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.