2012-07-19 19 views
10

मुझे यहां एक गंभीर समस्या है। मुझे कंसोल विंडो प्रदर्शित किए बिना सी ++ के माध्यम से एक सीएमडी कमांड लाइन निष्पादित करने की आवश्यकता है। इसलिए मैं system(cmd) का उपयोग नहीं कर सकता, क्योंकि विंडो प्रदर्शित होगी।सी ++ निष्पादन सीएमडी कमांड

मैंने winExec(cmd, SW_HIDE) की कोशिश की है, लेकिन यह या तो काम नहीं करता है। CreateProcess एक और मैंने कोशिश की है। हालांकि, यह प्रोग्राम या बैच फ़ाइलों को चलाने के लिए है।

मैं ShellExecute कोशिश कर समाप्त हो गया है:

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

किसी को भी कुछ भी ऊपर कोड के साथ गलत देख सकते हैं? मैंने SW_SHOWNORMAL का उपयोग किया है जब तक कि मैं यह काम नहीं जानता।

मुझे वास्तव में इसके साथ कुछ मदद की ज़रूरत है। कुछ भी प्रकाश में नहीं आया है, और मैं थोड़ी देर के लिए कोशिश कर रहा हूं। कोई सलाह जो भी दे सकती है वह बहुत अच्छा होगा :)

+0

क्या आपने रिटर्न कोड चेक किया है? – Collin

+1

मुझे पता है कि आपके पास जवाब है, लेकिन यह सामान्य रूप से यह कहना अच्छा विचार है कि यह कैसे काम नहीं कर रहा है। – Deanna

+0

क्यों WMI_ फ़ंक्शंस को कॉल न करें और परिणाम को फ़ाइल में लिखें। कोई खिड़की नहीं और केवल आपको आवश्यक डेटा। –

उत्तर

6

अपने स्वयं के पाइप के उत्पादन पर रीडायरेक्ट किया जा एक tidier समाधान है, क्योंकि यह आउटपुट फ़ाइल बनाने से बचा जाता है, लेकिन यह ठीक काम करता है:

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

आप cmd विंडो नहीं दिख रहा है और उत्पादन की उम्मीद के रूप में पुनः निर्देशित किया जाता ।

आपका कोड शायद असफल रहा है (/C चीज़ के अलावा) क्योंकि आप "c:\\projects\\b" के बजाय "c:\projects\b" के रूप में पथ निर्दिष्ट करते हैं।

4

आपको CreateProcesscmd.exe पर /C पैरामीटर के साथ ipconfig कमांड सुरंग करने के लिए उपयोग करना चाहिए। > कमांड लाइन पर प्रति से काम नहीं करता है। आपको redirect programmatically the stdout होना है।

+0

यदि यह 'cmd/c' कमांड लाइन है, तो निश्चित रूप से'> 'पुनर्निर्देशन काम करेगा। – eryksun

3

यहां एक डॉसएक्सैक फ़ंक्शन का कार्यान्वयन है जो किसी भी डॉस कमांड को निष्पादित करने (चुपचाप) करने और यूनिकोड स्ट्रिंग के रूप में जेनरेट आउटपुट को पुनर्प्राप्त करने की अनुमति देता है।

// 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

नमूना कोड के लिए धन्यवाद! –

0

मैं GitHub पर एक समान कार्यक्रम [windows7 और 10 का परीक्षण] है

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

यह सर्वर प्रोग्राम है जो

    पर "स्थानीय क्षेत्र कनेक्शन"
  1. सुनता में इंटरफ़ेस नामित है यूडीपी पोर्ट (5555) के लिए खिड़कियां और udp पैकेट प्राप्त करता है।
  2. प्राप्त हुआ udp पैकेट सामग्री cmd.exe पर निष्पादित की गई है [कृपया cmd.exe आदेश चलाने के बाद बंद नहीं है और आउटपुट स्ट्रिंग [निष्पादित कमांड का आउटपुट] उसी यूड पोर्ट पर क्लाइंट प्रोग्राम में फ़ेबैक है]। > पार्स udp पैकेट - -> cmd.exe पर निष्पादित -
  3. दूसरे शब्दों में, आदेश udp पैकेट में प्राप्त> क्लाइंट प्रोग्राम के लिए एक ही बंदरगाह पर वापस भेज दिया उत्पादन

यह प्रदर्शित नहीं करता है "कंसोल विंडो" cmd.exe पर मैन्युअल रूप से कमांड करने के लिए किसी की आवश्यकता नहीं है remwin.exe पृष्ठभूमि में चल रहा है और इसका एक पतला सर्वर प्रोग्राम

+0

हाय, बहुत अधिक पुलिसिंग की तरह दिखता है, मैंने एक वैध परीक्षण लिंक डाला है और यह कोड का परीक्षण के साथ विशिष्ट उत्तर द्वारा विशिष्ट प्रश्न का उत्तर देता है, आपकी हटाना समीक्षा अनुशंसा को समझ में नहीं आता है। आप कोड [एमएसवीसी] चलाते हैं, इसके आउटपुट की जांच करें, इसका परीक्षण करें और यदि यह फिट नहीं है, तो इसे हटाएं। – particlereddy

+0

क्षमा करें, मैंने अपनी टिप्पणी हटा दी है। मैं ट्रिगर पर बहुत तेज़ था ... – Jolta

+0

अपना उत्तर पढ़ना, ऐसा लगता है कि आपने समस्या हल की है। हालांकि, स्टैक ओवरफ़्लो उन उत्तरों को हतोत्साहित करता है जो बाहरी लिंक के रूप में समाधान प्रदान करते हैं। – Jolta

संबंधित मुद्दे