Created
October 11, 2016 06:01
-
-
Save sergnechaev/c8b3d511406abd65f0132a5f86bcbf11 to your computer and use it in GitHub Desktop.
C++ WinAPI, close window by title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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
It was very useful for me. Thanks