Taking control of main thread in windows c ++ application

I am writing code that I like can work with any window, like a window created via the windows API, MFC, wxWidgets, etc.

The problem is that for some things, I need to use the same thread that created the window, which in many cases just sits in a message loop.

My first thought was to send a callback message to the window, which will then call a function in my code when it receives a message using one of the parameters and a function pointer of some roles. However, there doesn't seem to be a standard Windows message for this, and I can't create my own message since I don't control the Windows code, so I can't add the code I need to the message handler to implement the callback ...

Is there any other way to get the thread that created the input box for my function?

EDIT: John Z clarified that I connected windows messages. If I do this, is there a way to get the "IDs" for the custom posts without risking bumping into any custom posts that are already in the window?

For example, I could do

WM_CALLBACK = WM_APP+1

      

But if the window I am hooking has already done something with WM_APP + 1, I will have problems.

EDIT2: Just found RegisterWindowMessage :)

+1


source to share


2 answers


If you are in the same process as a window, you can wire its messages by subclassing it. Check http://msdn.microsoft.com/en-us/library/ms633570(VS.85).aspx

The key API is SetWindowLong.



// Subclass the edit control. 
wpOrigEditProc = (WNDPROC) SetWindowLong(hwndEdit, GWL_WNDPROC, (LONG)EditSubclassProc); 

// Remove the subclass from the edit control. 
SetWindowLong(hwndEdit, GWL_WNDPROC, (LONG)wpOrigEditProc); 

      

+2


source


As an alternative to subclassing, you can use SetTimer to call a function on the window thread.



VOID CALLBACK Function(      
HWND hwnd,
UINT uMsg,
UINT_PTR idEvent,
DWORD dwTime
)
{
  // stuff
}

SetTimer(hWnd, event, 0, Function);

      

0


source







All Articles