I want to close all the running instances of Notepad through my application.
I got the window handle using
FindWindow() API.
I got the handle of Window.
CloseWindow() API
is minimizing the notepad but I want to close all the instances of Notepad.
How can I achieve this?
By using FindWindow() you will get HWND
Use that HWND to get pid i.e process id.
HWND hWnd; // using findwindow you will get hWnd
DWORD pid;
TCHAR tcInput [MAX_PATH];
CString strName;
GetWindowThreadProcessId(hWnd,&pid);
//::GetWindowText(hWnd,tcInput,MAX_PATH);
//strName = (CString)tcInput;
//if(strName.MakeLower().Find( _T("untitle"))!=-1) //you can check windows title here
KillProcess(pid); // kill the process
Related
I have the process name and the handle (HWND) of its window. I want now to get the relative icon (if available). Searching through MSDN, I found ExtractIcon() to get the handle to the icon from a given exe name, and GetIconInfo() to get "information" of the icon from the HICON. I don't know if it's the right way to do this, and how to retrieve correct information to show (in a second moment) the icon without the handle to the icon.I have to send this information to another process (through socket) that has to show the icon.In the ICONINFO structure there are HBITMAP fields that contains the bitmap (black&white and with colour). Is it useful?
you can use the API GetClassLong to retrieve the icon associated with your program then use SendMessage API passing the hwnd of of the window you want to change it's icon.
in this Example I extracted the icon from my application then set it to Calculator. my windows calculator is open before sending to it the icon:
case WM_LBUTTONDOWN: // just for explanation so left clicking in your client area and see the result
{
HICON icon = (HICON)GetClassLong(hWnd, GCL_HICON);
HWND hCons = FindWindow(NULL, "Calculator"); // I already opened windows calculator. you can use any other window but be sure to get its valid Handle
if(!hCons)
MessageBox(0, "\"Calculator\" windows is not found!", 0, MB_OK|MB_ICONHAND);
SendMessage(hCons, WM_SETICON, (WPARAM)ICON_BIG, (LPARAM)icon); // setting the icon
}
break;
Is there a way to programmatically hide an application on windows? I want to achieve the same thing as the windows+D shortcut, but for a single application. I want to do this from within that application (application consists of several windows, one of those can't be moved, resized, closed or minimized by the user). Application is written in c++ and uses Qt for the UI.
to do so it's so easy:
1- retrieve the handle to that window:
HWND hChild = GetDlgItem(hWnd, ID_MYCHILD);
2- send to it SW_SHOW either using ShowWindow or via SendMessage:
ShowWindow(hChild, SW_HIDE); // hide
ShowWindow(hChild, SW_SHOW); // show
SendMessage(hChild, SW_HIDE, 0, 0); // hide
SendMessage(hChild, SW_SHOW, 0, 0); // show
if the window doesn't belong to your application then:
1 - retrieve the main window with:
HWND hWnd = GetForegroundWindow(void);
2- use the above to hide/show it
ShowWindow(HwndWindow, SW_MINIMIZE);
Here's the MSDN ShowWindow documentation.
In addition you may find EnumChildWindows useful for finding all these windows if their handles aren't readily available to you.
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?
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.
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);
}
I am relatively new to C++ programming and currently using Visual Studio 2012 API. My OS is Windows 7 64-bit. I would like to write a program in C++ which displays a message box or window with a simple shutdown message e.g. 'shutting down.....' or something similar when I close a specific application. This window I am hoping will appear for the duration of the app exit time and then close.
Is it possible to create a handle which will retrieve the exit time for a running application when it is abruptly closed? And if so, how could I use this exit time in a statement which will display the message box?
I would appreciate constructive criticism as I am new to this language. Thank you sincerely for any advice you impart. If requested, I will display all source code.
I don't think this is the right way to go about this but anyway, below is a snippet of the code which I have been toying with, as part of a greater VS Win32 application project:
LPTSTR lpchText(_T("Shutting down...."));
LPFILETIME lpExitTime = 0; //Initialise
TCHAR Buffer[_MAX_PATH];
DWORD size = _MAX_PATH;
LPCTSTR lpStr(_T("C:\Program Files\Common Files\ExampleApp.exe")); // Path to executable app.
AssocQueryString( ASSOCF_OPEN_BYEXENAME,
ASSOCSTR_EXECUTABLE,
lpStr,
NULL,
Buffer,
&size
);
GetProcessTimes(
AssocQueryString,
NULL,
lpExitTime,
NULL,
NULL
);
while(lpExitTime){
MessageBoxEx(
_In_opt_ hWnd,
_In_opt_ lpchText,
_In_opt_ lpCaption,
_In_opt_ MB_ICONEXCLAMATION
,0
);
};
return TRUE;
enter code here
I guess shutting down the application is a lengthy operation othervise a normal application will be closed so fast that the user will have no chance to actual see the window.
But of course it possible, in meta code this is the steps.
Attach to the running process. You can enumrate the running process from the name of the executable to find the correct process.
Once you have the handle to the process you can attach to the process message loop.
Listen for the WM_QUIT (assuming it is a Windows applictaion) message and then display the window.
Wait for the process handle using MsgWaitForMultipleObject, the function will signal when the process terminates.
Attaching to other processes has some security issues so it might not work for a regular user.
Other options to explore is to handle it in a power Shell script or make a small launcher application that in turn starts the actual application.
Is it an option to modify the application itself to have this functionality?
I'm opening a process in C++ like so
FILE* pipe = _popen(ss.str().c_str(), "r");
This create a cmd window which goes to the forefront of the desktop. I want to prevent the cmd window from opening, and if I can't, a way to keep it minimized.
Tried solutions:
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
But that only works if you are calling that function in the process that holds the cmd window.
Instead of using _popen, use CreateProcess and the CREATE_NO_WINDOW flag.