Windows.Forms.Message - processing a user defined Windows-message - 2nd try 
Author Message
 Windows.Forms.Message - processing a user defined Windows-message - 2nd try

Hello,

I posted my problem a long time ago in the newsgroup and you (Lion Shi) replied me giving an example how to trap the
WM_KEYDOWN-message within a C#-program ... My problem now is, that I want to be able to trap a self-defined Windows-message from an
elder program written in VC++ 6.
Unfortunately neither PreProcessMessage nor WndProc ever receives the message - WndProc gets thousands of messages but never ever my
defined message ...
To verify that the message is sent I use the event of a button and make a PostThreadMessage-call with the approbriate parameters...

private const int WM_USER = 0x0400;
private const int MSG_COMMAND_TERMINATE = WM_USER + 200 + 4;

IntPtr wParamPtr = new IntPtr ((int) 0);
IntPtr lParamPtr = new IntPtr ((int) 0);
Win32.PostThreadMessage (AppDomain.GetCurrentThreadId (),  MSG_COMMAND_TERMINATE, wParamPtr,lParamPtr);

(Win32 is a wrapper-class which imports dll-functions from the user32.dll - I verified that PostThreadMessage() is working....)

I furtheron verified that AppDomain.GetCurrentThreadId() returns the same value (= the current threadID of my application) ... I
even used PostMessage() without any success...

I even tried using a higher "message"-number - which had no effect - of course...

So how in gods name do I receive user-defined messages from elder programs - the same way it worked in VC6...??

Sincerely

Tristan McLure



Fri, 15 Oct 2004 22:38:13 GMT  
 Windows.Forms.Message - processing a user defined Windows-message - 2nd try
Hello Tristan,

I have tried to capture the User-Define messages and it worked fine. You
may try the steps below:

1.      Create a C# Windows Application project
2.      Use the InteropServices name space.

using System.Runtime.InteropServices;

3.      Add a button to the form
4.      Double click the button and copy paste the following code:

[DllImport("user32.dll")]
private static extern bool SendMessage(
        IntPtr hWnd,      // handle to destination window
        UInt32 Msg,       // message
        Int32 wParam,  // first message parameter
        Int32 lParam   // second message parameter
        );

private const int WM_USER = 0x0400;
private const int MSG_COMMAND_TERMINATE = WM_USER + 200 + 4;

protected override void WndProc(ref Message m)
{
        if(m.Msg == MSG_COMMAND_TERMINATE)
                MessageBox.Show("Hello");
        base.WndProc(ref m);

Quote:
}

private void button1_Click(object sender, System.EventArgs e)
{
        SendMessage (this.Handle, MSG_COMMAND_TERMINATE, 0, 0);

Quote:
}

I hope it helps.

Best regards,

Lion Shi, MCSE, MCSD
Microsoft Support Engineer

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use.  2001 Microsoft Corporation. All rights
reserved.
--------------------

    Subject: Windows.Forms.Message - processing a user defined
Windows-message - 2nd try
    Date: Mon, 29 Apr 2002 16:38:13 +0200
    Lines: 31
    X-Priority: 3
    X-MSMail-Priority: Normal
    X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
    X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

    Newsgroups: microsoft.public.dotnet.languages.csharp
    NNTP-Posting-Host: pd9e7c7ee.dip0.t-ipconnect.de 217.231.199.238
    Path: cpmsftngxa08!cpmsftngxa07!tkmsftngxs01!tkmsftngp01!tkmsftngp04
    Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:57349
    X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

    Hello,

    I posted my problem a long time ago in the newsgroup and you (Lion Shi)
replied me giving an example how to trap the
    WM_KEYDOWN-message within a C#-program ... My problem now is, that I
want to be able to trap a self-defined Windows-message from an
    elder program written in VC++ 6.
    Unfortunately neither PreProcessMessage nor WndProc ever receives the
message - WndProc gets thousands of messages but never ever my
    defined message ...
    To verify that the message is sent I use the event of a button and make
a PostThreadMessage-call with the approbriate parameters...

    private const int WM_USER = 0x0400;
    private const int MSG_COMMAND_TERMINATE = WM_USER + 200 + 4;

    IntPtr wParamPtr = new IntPtr ((int) 0);
    IntPtr lParamPtr = new IntPtr ((int) 0);
    Win32.PostThreadMessage (AppDomain.GetCurrentThreadId (),  
MSG_COMMAND_TERMINATE, wParamPtr,lParamPtr);

    (Win32 is a wrapper-class which imports dll-functions from the
user32.dll - I verified that PostThreadMessage() is working....)

    I furtheron verified that AppDomain.GetCurrentThreadId() returns the
same value (= the current threadID of my application) ... I
    even used PostMessage() without any success...

    I even tried using a higher "message"-number - which had no effect - of
course...

    So how in gods name do I receive user-defined messages from elder
programs - the same way it worked in VC6...??

    Sincerely

    Tristan McLure



Mon, 18 Oct 2004 11:38:28 GMT  
 Windows.Forms.Message - processing a user defined Windows-message - 2nd try
Hello,

Unfortunately your approach does only work within one application but not
between different applications (VC6<->C#.NET)... so I'm still stuck in the
PostThreadMessage->WndProc-thing... I know that the message is sent via my
VC6-application (wrote another VC6-application which captures the message -
works fine) but my C#.NET application never receives the message...

Sincerely,

Tristan



Quote:
> Hello Tristan,

> I have tried to capture the User-Define messages and it worked fine. You
> may try the steps below:

> 1. Create a C# Windows Application project
> 2. Use the InteropServices name space.

> using System.Runtime.InteropServices;

> 3. Add a button to the form
> 4. Double click the button and copy paste the following code:

> [DllImport("user32.dll")]
> private static extern bool SendMessage(
> IntPtr hWnd,      // handle to destination window
> UInt32 Msg,       // message
> Int32 wParam,  // first message parameter
> Int32 lParam   // second message parameter
> );

> private const int WM_USER = 0x0400;
> private const int MSG_COMMAND_TERMINATE = WM_USER + 200 + 4;

> protected override void WndProc(ref Message m)
> {
> if(m.Msg == MSG_COMMAND_TERMINATE)
> MessageBox.Show("Hello");
> base.WndProc(ref m);
> }

> private void button1_Click(object sender, System.EventArgs e)
> {
> SendMessage (this.Handle, MSG_COMMAND_TERMINATE, 0, 0);
> }

> I hope it helps.

> Best regards,

> Lion Shi, MCSE, MCSD
> Microsoft Support Engineer

> This posting is provided "AS IS" with no warranties, and confers no
rights.
> You assume all risk for your use.  2001 Microsoft Corporation. All rights
> reserved.
> --------------------

>     Subject: Windows.Forms.Message - processing a user defined
> Windows-message - 2nd try
>     Date: Mon, 29 Apr 2002 16:38:13 +0200
>     Lines: 31
>     X-Priority: 3
>     X-MSMail-Priority: Normal
>     X-Newsreader: Microsoft Outlook Express 6.00.2600.0000
>     X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000

>     Newsgroups: microsoft.public.dotnet.languages.csharp
>     NNTP-Posting-Host: pd9e7c7ee.dip0.t-ipconnect.de 217.231.199.238
>     Path: cpmsftngxa08!cpmsftngxa07!tkmsftngxs01!tkmsftngp01!tkmsftngp04
>     Xref: cpmsftngxa08 microsoft.public.dotnet.languages.csharp:57349
>     X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

>     Hello,

>     I posted my problem a long time ago in the newsgroup and you (Lion
Shi)
> replied me giving an example how to trap the
>     WM_KEYDOWN-message within a C#-program ... My problem now is, that I
> want to be able to trap a self-defined Windows-message from an
>     elder program written in VC++ 6.
>     Unfortunately neither PreProcessMessage nor WndProc ever receives the
> message - WndProc gets thousands of messages but never ever my
>     defined message ...
>     To verify that the message is sent I use the event of a button and
make
> a PostThreadMessage-call with the approbriate parameters...

>     private const int WM_USER = 0x0400;
>     private const int MSG_COMMAND_TERMINATE = WM_USER + 200 + 4;

>     IntPtr wParamPtr = new IntPtr ((int) 0);
>     IntPtr lParamPtr = new IntPtr ((int) 0);
>     Win32.PostThreadMessage (AppDomain.GetCurrentThreadId (),
> MSG_COMMAND_TERMINATE, wParamPtr,lParamPtr);

>     (Win32 is a wrapper-class which imports dll-functions from the
> user32.dll - I verified that PostThreadMessage() is working....)

>     I furtheron verified that AppDomain.GetCurrentThreadId() returns the
> same value (= the current threadID of my application) ... I
>     even used PostMessage() without any success...

>     I even tried using a higher "message"-number - which had no effect -
of
> course...

>     So how in gods name do I receive user-defined messages from elder
> programs - the same way it worked in VC6...??

>     Sincerely

>     Tristan McLure



Mon, 18 Oct 2004 13:13:15 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Q: Threads and user-defined windows messages

2. sending user defined windows messages to a CCmdTarget derived class

3. User Defined Windows Messages

4. How do I send a user message to a windows process

5. Process Windows Messages during lengthy process

6. User defined messages and the message loop

7. handling user-defined messages (WM_USER) with message maps

8. WindowProc() not recieving Self defined Windows messages

9. WindowProc() not recieving Self defined Windows messages

10. WindowProc() not recieving Self defined Windows messages

11. Defining Windows Messages

12. WindowProc() not recieving Self defined Windows messages

 

 
Powered by phpBB® Forum Software