Created
January 26, 2021 16:28
-
-
Save emcodem/a1d6106ef2a11accd9860797fe28662a to your computer and use it in GitHub Desktop.
Send Ctrl+C to PID in Windows
This file contains hidden or 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 <stdio.h> | |
//sends ctrl+c to pid | |
//compile with gcc, no options | |
//first and only argument: int PID | |
int main(int argc, char** argv){ | |
DWORD pid = atoi(argv[1]); | |
printf ("Argument PID: %d\n",pid); | |
if (!FreeConsole()) | |
{ | |
printf("Could not FreeConsole\n"); | |
exit(1); | |
} | |
if (!AttachConsole(pid)){ | |
printf("Error: could not attach console to specified PID\n"); | |
exit(2); | |
} | |
//Console is attached, do not print to STDOUT anymore, it would go to the connected Console | |
if( ! GenerateConsoleCtrlEvent( CTRL_C_EVENT, 0 ) ) | |
{ | |
//could not GenerateConsoleCtrlEvent | |
exit(3); | |
}else{ | |
//all good, exit | |
exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment