Skip to content

Instantly share code, notes, and snippets.

@emcodem
Created January 26, 2021 16:28
Show Gist options
  • Save emcodem/a1d6106ef2a11accd9860797fe28662a to your computer and use it in GitHub Desktop.
Save emcodem/a1d6106ef2a11accd9860797fe28662a to your computer and use it in GitHub Desktop.
Send Ctrl+C to PID in Windows
#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