0

i am newbie. i am getting this exception last 2 days and not get solved :( it runs fine in vs 2008 but when i execute exe file it gives Exception after some time

the exception is

System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at clienttesting.EventLoop.GetMessage(MSG& lpMsg, IntPtr hWnd, UInt32 wMsgFil terMin, UInt32 wMsgFilterMax) at clienttesting.EventLoop.Run() in D:\noman\windowsconsole\windowsconsole\Pr ogram.cs:line 196 at clienttesting.Program.Main() in D:\noman\windowsconsole\windowsconsole\Pro gram.cs:line 35

the code is

         public static void Run()
    {
        MSG msg = new MSG();

        sbyte ret;
        do
        {
            if ((ret = GetMessage(out msg, IntPtr.Zero, 0, 0)) != -1)
            {
                Thread.Sleep(1000);
                Console.WriteLine("the mesaaeg" + msg.Message.ToString());
                if (msg.Message == WM_QUIT)
                {
                    break;
                }
                if (ret == -1)
                {
                    break; //-1 indicates an error
                }
                else
                {

                    TranslateMessage(ref msg);

                    DispatchMessage(ref msg);
                }
            }
        } while (true);


    }

the Exception states

Noaman Akram
  • 3,680
  • 4
  • 20
  • 37
  • I'd have to say, if I found myself having to manually implement a windows message pump from C#, I'm doing something wrong. Why have you decided to pump messages? – Damien_The_Unbeliever Jun 20 '13 at 07:32
  • Also, your outer `if` condition means that the `if(ret==-1)` can never be true. – Damien_The_Unbeliever Jun 20 '13 at 07:37
  • i want to known user activities that what he is doing this is my task bro – Noaman Akram Jun 20 '13 at 07:38
  • before it i have done it with peekmessage but same exception occured public static void Run() { MSG msg; while (true) { while (PeekMessage(ref msg, 0, 0, 0, PeekMessageOption.PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(ref msg); DispatchMessage(ref msg); } } } – Noaman Akram Jun 20 '13 at 07:39
  • You might do better to look at e.g. [Handling Messages in Console Apps](http://msdn.microsoft.com/en-us/magazine/cc163417.aspx) which leverages all of the code that's already available in .NET Framework and means you only have to write managed code. – Damien_The_Unbeliever Jun 20 '13 at 07:44

1 Answers1

0

I know this question has been around for a long time, but I landed on this page while researching the same error. My scenario is a bit different, but hopefully this answer will help someone. I'm creating a .Net 6 console app, but I'm calling the same Win32 Api methods.

I'm using the PInvoke.Win32 nuget package, that way I don't have to do all the DllImports manually.

My error was that I didn't use the address-of operator '&' when passing the first argument to the GetMessage method. If you check out the documentation, you will see the examples also use the '&' in front of the msg parameter.

I see you use the out and ref parameter modifiers when calling GetMessage, TranslateMessage and DispatchMessage. Have you tried using the address-of operator '&' in stead?

Here's an example of the relevant parts using the new .Net 6 console template and the PInvoke.Win32 nuget package:

using PInvoke;

unsafe
{
    User32.MSG msg;
    while (User32.GetMessage(&msg, IntPtr.Zero, User32.WindowMessage.WM_NULL, User32.WindowMessage.WM_NULL) > 0)   // synchonous call, will block the thread till a message is received
    {
        User32.TranslateMessage(&msg);
        User32.DispatchMessage(&msg);
    }
}
FluffyBike
  • 825
  • 1
  • 4
  • 17