如何用VC从控制台读取字符串?

时间:2013-02-12 05:27:10

标签: c visual-c++ command-line mfc

我想从命令行读取字符串。我打开一些在命令行中运行的程序,我必须等待该程序进程。所以,我不知道什么时候进程成功。如果无法从命令中读取字符串,我就无法输入next命令。这是我在命令中运行程序unhidden.bat的代码,并为进程发送Y.

DWORD WINAPI unhidden(LPVOID lp)
{
   //ShellExecute(NULL, L"open", L"C:/unhidden.exe", NULL, NULL, SW_SHOWNORMAL);
    system("C:/unhidden.bat");
   return 0;
}

int run_unhidden(){
    AllocConsole() ;
    AttachConsole( GetCurrentProcessId() ) ;
    freopen( "CON", "w", stdout ) ;

    HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD dwTmp;

    INPUT_RECORD ir[5];

    CreateThread(NULL, 0, unhidden,(LPDWORD)0, 0, &dwTmp);

    Sleep( 1000 );

    ir[0].EventType = KEY_EVENT;
    ir[0].Event.KeyEvent.bKeyDown = TRUE;
    ir[0].Event.KeyEvent.dwControlKeyState = 0;
    ir[0].Event.KeyEvent.uChar.UnicodeChar = 'y';
    ir[0].Event.KeyEvent.wRepeatCount = 1;
    ir[0].Event.KeyEvent.wVirtualKeyCode = 'Y';
    ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('Y', MAPVK_VK_TO_VSC);

    ir[1].EventType = KEY_EVENT;
    ir[1].Event.KeyEvent.bKeyDown = TRUE;
    ir[1].Event.KeyEvent.dwControlKeyState = 0;
    ir[1].Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
    ir[1].Event.KeyEvent.wRepeatCount = 1;
    ir[1].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
    ir[1].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);

    ir[2].EventType = KEY_EVENT;
    ir[2].Event.KeyEvent.bKeyDown = FALSE;
    ir[2].Event.KeyEvent.dwControlKeyState = 0;
    ir[2].Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
    ir[2].Event.KeyEvent.wRepeatCount = 1;
    ir[2].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
    ir[2].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);



    dwTmp = 0;
    WriteConsoleInput(hConIn, ir, 2, &dwTmp);

wprintf(GetCommandLine());

    Sleep( 1000 );



    ir[0].EventType = KEY_EVENT;
    ir[0].Event.KeyEvent.bKeyDown = TRUE;
    ir[0].Event.KeyEvent.dwControlKeyState = 0;
    ir[0].Event.KeyEvent.uChar.UnicodeChar = 'y';
    ir[0].Event.KeyEvent.wRepeatCount = 1;
    ir[0].Event.KeyEvent.wVirtualKeyCode = 'Y';
    ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('Y', MAPVK_VK_TO_VSC);

    ir[1].EventType = KEY_EVENT;
    ir[1].Event.KeyEvent.bKeyDown = TRUE;
    ir[1].Event.KeyEvent.dwControlKeyState = 0;
    ir[1].Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
    ir[1].Event.KeyEvent.wRepeatCount = 1;
    ir[1].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
    ir[1].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);


    ir[2].EventType = KEY_EVENT;
    ir[2].Event.KeyEvent.bKeyDown = FALSE;
    ir[2].Event.KeyEvent.dwControlKeyState = 0;
    ir[2].Event.KeyEvent.uChar.UnicodeChar = VK_RETURN;
    ir[2].Event.KeyEvent.wRepeatCount = 1;
    ir[2].Event.KeyEvent.wVirtualKeyCode = VK_RETURN;
    ir[2].Event.KeyEvent.wVirtualScanCode = MapVirtualKey(VK_RETURN, MAPVK_VK_TO_VSC);

     dwTmp = 0;
    WriteConsoleInput(hConIn, ir, 3, &dwTmp);
    wprintf(GetCommandLine());
     Sleep( 1000 );

它在控制台中显示如下

Do you want to process now (Y/N)?y "C:\Users\PKRU\documents\visual studio 2010\Projects\Virus Scan\Debug\Virus Scan .exe" You chose: y 
Please wait . . . 
"C:\Users\PKRU\documents\visual studio 2010\Projects\Virus Scan\Debug\Virus Scan .exe" Could Not Find N:\*.lnk 
N:\autorun.ini\*, Are you sure (Y/N)?

如何阅读

  

N:\ autorun.ini *,你确定(是/否)?

使用MFC将下一个密钥发送到控制台。如果我不知道它何时显示此行,那么我就不能发送Y或N来编程

0 个答案:

没有答案