Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

// The class works fine.
// If correctely used, It write a string in the front window windows
#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){
      // CreateProcess() failed
      // Get the error from the system
      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);

      // Display the error
      string strError = (LPTSTR) lpMsgBuf;
      //TRACE(_T("::executeCommandLine() failed at CreateProcess()\nCommand=%s\nMessage=%s\n\n"), cmdLine, strError);

      // Free resources created by the system
      LocalFree(lpMsgBuf);

      // We failed.
      return FALSE;
   }
   else{
// Here is the moment I use the keyboard to write the login and password.
// It works fine, but it's a week solution, a need to send 2 different strings in two //dialogue boxes that opens one after another (once the ENTER key was pressed, in both //cases) when the process starts: one with the LOGIN and other with the PASSWORD
	   	keyboard keybd;
	   	Sleep(3000);
	   	keybd.text("LOGIN");
	   	keybd.press_enter(TRUE);
	   	Sleep(200);
	   	keybd.text("PASSWORD");
	   	keybd.press_enter(TRUE);

      // Successfully created the process.  Wait for it to finish.
      WaitForSingleObject( processInformation.hProcess, INFINITE );

      // Get the exit code.
      result = GetExitCodeProcess(processInformation.hProcess, &exitCode);

      // Close the handles.
      CloseHandle( processInformation.hProcess );
      CloseHandle( processInformation.hThread );

      if (!result){
         return FALSE;
      }

      // We succeeded.
      return TRUE;
   }
}

int main() {
	LPSTR command = LPSTR("command line text");
	DWORD exitCode;
// executeCommandLine two times.
// to verfy if the first waits for the second. It works fine.
	executeCommandLine(NULL, command, exitCode);
	executeCommandLine(NULL, command, exitCode);
	return 0;
}
Posted
Updated 5-Oct-17 19:21pm
v3

1 solution

You could try and send the text using SendMessage to the newly opened window.

1. Call CreateProcess[^] to start the new process.
2. Use EnumWindows[^] to enumerate all top level windows.
3. Use GetWindowThreadProcessId[^] to get the process id of the enumerated window.
4. Compare the process id with that in the PROCESS_INFORMATION structure to confirm the window identity.
5. Use SendMessage[^] with WM_SETTEXT and the text to send to send to the window handle in EnumWindowsProc[^].
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900