Having installed VS2005 pro yesterday (MSDN download version) I am
gradually moving my VS2003 stuff over. I am having a problem with
IContextMenu3.HandleMenuMsg (called when the Explorer Context Menu is
shown) although it worked fine in VS2003. I get an error:
System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often
an indication that other memory is corrupt."
Source="JGLibDLL"
StackTrace:
at JGLibDLL.IContextMenu2.HandleMenuMsg(UInt32 uMsg, UInt32
wParam, UInt32 lParam)
... rest of trace omitted.
It is called from WndProc:
protected override void WndProc(ref Message message)
{
// NB call base first or some user drawn menu items will not display
properly!
base.WndProc(ref message);
const int WM_DRAWITEM = 0x002B;
const int WM_MEASUREITEM= 0x002C;
const int WM_INITMENUPOPUP= 0x0117;
const int WM_MENUCHAR = 0x0120;
IntPtr lpResult = IntPtr.Zero;
switch (message.Msg)
{
case WM_DRAWITEM:
case WM_MEASUREITEM:
case WM_INITMENUPOPUP:
if(m_IContextMenu3 != null)
m_IContextMenu3.HandleMenuMsg((uint)message.Msg,
(uint)message.WParam, (uint)message.LParam);
break;
case WM_MENUCHAR:
if(m_IContextMenu3 != null)
m_IContextMenu3.HandleMenuMsg2((uint)message.Msg,
(uint)message.WParam, (uint)message.LParam, lpResult);
break;
default:
break;
}
}
The Interfaces are defined:
[ComImport]
[Guid("000214e4-0000-0000-c000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IContextMenu : IUnknown
{
[PreserveSig()]
int QueryContextMenu(uint hMenu, uint indexMenu, int idCmdFirst, int
idCmdLast, uint uFlags);
[PreserveSig]
int InvokeCommand(ref CMINVOKECOMMANDINFO lpici) ;
[PreserveSig()]
void GetCommandString(int idcmd, uint uflags, int reserved, IntPtr
commandstring, int cch);
}
[ComImport]
[Guid("000214F4-0000-0000-c000-000000000046")]
public interface IContextMenu2 : IContextMenu
{
[PreserveSig]
int HandleMenuMsg(uint uMsg, uint wParam, uint lParam);
}
[ComImport]
[Guid("bcfce0a0-ec17-11d0-8d10-00a0c90f2719")]
public interface IContextMenu3 : IContextMenu2
{
[PreserveSig]
int HandleMenuMsg2(uint uMsg, uint wParam, uint lParam, IntPtr
plResult);
}
Can anybody suggest what I need to do to resolve this please?
The project was converted by VS2005 with no error messages but it is
obviously not happy with something!

Signature
Jeff Gaines
Mattias Sjögren - 17 Nov 2005 17:53 GMT
Hi Jeff,
>I am having a problem with
>IContextMenu3.HandleMenuMsg (called when the Explorer Context Menu is
>shown) although it worked fine in VS2003.
I'm surprised it ever worked, it shouldn't. Interface inheritance
doesn't work the same way in COM and .NET, so making IContextMenu2
derive from IContextMenu doesn't really help. You have to include all
IContextMenu methods in IContextMenu2.
Mattias

Signature
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jeff Gaines - 17 Nov 2005 22:07 GMT
> Hi Jeff,
>
[quoted text clipped - 6 lines]
> derive from IContextMenu doesn't really help. You have to include all
> IContextMenu methods in IContextMenu2.
Hi Mattias!
Thanks for coming to the rescue again :-)
Certainly it did work under VS2003, my 'ShowContextMenu' function has
your notes from November 2003 embedded in it!
I have re-defined the interfaces and included ICM in ICM2 and ICM2 in
ICM3 with the 'new' keyword. There was still a problem until I altered
the end of WndProc as follows:
....
default:
base.WndProc(ref message);
break;
i.e. it only calls base.WndProc when it hasn't called any of the
ICM2/ICM3 functions. I don't know if that makes sense, there's really
very little about still relating to interfaces and C#.
Thanks again!

Signature
Jeff Gaines