The goal is to programatically hit Enter to some cmd.exe (running in background, e.g. owned by SYSTEM user, started from a windows service) which would visually result in a new, empty promt line like "C:\dev\sendenter>".
All i found on the net concentrates on receiving the keys where you have the option to check if ascii code 10 or 13 was sent. But when sending those to a running cmd.exe, all that will happen is that you get a newline but no new command prompt. Even using VK_RETURN which should be an alias for Carriage Return and Line Feed does not end up in a new prompt.
I believe the problem could be that WM_KEYUP or such must follow the ASCII key 10 or 13 but i was not able to find out how to send this using below minimal example.
#include <Windows.h>
#include <stdio.h>
//argument: int PID of process
//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);
}
char s[] = {VK_RETURN};
HANDLE stdoutt = GetStdHandle(STD_OUTPUT_HANDLE);
unsigned long cChars;
WriteConsole(stdoutt, s, lstrlen(s), &cChars, NULL);
return 0;
}