473,651 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PostMessage problem

In my code I have a problem in abtaining a windows handle. I do not
know the namespace to obtain the windows hanle. here is the code.

using System;
using System.Collecti ons;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

namespace mynamespace
public class HandleMessage : System.Windows. Forms.NativeWin dow
{
[DllImport("user 32.dll", CharSet=CharSet .Auto,
SetLastError=tr ue)]

public static extern bool PostMessage(
IntPtr hWnd,
[MarshalAs(Unman agedType.U4)] int Msg,
IntPtr wParam,
IntPtr lParam);

private Queue myqueue;
private CMsgDelegate ReceiveMessage;

public HandleMessage(M sgDelegate ReceiveMessage)
{
myqueue = new Queue();
this.ReceiveMes sage = ReceiveMessage;
}

public void PutinQue(Messag e msg)
{
queue.PutElemen t(msg);

System.Windows. Forms.Message msg =
System.Windows. Forms.Message.C reate(this.Hwnd ,0

,(IntPtr)0,(Int Ptr)0);
PostMessage (******);

private Message GetFromQue()
{
return (Message)queue. GetElement();
}

protected override void WndProc(ref

System.Windows. Forms.Message msg)
{

this.ReceiveMes sage(GetFromQue ());
}
}

Having inherited the class from System.Windows. Forms.NativeWin dow does
not give me windows handle. Can anyone help.

Thanks in advance
Nov 15 '05 #1
15 8442
You can use GetForegroundWi ndow() function in user32.dll to get the window
handle for the active window.
--
With Regards,
Deepak
[I code, therefore I am]
http://deepakictim.blogspot.com
"James" <ja************ *****@yahoo.com .au> wrote in message
news:e6******** *************** ***@posting.goo gle.com...
In my code I have a problem in abtaining a windows handle. I do not
know the namespace to obtain the windows hanle. here is the code.

using System;
using System.Collecti ons;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

namespace mynamespace
public class HandleMessage : System.Windows. Forms.NativeWin dow
{
[DllImport("user 32.dll", CharSet=CharSet .Auto,
SetLastError=tr ue)]

public static extern bool PostMessage(
IntPtr hWnd,
[MarshalAs(Unman agedType.U4)] int Msg,
IntPtr wParam,
IntPtr lParam);

private Queue myqueue;
private CMsgDelegate ReceiveMessage;

public HandleMessage(M sgDelegate ReceiveMessage)
{
myqueue = new Queue();
this.ReceiveMes sage = ReceiveMessage;
}

public void PutinQue(Messag e msg)
{
queue.PutElemen t(msg);

System.Windows. Forms.Message msg =
System.Windows. Forms.Message.C reate(this.Hwnd ,0

,(IntPtr)0,(Int Ptr)0);
PostMessage (******);

private Message GetFromQue()
{
return (Message)queue. GetElement();
}

protected override void WndProc(ref

System.Windows. Forms.Message msg)
{

this.ReceiveMes sage(GetFromQue ());
}
}

Having inherited the class from System.Windows. Forms.NativeWin dow does
not give me windows handle. Can anyone help.

Thanks in advance

Nov 15 '05 #2
Hi James,
I'm not quite getting your problem and what you are trying to achieve with
the code you posted.
However,
In my code I have a problem in abtaining a windows handle. I do not
know the namespace to obtain the windows hanle. here is the code.
What window handle do you want to obtain? I'm not quite sure what do
namespaces have to do with the window handle.

Having inherited the class from System.Windows. Forms.NativeWin dow does
not give me windows handle. Can anyone help.


Having your class inherited form NativeWindow you have property called
Handle. This very property gives you the window handle of the underlying
Windows OS native window. However it is your responsibility to give that
property value (its default value is 0 (NULL))

Instantiating your class with the *new* operator doesn't create the Windows
OS native window (and respectively the handle (HWND)). You have to create
either new Windows OS native windows calling NativeWindow.Cr eateHandle
method or attach the object to already existing one -
NativeWindow.As signHandle.

I don't see such calls in your code.

Maybe I'm not getting your question correctly so it will be helpful if you
provide more information about what you are trying to achieve.

B\rgds
100
Nov 15 '05 #3
I think I did not state the problem clearly. I have a separate form
and that form is on a seperate thread started by a main program. In
the form I am sending messages. And I need a way to call a receive
method to process those messages. So what I do is once I send a
message through the form I call Enque()method and inside I Post a
dummy message to windows and when I capture that message in WndProc I
will call the Receic\ve method to process that message. The problem I
am having is that the PostMessage method. I need a way to get the
handle of the parent window which is the form(Form calls Enque() in
MessageHandler) .How do I do that, I need that in CreateParams.
using System;
using System.Collecti ons;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

public class CMessageHandler : System.Windows. Forms.NativeWin dow
{
[DllImport("user 32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]

public static extern bool PostMessage(
IntPtr hWnd,
[MarshalAs(Unman agedType.U4)] int Msg,
IntPtr wParam,
IntPtr lParam);

private Queue queue;
private CMessageDelegat e receiveMessage;

public CMessageHandler (CMessageDelega te receiveMessage)
{
queue = new Queue();
this. receiveMessage = receiveMessage;
}

public void Enqueue(CMessag e message)
{
queue.Enqueue(m essage);

CreateParams cp = new CreateParams();
cp.Parent = this.Handle; ?????? here is the problem how do I
get the handle for the form????
CreateHandle(cp );

PostMessage(thi s.Handle,99999, (IntPtr)0,(IntP tr)0);

}

private CMessage GetMessage()
{
return (CMessage)queue .Dequeue();
}
protected override void WndProc(ref Message msg)
{
if (msg.Msg == 99999)
{
this.decodeMess age(GetMessage( ));
base.WndProc(re f msg);

}
}
}
Nov 15 '05 #4
I actually changed the code a bit. What happens is once it reaches
this.CreateHand le(cp); statement it jumps to WndProc() without
executing the PostMessage((In tPtr)this.Handl e,1234,(IntPtr) 2,(IntPtr)3)
statement. What am I doing wrong.

Thanks
using System.Collecti ons;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

public class CMessageHandler : System.Windows. Forms.NativeWin dow
{

[DllImport("user 32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
public static extern bool PostMessage(
IntPtr hWnd,
[MarshalAs(Unman agedType.U4)] int Msg,
IntPtr wParam,
IntPtr lParam);

private Queue queue;
private CMessageDelegat e receiveMessage;

public CMessageHandler (CMsgDelegate receiveMessage)
{
queue = new Queue();
this.receiveMes sage = receiveMessage;
}

public void Enqueue(CMessag e message)
{
queue.Enqueue(m essage);

CreateParams cp = new CreateParams();

cp.Parent = (IntPtr)this.Ha ndle;

this.CreateHand le(cp);

PostMessage((In tPtr)this.Handl e,1234,(IntPtr) 2,(IntPtr)3);

}

private CMessage GetMessage()
{
return (CMessage)queue .Dequeue();
}

protected override void WndProc(ref Message msg)
{
if (msg.Msg == 1234)
{
this.receiveMes sage(GetMessage ());
}
}
}
Nov 15 '05 #5
Hi James,

I think I got your idea. What I got is:
You have a form and you want this very form to send messages to the others.
You want each receiver to have its own queue of messages. Each receiver will
create a CMeassageHandle r object and will register that object with the
form. When the form wants to send a message it goes thru all registered
receivers and call their Enqueue methods. But you don't want the form to be
blocked in waiting the reveivers to process the message that's why you want
to have a queue. The form just queue the message and continue. The message
will be process in some moment when it times come (up to the receiver).

Tell me if I got your idea right.

Do you need CMessageHandler to be a window in order to be able to implement
this enqueue-and-go feature or you have some other issues (like switchng
threads or so)?

Is the receiver-CMessageHandler relation one-to-one? In other words can more
then one receivers share the same CMessageHandler object (receiveMessage
delegate to be actualy a chain of delegates).

It is possible that you don't need CMessageHandler to be window at all.

IMHO your code of creating CMessageHandler window is not correct.
--
B\rgds
100
"James" <ja************ *****@yahoo.com .au> wrote in message
news:e6******** *************** ***@posting.goo gle.com...
I actually changed the code a bit. What happens is once it reaches
this.CreateHand le(cp); statement it jumps to WndProc() without
executing the PostMessage((In tPtr)this.Handl e,1234,(IntPtr) 2,(IntPtr)3)
statement. What am I doing wrong.

Thanks
using System.Collecti ons;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

public class CMessageHandler : System.Windows. Forms.NativeWin dow
{

[DllImport("user 32.dll", CharSet=CharSet .Auto, SetLastError=tr ue)]
public static extern bool PostMessage(
IntPtr hWnd,
[MarshalAs(Unman agedType.U4)] int Msg,
IntPtr wParam,
IntPtr lParam);

private Queue queue;
private CMessageDelegat e receiveMessage;

public CMessageHandler (CMsgDelegate receiveMessage)
{
queue = new Queue();
this.receiveMes sage = receiveMessage;
}

public void Enqueue(CMessag e message)
{
queue.Enqueue(m essage);

CreateParams cp = new CreateParams();

cp.Parent = (IntPtr)this.Ha ndle;

this.CreateHand le(cp);

PostMessage((In tPtr)this.Handl e,1234,(IntPtr) 2,(IntPtr)3);

}

private CMessage GetMessage()
{
return (CMessage)queue .Dequeue();
}

protected override void WndProc(ref Message msg)
{
if (msg.Msg == 1234)
{
this.receiveMes sage(GetMessage ());
}
}
}

Nov 15 '05 #6
I think you have got the basic idea from my "not quite fully explained
messages", for that thank you!

I logic is a bit different. I will explain,

I have a form, and that form needs to process incommimg
messages(forget about sending messages for now). For that I have
created a message handler which I have shown you before. The reason
for creating a seperate message handler is, to not interrupt the forms
activities. The messages would be put in a que and we need to get them
from the que. If we were not in a form we simple could have read the
que in a class running on a separate thread such as,

while(running)
{
try
{
getmessage((CMe ssage)Mailbox.D equeue());
}
catch(Exception e)
{
}

here the que will block the thread, but since we are not doing
anything it is ok.

But my case since I am working with a form I cannot block the
Application.Run
since then then my form would be idle.

So when a message arrives I call the messagehanlers enque method, and
the message will be put in the que, then I will post a windows message
in the handler, this message will be received by the WndProc method,
in WndProc method I call a Deque method to get the received message
which will be returned to the forms message handling function through
a function delegate which was set up at the startup.

Thats why I need to set the MessageHanler to derive from nativeWindow
and thats where I am having problems with getting the parent.Handle

Thanks

I hope this made sense.
Nov 15 '05 #7
James,

Do you need that NativeWindow at all.
why you don't implement all queue logic in your form
for example

class MyForm:Form
{
void Engue(CMessage msg)
{
//Puts message in queue queue. Notify the form for new message in
the queue
//PostMessage to this.Handle or call this.BeginInvok e, which will
have similar effect
//However if you use BeginInvoke the message processing method will
be the next think run in the form when it finishes it current work. If you
use PostMessage notification will be qeued in the windows message queue
along with the other messages
//Both techniques won't block the sender
}

CMessage GetMessage()
{
}

//In case you use PoseMessage insted of this.BeginInvok e
protected override WndProc(....)
{
//Call procedure to process the message
switch(m.Msg)
{
case SPECIALMSG:
//call for processing the next enqueued message
return;
}
}
}

Tell me what you think about this
--
B\rgds
100

"James" <ja************ *****@yahoo.com .au> wrote in message
news:e6******** *************** ***@posting.goo gle.com...
I think you have got the basic idea from my "not quite fully explained
messages", for that thank you!

I logic is a bit different. I will explain,

I have a form, and that form needs to process incommimg
messages(forget about sending messages for now). For that I have
created a message handler which I have shown you before. The reason
for creating a seperate message handler is, to not interrupt the forms
activities. The messages would be put in a que and we need to get them
from the que. If we were not in a form we simple could have read the
que in a class running on a separate thread such as,

while(running)
{
try
{
getmessage((CMe ssage)Mailbox.D equeue());
}
catch(Exception e)
{
}

here the que will block the thread, but since we are not doing
anything it is ok.

But my case since I am working with a form I cannot block the
Application.Run
since then then my form would be idle.

So when a message arrives I call the messagehanlers enque method, and
the message will be put in the que, then I will post a windows message
in the handler, this message will be received by the WndProc method,
in WndProc method I call a Deque method to get the received message
which will be returned to the forms message handling function through
a function delegate which was set up at the startup.

Thats why I need to set the MessageHanler to derive from nativeWindow
and thats where I am having problems with getting the parent.Handle

Thanks

I hope this made sense.

Nov 15 '05 #8
Thank you for your reply. The problem I have is if I have the WndProc
method in the same form class would be that if I post a message it
will hold up my form. Thats what made me to have a different class to
have WndProc. I actually coded the way you mentioned to show that it
actually holds up the form, If you know any solution let me know.
Thanks for the help.

using System;
using System.Threadin g;
using System.Collecti ons;
using System.Windows. Forms;
using System.Runtime. InteropServices ;

public class form2 : System.Windows. Forms.Form
{
[DllImport("user 32.dll", CharSet=CharSet .Auto,SetLastEr ror=true)]
public static extern bool PostMessage(Sys tem.Windows.For ms.Message
Msg);

private Queue queue;

public form2(CMessageD elegate PostMessagetoRo ot)
{
InitializeCompo nent();
queue = new Queue();
this.PostMessag etoRoot = PostMessagetoRo ot;
}

private void InitializeCompo nent()
{
........
}
private Common.CMsgDele gate PostMessagetoRo ot;
private System.Windows. Forms.Button button1;

public void Start()
{
Application.Run (this);

}

public void PostMessage(CMe ssage message)
{
Enqueue(message );
}

public void Enqueue(CMessag e message)
{
queue.Enqueue(m essage);
System.Windows. Forms.Message msg =
System.Windows. Forms.Message.C reate(this.Hand le,1234,(IntPtr )0,(IntPtr)0);
PostMessage(msg );
}

public void Shutdown()
{
this.Close();
}

private void button1_Click_1 (object sender, System.EventArg s e)
{
PostMessageToCo re("........... .");
}

protected override void WndProc(ref Message msg)
{

if (msg.Msg == 1234)
{

//for(int i=0;i<100000000 00000;i++)
// i++;

CMessage message = GetMessage();

Message Handlers can go here!!!!!!

base.WndProc(re f msg);

}
}
private CMessage GetMessage()
{
return (CMessage)queue .Dequeue();
}
}
Nov 15 '05 #9
Hi stoitcho,

Just one more thing I forgot to mention. Actually thats the main
reason. I have separated the form and the message receiver is to
separate the que from the form, so that it will not hold up the form
while its processing. And also message handling will go here as well.
Tell me what you think.

Regards

James
Nov 15 '05 #10

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

Similar topics

2
1862
by: Paul | last post by:
Hi all. Probably a quick one, I'm using PostMessage to send keys to an applicatoin but how do I convert the key string to a long values so that I can pass this to the wParam. The definition is below Private Declare Auto Function PostMessage Lib "user32" ( _ ByVal hWnd As IntPtr, _ ByVal Msg As Integer, _ ByVal wParam As Long, _
1
3313
by: Harry Whitehouse | last post by:
I have a form which, when launched, automatically begins a fairly intensive process. The problem is that the process is so intensive that the form doesn't have time to draw itself -- it only becomes visible at the end of the process. In C++ I used to solve this type of problem by sending a POSTMESSAGE during the dialog initialization process. The POSTMESSAGE would trigger a member which performed the intensive process. But the dialog...
12
7187
by: Wilfried Mestdagh | last post by:
Hi, Using P/Invoke I use PostMessage to send a message from one thread to another. I'm pretty sure this has already worked in dotnet. but I've upgraded version 2 few day ago. Now I have an illegal cross thread operation if posting a message. Is this a bug introduced in latest version of dotnet ? It is very legal to use postmessage because the message will execute in context of the thread the one was posted to. Same with...
6
4764
by: SM | last post by:
Hello group, i'm converting a VB6 application for .NET Framework. The application depends on a DLL written in standard C. The C DLL code creates an invisible window with CreateWindow() with the WndProc function located into the VB.NET code. Until the code was VB6/C code, they was OK. Now the VB WndProc receives the startup message correctly
2
2615
by: Lenster | last post by:
When using PostMessage to post myself a message, the msg and wparam parameters somehow get swapped over. They are in the correct order when calling PostMessage but by the time wndproc handles the message msg is now in the wparam position and msg is set to 0 (NULL message). Does anyone know why this behaviour is occurring ? I've included a simple test form below to illustrate this...
3
5083
by: Max M. Power | last post by:
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:
3
9460
by: knikkix | last post by:
Hi, I created a form in VB.Net with only one button. This is the code in button click event Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click AppGlobals.SetHandles(Me.Handle.ToInt64()) Call OpenModel() End Sub
10
2576
by: Sergei | last post by:
Can anyone explain why PostMessage to a Windows Form is considered unsafe and raises InvalidOperationException? I can get rid of that setting CheckForIllegalCrossThreadCalls to false and everything works just fine. Still I feel somewhat uncomfortable. Sergei
21
6875
by: one2001boy | last post by:
PostMessage() function returns ERROR_NOT_ENOUGH_QUOTA after running in a loop for 700 times, but the disk space and memory are still big enough. any suggestion to resolve this problem? thanks.
0
8352
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
8802
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
8579
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...
1
6158
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
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
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
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.