2012-07-19 34 views
10

Sto avendo un problema serio qui. Ho bisogno di eseguire una riga di comando CMD tramite C++ senza la visualizzazione della finestra della console. Pertanto non posso usare system(cmd), poiché verrà visualizzata la finestra.C++ Esecuzione comandi CMD

Ho provato winExec(cmd, SW_HIDE), ma questo non funziona neanche. CreateProcess è un altro che ho provato. Tuttavia, questo è per l'esecuzione di programmi o file batch.

ho finito per cercare ShellExecute:

ShellExecute(NULL, "open", 
    "cmd.exe", 
    "ipconfig > myfile.txt", 
    "c:\projects\b", 
    SW_SHOWNORMAL 
); 

chiunque può vedere qualcosa di sbagliato con il codice di cui sopra? Ho usato SW_SHOWNORMAL fino a quando so che questo funziona.

Ho davvero bisogno di aiuto con questo. Nulla è venuto alla luce, e ci sto provando da un po '. Qualsiasi consiglio che chiunque potrebbe dare sarebbe bello :)

+0

hai controllato il codice di ritorno? – Collin

+1

So che hai avuto una risposta, ma normalmente è una buona idea dire come non funziona. – Deanna

+0

Perché non chiamare le funzioni WMI_ e scrivere i risultati su file. Nessuna finestra e solo i dati di cui hai bisogno. –

risposta

6

Reindirizzare l'uscita per il tubo stesso è una soluzione più ordinato perché evita la creazione del file di output, ma questo funziona bene:

ShellExecute(0, "open", "cmd.exe", "/C ipconfig > out.txt", 0, SW_HIDE); 

non si vede la finestra cmd e l'output viene reindirizzato come previsto .

Probabilmente il tuo codice non funziona (a parte la cosa /C) perché si specifica il percorso come "c:\projects\b" anziché "c:\\projects\\b".

4

si dovrebbe usare CreateProcess sul cmd.exe con il parametro /C al tunnel il comando ipconfig. > Non funziona di per sé sulla riga di comando. Devi redirect programmatically the stdout.

+0

Se è una riga di comando 'cmd/c', ovviamente' '' il reindirizzamento funzionerà. – eryksun

3

Ecco la mia implementazione di una funzione DosExec che consente di eseguire (silenziosamente) qualsiasi comando DOS e recuperare l'output generato come stringa unicode.

// Convert an OEM string (8-bit) to a UTF-16 string (16-bit) 
#define OEMtoUNICODE(str) CHARtoWCHAR(str, CP_OEMCP) 

/* Convert a single/multi-byte string to a UTF-16 string (16-bit). 
We take advantage of the MultiByteToWideChar function that allows to specify the charset of the input string. 
*/ 
LPWSTR CHARtoWCHAR(LPSTR str, UINT codePage) { 
    size_t len = strlen(str) + 1; 
    int size_needed = MultiByteToWideChar(codePage, 0, str, len, NULL, 0); 
    LPWSTR wstr = (LPWSTR) LocalAlloc(LPTR, sizeof(WCHAR) * size_needed); 
    MultiByteToWideChar(codePage, 0, str, len, wstr, size_needed); 
    return wstr; 
} 

/* Execute a DOS command. 

If the function succeeds, the return value is a non-NULL pointer to the output of the invoked command. 
Command will produce a 8-bit characters stream using OEM code-page. 

As charset depends on OS config (ex: CP437 [OEM-US/latin-US], CP850 [OEM 850/latin-1]), 
before being returned, output is converted to a wide-char string with function OEMtoUNICODE. 

Resulting buffer is allocated with LocalAlloc. 
It is the caller's responsibility to free the memory used by the argument list when it is no longer needed. 
To free the memory, use a single call to LocalFree function. 
*/ 
LPWSTR DosExec(LPWSTR command){ 
    // Allocate 1Mo to store the output (final buffer will be sized to actual output) 
    // If output exceeds that size, it will be truncated 
    const SIZE_T RESULT_SIZE = sizeof(char)*1024*1024; 
    char* output = (char*) LocalAlloc(LPTR, RESULT_SIZE); 

    HANDLE readPipe, writePipe; 
    SECURITY_ATTRIBUTES security; 
    STARTUPINFOA  start; 
    PROCESS_INFORMATION processInfo; 

    security.nLength = sizeof(SECURITY_ATTRIBUTES); 
    security.bInheritHandle = true; 
    security.lpSecurityDescriptor = NULL; 

    if (CreatePipe(
        &readPipe, // address of variable for read handle 
        &writePipe, // address of variable for write handle 
        &security, // pointer to security attributes 
        0   // number of bytes reserved for pipe 
        )){ 


     GetStartupInfoA(&start); 
     start.hStdOutput = writePipe; 
     start.hStdError = writePipe; 
     start.hStdInput = readPipe; 
     start.dwFlags  = STARTF_USESTDHANDLES + STARTF_USESHOWWINDOW; 
     start.wShowWindow = SW_HIDE; 

// We have to start the DOS app the same way cmd.exe does (using the current Win32 ANSI code-page). 
// So, we use the "ANSI" version of createProcess, to be able to pass a LPSTR (single/multi-byte character string) 
// instead of a LPWSTR (wide-character string) and we use the UNICODEtoANSI function to convert the given command 
     if (CreateProcessA(NULL,     // pointer to name of executable module 
          UNICODEtoANSI(command), // pointer to command line string 
          &security,    // pointer to process security attributes 
          &security,    // pointer to thread security attributes 
          TRUE,     // handle inheritance flag 
          NORMAL_PRIORITY_CLASS, // creation flags 
          NULL,     // pointer to new environment block 
          NULL,     // pointer to current directory name 
          &start,     // pointer to STARTUPINFO 
          &processInfo    // pointer to PROCESS_INFORMATION 
         )){ 

      // wait for the child process to start 
      for(UINT state = WAIT_TIMEOUT; state == WAIT_TIMEOUT; state = WaitForSingleObject(processInfo.hProcess, 100)); 

      DWORD bytesRead = 0, count = 0; 
      const int BUFF_SIZE = 1024; 
      char* buffer = (char*) malloc(sizeof(char)*BUFF_SIZE+1); 
      strcpy(output, ""); 
      do {     
       DWORD dwAvail = 0; 
       if (!PeekNamedPipe(readPipe, NULL, 0, NULL, &dwAvail, NULL)) { 
        // error, the child process might have ended 
        break; 
       } 
       if (!dwAvail) { 
        // no data available in the pipe 
        break; 
       } 
       ReadFile(readPipe, buffer, BUFF_SIZE, &bytesRead, NULL); 
       buffer[bytesRead] = '\0'; 
       if((count+bytesRead) > RESULT_SIZE) break; 
       strcat(output, buffer); 
       count += bytesRead; 
      } while (bytesRead >= BUFF_SIZE); 
      free(buffer); 
     } 

    } 

    CloseHandle(processInfo.hThread); 
    CloseHandle(processInfo.hProcess); 
    CloseHandle(writePipe); 
    CloseHandle(readPipe); 

    // convert result buffer to a wide-character string 
    LPWSTR result = OEMtoUNICODE(output); 
    LocalFree(output); 
    return result; 
} 
+0

Grazie per il codice di esempio! –

0

Ho un programma simile [Windows7 e 10 testate] su github

https://github.com/vlsireddy/remwin/tree/master/remwin

Questo è il programma che

  1. ascolti su "Connessione alla rete locale" server denominato interfaccia windows per la porta UDP (5555) e riceve il pacchetto udp.
  2. il contenuto del pacchetto udp ricevuto viene eseguito su cmd.exe [per favore non cmd.exe NON viene chiuso dopo l'esecuzione del comando e la stringa di output [l'output del comando eseguito] è fedback al programma client sulla stessa porta udp].
  3. In altre parole, comando ricevuto nel pacchetto UDP -> analizzato pacchetto UDP -> eseguito su cmd.exe -> output inviato indietro sulla stessa porta al programma client

Questo non mostra "finestra della console" Non c'è bisogno di qualcuno per eseguire manualmente il comando cmd.exe remwin.exe può essere in esecuzione in background e la sua un programma server sottile

+0

Ciao, sembra troppo di polizia, ho messo un link testato valido e risponde alla domanda specifica per risposta specifica con codice testato, non capisco la tua raccomandazione di revisione cancellazione. Si esegue il codice [MSVC], si controlla l'output, si verifica e se non si adatta, eliminarlo. – particlereddy

+0

Siamo spiacenti, ho eliminato il mio commento. Ero troppo veloce sul grilletto ... – Jolta

+0

Leggendo la tua risposta, mi sembra che tu abbia risolto il problema. Tuttavia, Stack Overflow scoraggia le risposte che forniscono soluzioni sotto forma di collegamenti esterni. – Jolta