IContextMenu InvokeCommand

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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\Shell 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
 
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
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top