system befehl ausgabe umleiten



  • wattsdattsn schrieb:

    hallo,
    wenn ich schreib:

    system ("dir");
    

    wie kann ich die ausgabe in einen string speichern?

    Lieber auf system verzichten und die Windowsfunktionen nutzen.



  • der threat ist zwar schon alt aber...

    könnt ihr ****** nichtmal n beispiel angeben



  • csds schrieb:

    könnt ihr ****** nichtmal n beispiel angeben

    Hier:
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx



  • Wenn es nur um eine "dir" Funktion geht wäre tatsächlich Win32
    Systemaufruf der bessere Weg.

    #include <windows.h>
    #include <tchar.h> 
    #include <stdio.h>
    #include <strsafe.h>
    
    #pragma comment(lib, "User32.lib");
    
    void dir(TCHAR *szDir)
    {
      HANDLE hFind;
      WIN32_FIND_DATA finddata;
    
      // Find the first file in the directory.
      hFind = FindFirstFile(szDir, &finddata);
    
      // List all the files in the directory with some info about them.
      do {
         if(finddata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
            _tprintf(TEXT("  %s   <DIR>\n"), finddata.cFileName);
           }
         else {
            LARGE_INTEGER filesize;
            filesize.LowPart = finddata.nFileSizeLow;
            filesize.HighPart = finddata.nFileSizeHigh;
            _tprintf(TEXT("  %s   %ld bytes\n"), finddata.cFileName, filesize.QuadPart);
           }
      }
      while(FindNextFile(hFind, &finddata) != 0);
    
      FindClose(hFind);
    }
    

    siehe: Listing the Files in a Directory
    http://msdn.microsoft.com/en-us/library/windows/desktop/aa365200(v=vs.85).aspx



  • Hallo an alle,

    Ich weiss man soll auf System Aufrufe verzichten aber oft gibt es ja keine Alternative bzw man ist nicht in der Lage den Befehl neuzuschreiben 🙄

    gibt es die Möglichkeit eine System Ausgabe in einem string,char.. oder irgendwas zu Speichern, so das man zum Beispiel die Möglichkeit hat zwei Ausgaben miteinander zu vergleichen, ohne die Ergebnisse in ein file zu Speichern ? 😕



  • popen ...



  • Okay dann werd ich mich damit mal ausseinandersetzen, Dankeschön



  • Ich hab mal was gebastelt, was es einem erlaubt, einen Konsolenbefehl (dir oder sowas) abzusetzen und das Ergebnis in einem String-Vektor einzusammeln:
    Hilfsklasse pipe:

    #ifndef PIPEH
    #define PIPEH
    
    #include <windows.h>
    
    class pipe
    {
    	public:
    		enum pipeEnde {READ, WRITE};
    
    		pipe();
    		~pipe();
    
    		HANDLE GetPipeHandle(pipeEnde end);
    
    		void MakeInheritable(pipeEnde end);
    		void ClosePipeHandle(pipeEnde end);
    
    	private:
    
    		HANDLE readEnd, writeEnd;	
    
    		void MakeInheritable(HANDLE *h, bool inheritable);
    
    		//dont copy || assign
    		pipe(const pipe &);
    		pipe& operator=(const pipe &);
    };
    
    #endif
    
    #include "pipe.h"
    
    //Konstruktor
    pipe::pipe()
    {
    	CreatePipe(&readEnd, &writeEnd, NULL, 0);
    }
    
    //Destruktor
    pipe::~pipe()
    {
    	ClosePipeHandle(READ);
    	ClosePipeHandle(WRITE);
    }
    
    //public
    void pipe::ClosePipeHandle(pipeEnde end)
    {
    	switch(end)
    	{
    		case READ:
    				if(readEnd)
    				{
    					CloseHandle(readEnd);
    					readEnd = 0;
    				}
    				break;
    		case WRITE:
    				if(writeEnd)
    				{
    					CloseHandle(writeEnd);
    					writeEnd = 0;
    				}
    				break;
    	}
    }
    
    HANDLE pipe::GetPipeHandle(pipeEnde end)
    {
    	switch(end)
    	{
    		case READ:  return readEnd;
    		case WRITE: return writeEnd;
    	}
    
    	return NULL;
    }
    
    void pipe::MakeInheritable(pipeEnde end)
    {
    	switch(end)
    	{
    		case READ:
    				MakeInheritable(&readEnd, true); 
    				MakeInheritable(&writeEnd, false);
    				break;
    		case WRITE:
    				MakeInheritable(&readEnd, false); 
    				MakeInheritable(&writeEnd, true);
    				break;
    	}
    }
    
    //private
    void pipe::MakeInheritable(HANDLE *h, bool inheritable)
    {
    	if(!h)
    		return;
    
    	HANDLE proc = GetCurrentProcess();
    
    	DuplicateHandle(proc, *h, proc, h, 0, inheritable, DUPLICATE_SAME_ACCESS | DUPLICATE_CLOSE_SOURCE);
    }
    

    Klasse mySystem:

    #ifndef MYSYSTEMH
    #define MYSYSTEMH
    
    #include <vector>
    #include <string>
    #include "pipe.h"
    
    class mySystem
    {
    	public:
    		void doCommand(std::string command);
    		const std::vector<std::string> & getResultVector() const;
    
    	private:
    
    		std::vector<std::string> result;
    
    		void startCommandProcess(std::string cmd, HANDLE wrHandle);
    		void readToVector(HANDLE rHandle);	
    
    };
    
    #endif
    
    #include <iostream>
    #include "mysystem.h"
    
    //public
    const std::vector<std::string> & mySystem::getResultVector() const
    {
    	return result;
    }
    
    void mySystem::doCommand(std::string command)
    {
    	result.clear();
    
    	pipe readPipe;
    	readPipe.MakeInheritable(pipe::WRITE);
    	startCommandProcess(command, readPipe.GetPipeHandle(pipe::WRITE));
    
    	readPipe.ClosePipeHandle(pipe::WRITE);
    	readToVector(readPipe.GetPipeHandle(pipe::READ));
    }
    
    //private
    void mySystem::startCommandProcess(std::string cmd, HANDLE wrHandle)
    {
       PROCESS_INFORMATION pi;
       STARTUPINFO si;
    
       // Set up the start up info struct.
       ZeroMemory(&si,sizeof(STARTUPINFO));
       si.cb = sizeof(STARTUPINFO);
       si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
       si.hStdOutput = wrHandle;
       si.hStdError  = wrHandle;
    
       // Use this if you want to hide the child:
            si.wShowWindow = SW_HIDE;
       // Note that dwFlags must include STARTF_USESHOWWINDOW if you want to
       // use the wShowWindow flags.
    
       // Launch the process that you want to redirect (in this case,
       // Child.exe). Make sure Child.exe is in the same directory as
       // redirect.c launch redirect from a command line to prevent location
       // confusion.
    
       std::string command("cmd.exe /C ");
       command.append(cmd);
    
       char *cmdPtr = new char[command.size() + 1];
       lstrcpy(cmdPtr, command.c_str());
    
       CreateProcess(NULL, cmdPtr, NULL, NULL, TRUE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
    
    	delete [] cmdPtr;	
    
       // Close any unnecessary handles.
       CloseHandle(pi.hThread);
       CloseHandle(pi.hProcess);
    }
    
    void mySystem::readToVector(HANDLE rHandle)
    {
       char inBuf[256];
       DWORD nBytesRead;
    	std::string tmp;
    
       while(TRUE)
       {
          if (!ReadFile(rHandle, inBuf, /*sizeof(inBuf)*/ 10, &nBytesRead, NULL) || !nBytesRead)
          	break;
    
          for(int i = 0; i < nBytesRead; ++i)
          {
          	if(inBuf[i] == 10)
          	{
          		result.push_back(tmp);
          		tmp.clear();
          	}
          	else if(inBuf[i] == 13)
          		;
          	else
          		tmp.push_back(inBuf[i]);
          }
       }	
    
    }
    

    Testprogramm:

    #include <iostream>
    #include "mysystem.h"
    
    using namespace std;
    
    int main()
    {
    	mySystem ms;
    
    	ms.doCommand("dir");
    
    	vector<string> v = ms.getResultVector();
    
    	for(int i = 0; i < v.size(); ++i)
    	{
    		cout << v[i] << '\n';
    	}
    	cout << "***";
    
    	ms.doCommand("ipconfig /all");
    
    	v = ms.getResultVector();
    
    	for(int i = 0; i < v.size(); ++i)
    	{
    		cout << v[i] << '\n';
    	}
    	cout << "***";
    
    }
    


  • Der Morgen schrieb:

    Hallo an alle,
    ... aber oft gibt es ja keine Alternative bzw man ist nicht in der Lage den Befehl neuzuschreiben 🙄

    Was heißt denn oft?
    Und welche Systemkommandos sind so komplizert implementiert, das man sie nicht flott mittels entsprechender API oder Lib nachbauen /nachstellen kann?



  • Also wenn sich hier einer einbildet er kann programmieren nur weil er

    system ("irgendwas");
    

    zwischen main und return setzt. Der glaubt bestimmt auch er kann fliegen wenn er ausm Fernster hüpft.


Anmelden zum Antworten