Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In my windows application I have overridden one windows application function WndProc. It's to enable Inter-Process-Communication between two exes through windows messages. It's working fine, But, Sometimes, Application crashes at this line base.WndProc(ref m); I dont know what causes the error. Can anybody tell me please. Is there any problem will happen if I remove this line ??
here is the code..

C#
protected override void WndProc(ref Message m)
     {
         switch (m.Msg)
         {
             case Win32.WM_COPYDATA:
                 Win32.CopyDataStruct st = (Win32.CopyDataStruct)Marshal.PtrToStructure(m.LParam, typeof(Win32.CopyDataStruct));
                 string strData = Marshal.PtrToStringUni(st.lpData);

                 if (strData == "msgfromclientexe")
                     timer2.Enabled = true;
                 break;
             default:
                 // let the base class deal with it
                 base.WndProc(ref m);
                 break;
         }
     }


update:

When I dont use base.WndProc(ref m)
MY main function throws an error "Error creating handle"
at this line

App myApp = new App();
myApp.Run(args);
Posted
Updated 27-May-14 3:00am
v2
Comments
johannesnestler 27-May-14 8:54am    
any inner exceptions on your exception?
johannesnestler 27-May-14 8:55am    
If you don't handle the message - you want "default handling", so no - you can't just "leave it away" (the base.WndProc call)
Yesudasan Moses 27-May-14 9:00am    
When I dont use base.WndProc(ref m)
MY main function throws an error "Error creating handle"
at this line

App myApp = new App();
myApp.Run(args);
Yesudasan Moses 27-May-14 9:09am    
this error happens sometimes only,,, but it lets the released application to crash. I will check the inner exception next time and update it.. thanks :)

1 solution

You don't call that in your default case handler. You call it always, either at the top of your override code or at the bottom, depending on if you want the base default code to handle the message you're overriding or not. But, it either case, the base code must be called to handle all of the messages you're not handling.

THe code below will allow the base code to handle the CopyData message as well as letting your code handle it, adding to the functionality provided by the base code.

C#
protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        switch (m.Msg)
        {
            case Win32.WM_COPYDATA:
                Win32.CopyDataStruct st = (Win32.CopyDataStruct)Marshal.PtrToStructure(m.LParam, typeof(Win32.CopyDataStruct));
                string strData = Marshal.PtrToStringUni(st.lpData);

                if (strData == "msgfromclientexe")
                    timer2.Enabled = true;
                break;
        }
    }

This makes sure that the base code gets a crack at the message before you try to do anything with it.
 
Share this answer
 
Comments
Yesudasan Moses 27-May-14 10:22am    
Thanks,, i i will try it and update you,,, :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900