0

In our Program we have a Dialog from a separate dll open to display infomation. I need to close this dialog when our system timer causes the system to lock.

I send information to the dll by registering a system message in both my MainFrm and EditDisplayDll

SYSTEMLOCK = RegisterWindowMessage("SystemLock");

When I sent the message via

::PostMessage(GetActiveWindow()->m_hWnd,SYSTEMLOCK,0,0);

The message correctly sends to my EditDisplayDll and closes the dialog when the system locks; however, if I alt tab while waiting for the timeout and use another program(firefox, outlook, etc.) the message never correctly calls to the EditDisplayDll. The MainFrm and other windows inside of the MainFrm correctly lockout and hide themselves in either case.

I have tried also using HWND_BROADCAST with PostMessage and SendNotifyMessage. I have also tried to use FindWindow() and FindWindowEx() to specifically call the EditDisplayDll.

I cannot use something like GetDlgItem() because my MainFrm.cpp doesn't have access to this dll.

My decision to use GetActiveWindow() was because I believe it looks to windows specific to my program no matter what window I am in as seen in the imagery in Foreground Vs Active window

Finally, my question is, is there a way to call all Windows in my program no matter what program I am currently in or is there another way I could get access to the specific IDD of the EditDisplayDll in order to send the SYSTEMLOCK message to it?

Community
  • 1
  • 1
Jeff S
  • 250
  • 6
  • 13
  • Whats the problem you face to find the window handle using FindWindow() ? – hypheni Apr 29 '15 at 21:16
  • The build compiles when I try to use `FindWindow()` but it never correctly finds the EditDisplayDll so the message never sends. – Jeff S Apr 29 '15 at 21:23
  • How are you calling FindWindow ? – hypheni Apr 29 '15 at 21:29
  • One way I tried was `CWnd *cWndED = FindWindow(_T("EditDisplay"),_T("EditDisplay")); HWND hwnd = (HWND)cWndED; ::PostMessage(hwnd,SYSTEMLOCK,0,0);' – Jeff S Apr 29 '15 at 21:38

2 Answers2

2

CWnd *cWndED = FindWindow(_T("EditDisplay"),_T("EditDisplay")); HWND hwnd = (HWND)cWndED;

You should use win32 API ::FindWindow with proper class, window name. And do not cast CWnd pointer to HWND. Your code should look like:

HWND hWnd = ::FindWindow(_T("ProperClass"), _T("ProperNmae"));
if (hWnd != NULL)
{
  ::PostMessage(hWnd, YOUR_MESSAGE, ....);
}

I will suggest you to find your Dll window class and name using Spy++ and then try to find that using above method. Remember it's always better to use native API for this kind of tasks.

hypheni
  • 756
  • 3
  • 17
  • 37
  • I'll look in to Spy++ and see if I can implement the changes you have suggested when I return to work tomorrow. – Jeff S Apr 29 '15 at 23:11
1

FindWindow is a good solution if you know both, name of the window and the element.
If you want to get the HWND of your window - no element inside the window -, you can pass as first parameter NULL.

::FindWindow(NULL, _T("WindowName"));

Back to your code: If you are lucky your PostMessage does nothing, otherwise the active window may catch your message. Who knows how/if it is handled in the active window ? Use the PostMessage if you have a valid IsWindow(HWND) from FindWindow or FindWindowEx.
In case you want a CWnd from your HWND take a look at this. (The call may be slow)

HWND hWnd = ::FindWindow(_T("ClassName"), _T("WindowName"));
if (hWnd && IsWindow(hWnd))
{
    ::PostMessage(hWnd, MESSAGE_TO_BE_SEND, lParam_or_Flags);
}
Blacktempel
  • 3,935
  • 3
  • 29
  • 53
  • I don't know why you are suggesting OP to use nulltptr as first parameter. It should be NULL in windows terminology to find a window with only title name. And moreover in microsoft enviroment its always preferrable to use NULL as a notation. – hypheni Apr 30 '15 at 08:15