Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting "Intptr" to a "HWND" handle 1

Status
Not open for further replies.

Nidal

Programmer
Aug 30, 2004
4
0
0
US
Hello.
I build an application using Windows Forms.
I spawn a thread that starts a Window Form. I am in a point where I need to send messages to these Windows. When I try to get a handle to the Form's Window (Form->Handle) I get a value of type "Intptr". On the other hand, the methods for sending (posting) messages Windows expect a window handle (HWND). All attempts to cast Intptr to CWnd's HWND did not work.
I have already set up the WndProc() function for each form's Window I popup. I just could not figure out how to send messages to these Windows.Does anyone know how to send messages to these kind of Windows.

I am not using MFC CWnd windows. I build System::Windows::Forms classes.

Thanks.

- Nidal
 
Try using PostThreadMessage. You will need the threadid.
 
Thank you xwb for your suggestion.

Your suggestion, I guess, boils down to using LocalDataStoreSlots (I assume that LocalDataStore is the same as "Thread Message Queue"). I create a named LocalDataStoreSlot for each thread and communicate with each thread by writing messages to these LocalDataStoreSlot. The thread needs to build a mechanism for repeatedly reading data from its DataSlot.

Trouble with this method is that I am using a thread to start a Form. Once a thread starts a Form, there is no way for me have that same thread do anything else, like checking on its dataSlot for messages.

What I was hoping to do is Send a message to the Window of the Form that the thread started and have the Window handle these messages using its Build-In mechanism, i.e., using the WndProc() function. But my problem is that I can not get to that window's handle.

Thank you very much.

- Nidal

 
Use GetCurrentThreadId() to get the thread id when the form starts up and send it to the window that has to send the message.

Actually, check that your threadid isn't the same on both threads. If it is, your 'forms' system isn't starting up a new thread but using some other mechanism.
 
Thanks xwb.

I checked on the threadIDs and found that the thread that starts a form is different from the main thread.

Again, even if I pass the threadID of the new thread back to the main thread to save it in a list somewhere, if I pass a message to that thread, that thread will not be able to pick his message because I am not able to add the capability of threading messages to that thread. the reason is that once a thread starts a Form, it can not do anything else. Any code you put after the call that starts the form will not be reached.

start_thread_to_start_form()
{
start new thread ...
Application::Run(newForm);
// any code you add here will be reached untill after the form is closed.
return threadID
}

main_thread()
{
wait for requests to start new form from an external entity;
Do for ever()
{
Got a request()....
Yes
{
do we have a Form already popped up for that entity?
Yes
{
I need to be able to communicate with the window/thread of that (supposedly already popped up) form
}

NO
{
start_thread_to_start_form()
}
}
}
}
 
>All attempts to cast Intptr to CWnd's HWND did not work.

But IntPtr has a ToInt32 member you could call.


/Per
[sub]
"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."[/sub]
 
Thank you xwb and PerFnurt for you help. I am able to cast From Intrptr to HWND and communicate to the Window of the Form.

Regards,

- Nidal
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top