Home » Category » Microsoft Visual C#

Microsoft Visual C#: How to deal with access violation "0xc0000005" - TrackPopupMenu

100| Mon, 31 Dec 2007 18:39:00 GMT| aranius| Comments (5)

I'm writing program in c# .net 2.0 and I got access violation "0xc0000005" when i try call this line >

cmdID = TrackPopupMenu(hMenu, (TPM_RETURNCMD | TPM_LEFTALIGN), intX, intY, 0, hwnd, out mRect);

Do anyone know how to deal with this error or why is this violation showing?

the whole class, which I use to call the shell contextmenu is here> www.aranius.net/tmp/ContextMenu.cs

Keywords & Tags: deal, access, violation, quot, 0xc0000005, trackpopupmenu, microsoft, visual c#, vc

URL: http://msdn.itags.org/visual-csharp/91347/
 
«« Prev - Next »» 5 helpful answers below.

I'm guessing that you're not pinning managed objects before passing them to unmanaged functions. Thus when the GC rearranges memory of managed objects, the unmanaged code can't find the object. If this is the case, you may want to look into Interop a little more. If not, sorry to be a bother.

RayW

raywilfong | Fri, 07 Sep 2007 02:18:00 GMT |

I have tried to pinn almost everything but without any result.

The actual version is here > www.aranius.net/test/ContextMenu.cs

Now for first time I call this function then it goes well, it shows the context menu (not whole, but almost :D), but second time I call it the mentoined access violation apears. I realy don't know what to do... I will apriciate any help....

Aranius

aranius | Fri, 07 Sep 2007 02:19:00 GMT |

My guess would be that with your pinning, it is not long enough. If the unmanaged code is asynchronous, then you can not be sure when to unpin the mRect object. Using this assumption, your mRect needs to be static, pinned at the start of the problem and then unpinned at the end of the program, otherwise your mRect object can be moved, the unmanaged code is likely writing to the address it had from before (writing over another object with invalid data) then your managed code is trying to access the now corrupt memory and causing access violation. It's not so much the unmanaged code finding the object, but having the assumption that the object hasn't been moved, so it is free to write to that area and corrupt the data.

I had a similar thing happen to me with asynchronous P/Invoke calls and repeating them.

isshoufuuraibou | Fri, 07 Sep 2007 02:20:00 GMT |

In my second post is second version of my class, and there I have excluded mRect from TrackPopupMenu, where I have mRect replaced by IntPtr Test. (mRect is not used further)

I realy don't know what to do or what to pin... I have tryed to pinn parameters that uses this function, but didn't help....

Aranius

aranius | Fri, 07 Sep 2007 02:21:00 GMT |

Well when starting through the code I'm looking at every ref or out parameter to the P/Invoke calls.

I notice that you use SHGetDesktopFolder, I don't see you making it null/releasing it until you make the ShowContextMenu call again, the documentation for that function says you are responsible for releasing it with an IUnknown::Release call

SHGetDesktopFolder

P/Invoke describes interop

It looks like...

// TrackPopupMenu
[DllImport("user32.dll", EntryPoint = "TrackPopupMenu", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
public static extern int TrackPopupMenu(IntPtr hMenu, int wFlags, int x, int y, int nReserved, IntPtr hwnd, out IntPtr lprc);

becomes

// TrackPopupMenu
[DllImport("user32.dll", EntryPoint = "TrackPopupMenu", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
public static extern int TrackPopupMenu(IntPtr hMenu, uint wFlags, int x, int y, int nReserved, IntPtr hwnd, IntPtr lprc);

and your call

cmdID = TrackPopupMenu(hMenu, (TPM_RETURNCMD | TPM_LEFTALIGN), intX, intY, 0, hwnd, out Test);

should be

cmdID = TrackPopupMenu(hMenu, (TPM_RETURNCMD | TPM_LEFTALIGN), intX, intY, 0, hwnd, IntPrt.Zero);

or

cmdID = TrackPopupMenu(hMenu, (TPM_RETURNCMD | TPM_LEFTALIGN), intX, intY, 0, hwnd, null);

So your problem is either with the TrackPopupMenu still having out test, or actually with the SHGetDesktopFolder using the DesktopFolder object after it has been moved.

Unfortunately with interop access violation aren't always where they appear to be.

isshoufuuraibou | Fri, 07 Sep 2007 02:22:00 GMT |

Microsoft Visual C# Hot Answers

Microsoft Visual C# New questions

Microsoft Visual C# Related Categories