473,657 Members | 2,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SendMessage works, PostMessage doesn't work.

When I use the SendMessage API I can sucessfully send and receive a user
defined message.
When I use the PostMessage API I can NOT sucessfully send and receive the
same user defined message.

I've got a C# class library project with two classes:

Class 1 is derives from : System.Windows. Forms.Form and overrides the base
WndProc method for the purpose of receiving and handeling user defined
messages:

protected override void WndProc(ref System.Windows. Forms.Message m)
{
switch (m.Msg)
{
case WM_MESSAGE1:
// do something here.
break;
case WM_MESSAGE2:
// do something here.
break;
default:
// do something here.
break;
}

Class 2 is created by Class 1 and runs as a background thread. At the
appropriate time Class 2 will either use:
SendMessage - when Class2 needs to wait for Class 1 to process the message
and possible return a response.
PostMessage - when Class2 does not need to wait for Class1 to processes
the message.

Class 2 uses this code to Send / Post the user defined messages:

[System.Runtime. InteropServices .DllImport("use r32.dll")]
private static extern Int32 SendMessage(Int Ptr hWnd, Int32 msg, Int32
wParam, IntPtr lParam);

[System.Runtime. InteropServices .DllImport("use r32.dll")]
private static extern Int32 PostMessage(Int Ptr hWnd, Int32 msg, Int32
wParam, IntPtr lParam);

private const Int32 WM_USER= (0x400);
private const Int32 WM_MESSAGE1 = (WM_USER + 101);
private const Int32 WM_MESSAGE2 = (WM_USER + 102);

// THIS CODE SUCCEEDS.
IntPtr lParam = System.IntPtr.Z ero;
int res = SendMessage(Cla ss1.Handle, WM_MESSAGE1, 0, lParam);

// THIS CODE FAILS.
IntPtr lParam = System.IntPtr.Z ero;
PostMessage(Cla ss1.Handle, WM_MESSAGE1, 0, lParam);

I'm logging the messages (m.Msg) that are arriving in Class1 - WndProc
function to a file for review. The log file shows that both the SendMessage
and PostMessage API calls cause the Class1 - WndProc method to fire, but in
the case of PostMessage the message that I'm looking for WM_MESSAGE1 never
arrives.

The log file also shows both the SendMessage and PostMessage cause exactly
the same additional messages to appear in the Class1 - WndProc method that I
did not explicitly issue. There are about 23 or so additional messages that
appear in the log prior to WM_MESSAGE1. But in the case of PostMessage
WM_MESSAGE1 never arrives. Just the other non-WM_MESSAGE1 messages appear in
the log.

Any idea why this might be?

Thanks in advance.
Nov 28 '05 #1
3 5083
>Class 2 is created by Class 1 and runs as a background thread. At the
appropriate time Class 2 will either use:
SendMessage - when Class2 needs to wait for Class 1 to process the message
and possible return a response.
PostMessage - when Class2 does not need to wait for Class1 to processes
the message.

Any reason you're not using the Invoke and BeginInvoke methods of the
form instead?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 28 '05 #2
Thanks for the suggestion.

I'm really in need of a solution to the issue of the PostMessage command not
working as it should.

Does anyone have any idea why SendMessage would work, but PostMessage doesn't?

Thanks again.

"Mattias Sjögren" wrote:
Class 2 is created by Class 1 and runs as a background thread. At the
appropriate time Class 2 will either use:
SendMessage - when Class2 needs to wait for Class 1 to process the message
and possible return a response.
PostMessage - when Class2 does not need to wait for Class1 to processes
the message.

Any reason you're not using the Invoke and BeginInvoke methods of the
form instead?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 29 '05 #3
>I'm really in need of a solution to the issue of the PostMessage command not
working as it should.

Does anyone have any idea why SendMessage would work, but PostMessage doesn't?


Can you post a complete sample that will let us reproduce the behavior
you're seeing?
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5791
by: newsgroups.bellsouth.net | last post by:
I was checking out SAPI, ms's speech recognition api, and thought it would be cool to write a quick app for bf1942 that would recognise voice commands and pass them to the game's commun. So if you say "roger" it would then send f1 f1 to bf1942 for it to send out the roger commun..I'm finding out that directinput is a bitch, uses a low level, so it ignores Sendkeys and sendmessage/postmessage. I've completed everything except sending the...
8
6681
by: g.franzkowiak | last post by:
Hello everybody, I've tryed to use an interprocess communication via SendMessage on Windows. Unfortunately, nothing goes on ######################################################################### #! /usr/bin/env python import win32api, win32ui, win32con
5
19400
by: Mark Overstreet | last post by:
I am trying to click a button in another window and I have it's hWnd value so I was trying to use Send message. Here is my code but it doesn't work as expected... response = Win32API.SendMessage(hWndYesButton,Win32API.BM_SETSTATE,0,null); response = Win32API.SendMessage(hWndYesButton,Win32API.WM_LBUTTONDOWN,1,"11"); response = Win32API.SendMessage(hWndYesButton,Win32API.WM_LBUTTONDOWN,1,null); response =...
2
6826
by: MCC | last post by:
I need to send messages to a VB 6.0 exe with SendMessage (from a C++ app). Anybody have sample code for a VB 6.0 message handler to receive SendMessage or PostMessage commands? Thanks.
0
1166
by: Stephan Steiner | last post by:
Hi I'm trying to automate Nortel's VPN Client. Unlike their competitors, the Nortel client doesn't output the results of a connection attempt to the commandline, but instead uses PostMessage commands to send the connection status back. When you launch the client, you can tell it to which hwnd the message should be sent, and which message number should be used. I write a simple windows form app that catches the message in question, then...
11
48033
by: NVergunst | last post by:
Hello everyone. I have what I believe to be a simple question, yet I can not find anything that is helpful to me. Basically I have an application, that I want to use to control external applications. My program will run either embedded within another program or minimized to the system tray. So SendKeys is out because it only works if the controlled application has focus, which it will not. So my conclusion is the windows API. I have...
8
1715
by: Dr1ZZ | last post by:
Hi, I'm currently working on a bot for a game. It works like this: 1: Take a picture of the current playing field 2: Do the calculations on what i gotta do (best move) 3: Use sendmessage to send a mouseclick to the game at the correct position
14
12632
by: Kerem Gümrükcü | last post by:
Hi, i want to emulate a (synchronous) BroadCastSystemMessage with EnumWindows and SendMessage. I dont want to use the BroadcastSystemMessage because it needs the SE_TCB_NAME Privilege (you dont have this in normal). So i decided to use the EnumWindows with SendMessage. What i try here is to pass a WM_USER+1111 with LParam and WParam, pointing to strings. But when i try to get the strings at the other end with Marshal.PtrToStringAnsi,
2
5509
by: alag20 | last post by:
Hi Guys, I need to send a string from c# application to another c# Application. On receiving application side the code i have is below. protected override void WndProc(ref Message m) { switch (m.Msg)
0
8421
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8621
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7354
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.