| Autor |
Nachricht |
wattsdattsn
Unregistrierter
|
wattsdattsn Unregistrierter
19:17:15 21.08.2009 Titel: |
system befehl ausgabe umleiten |
Zitieren |
hallo,
wenn ich schreib:
wie kann ich die ausgabe in einen string speichern? |
|
|
|
 |
asdfj
Unregistrierter
|
asdfj Unregistrierter
00:56:53 22.08.2009 Titel: |
|
Zitieren |
|
 |
BBBB
Unregistrierter
|
BBBB Unregistrierter
23:44:51 22.08.2009 Titel: |
Re: system befehl ausgabe umleiten |
Zitieren |
| wattsdattsn schrieb: | hallo,
wenn ich schreib:
wie kann ich die ausgabe in einen string speichern? |
Lieber auf system verzichten und die Windowsfunktionen nutzen. |
|
|
|
 |
csds
Unregistrierter
|
csds Unregistrierter
03:00:49 27.04.2012 Titel: |
|
Zitieren |
der threat ist zwar schon alt aber...
könnt ihr ****** nichtmal n beispiel angeben |
|
|
|
 |
Belli
Mitglied
Benutzerprofil
Anmeldungsdatum: 29.08.2009
Beiträge: 1773
|
Belli Mitglied
07:39:35 27.04.2012 Titel: |
|
Zitieren |
|
 |
merano
Mitglied
Benutzerprofil
Anmeldungsdatum: 21.12.2006
Beiträge: 412
|
merano Mitglied
13:49:36 27.04.2012 Titel: |
|
Zitieren |
Wenn es nur um eine "dir" Funktion geht wäre tatsächlich Win32
Systemaufruf der bessere Weg.
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #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 |
|
|
|
 |
Der Morgen
Unregistrierter
|
Der Morgen Unregistrierter
00:15:40 01.05.2012 Titel: |
|
Zitieren |
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 ? |
|
|
|
 |
knivil
Mitglied
Benutzerprofil
Anmeldungsdatum: 11.02.2009
Beiträge: 5867
|
knivil Mitglied
00:17:41 01.05.2012 Titel: |
|
Zitieren |
popen ... |
_________________ If it were not for laughter, there would be no Tao.
Sie können einen Beitrag nicht so schnell nach Ihrem letzten absenden, bitte warten Sie einen Augenblick.
|
|
 |
Der Morgen
Unregistrierter
|
Der Morgen Unregistrierter
00:23:26 01.05.2012 Titel: |
|
Zitieren |
Okay dann werd ich mich damit mal ausseinandersetzen, Dankeschön |
|
|
|
 |
Belli
Mitglied
Benutzerprofil
Anmeldungsdatum: 29.08.2009
Beiträge: 1773
|
Belli Mitglied
12:51:47 04.05.2012 Titel: |
|
Zitieren |
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:
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #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 | |
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #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:
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #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 | |
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #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:
| C++: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #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 << "***";
} | | |
|
|
|
 |
|
Nächstes Thema anzeigen
Vorheriges Thema anzeigen
Sie können Beiträge in dieses Forum schreiben. Sie können auf Beiträge in diesem Forum antworten. Sie können Ihre Beiträge in diesem Forum nicht bearbeiten. Sie können Ihre Beiträge in diesem Forum nicht löschen. Sie können an Umfragen in diesem Forum nicht mitmachen.
|
|
|
|
|