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