Skip to content

Instantly share code, notes, and snippets.

@dhtml
Forked from sergnechaev/close_window_by_title.cpp
Created March 18, 2023 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhtml/12ce709202bdd8d93a6eb54eecb608e6 to your computer and use it in GitHub Desktop.
Save dhtml/12ce709202bdd8d93a6eb54eecb608e6 to your computer and use it in GitHub Desktop.
C++ WinAPI, close window by title
#include <windows.h>
#include <iostream>
int main(int argc, char* argv[]) {
if (argc <= 1) {
std::cerr << "ERROR: Please, specify the window title to close" << std::endl;
return EXIT_FAILURE;
}
auto hwnd = FindWindow(nullptr, argv[1]);
if (hwnd != nullptr) {
std::cout << "Closing \"" << argv[1] << "\"" << std::endl;
PostMessage(hwnd, WM_CLOSE, 0, 0);
} else {
std::cerr << "ERROR: Can't find the window to close" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment