Skip to content

Instantly share code, notes, and snippets.

@sergnechaev
Created October 11, 2016 06:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sergnechaev/c8b3d511406abd65f0132a5f86bcbf11 to your computer and use it in GitHub Desktop.
Save sergnechaev/c8b3d511406abd65f0132a5f86bcbf11 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;
}
@choi303
Copy link

choi303 commented Dec 12, 2020

It was very useful for me. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment