I have a windows program that awaits for a login and password to keep running. I need to pass this information automatically, to it finish its job and start another.
I use createProcess to open this program, and waits for the response.
Right after I use keybd_event, to write the credential, and everything works fine.
But I'm not stisfyed, because this is a week solution, once someone opens a window while the programa is running the keybd_event will write on the opened window, and not in that one a wish to.
So how to send a string information to a program, and make it understand this string is the question that I wont to know. Any clues, and tips?
PS: In the code below I didn't put the keyboard class, because it is working fine.
I execute two times, in main, the funcition
executeCommandLine
to test if really one waits another finish its job to start.
Thanks very much for your time.
APS.
What I have tried:
#include "keyboard.h"
bool executeCommandLine(LPCSTR path, LPSTR cmdLine, DWORD &exitCode){
PROCESS_INFORMATION processInformation = {0};
STARTUPINFO startupInfo = {0};
startupInfo.cb = sizeof(startupInfo);
BOOL result = CreateProcess(NULL, cmdLine,
NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW,
NULL, NULL, &startupInfo, &processInformation);
if (!result){
LPVOID lpMsgBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpMsgBuf, 0, NULL);
string strError = (LPTSTR) lpMsgBuf;
LocalFree(lpMsgBuf);
return FALSE;
}
else{
keyboard keybd;
Sleep(3000);
keybd.text("LOGIN");
keybd.press_enter(TRUE);
Sleep(200);
keybd.text("PASSWORD");
keybd.press_enter(TRUE);
WaitForSingleObject( processInformation.hProcess, INFINITE );
result = GetExitCodeProcess(processInformation.hProcess, &exitCode);
CloseHandle( processInformation.hProcess );
CloseHandle( processInformation.hThread );
if (!result){
return FALSE;
}
return TRUE;
}
}
int main() {
LPSTR command = LPSTR("command line text");
DWORD exitCode;
executeCommandLine(NULL, command, exitCode);
executeCommandLine(NULL, command, exitCode);
return 0;
}