如何将字符串发送到程序

如何将字符串发送到程序

本文介绍了如何将字符串发送到程序,打开框等待字符串响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows程序,等待登录和密码继续运行。我需要自动传递这些信息,然后完成它的工作并启动另一个。



我使用createProcess来打开这个程序,并等待响应。



我使用keybd_event之后,写了凭证,一切正常。

但我不满意,因为这是一周的解决方案,一旦有人在程序运行时打开一个窗口,keybd_event将在打开的窗口上写,而不是那个想要的。



那么如何发送一个字符串信息到程序,并让它理解这个字符串是我不知道的问题。任何线索和提示?



PS :在下面的代码中我没有把键盘类,因为它工作正常。

我执行两次,在main中,函数

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

来测试是否真的等待另一个完成其工作开始。



非常感谢您的时间。

APS。



我尝试了什么:



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;
}

推荐答案


这篇关于如何将字符串发送到程序,打开框等待字符串响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:44