return Process

partitionist

Erfahrenes Mitglied
Hab folgendes:

Code:
 void getPID()
 {
 	HANDLE hSysSnapshot = NULL;
 	PROCESSENTRY32 proc;
 
 	proc.dwSize = sizeof(proc);
 	hSysSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );
 	if ( hSysSnapshot == (HANDLE)-1 )
 	system("exit");
 
 	
 	if ( Process32First ( hSysSnapshot, &proc ) )
 	{
 		proc.dwSize = sizeof(proc);
 		cout << endl << "PID	Process";
 		cout << endl << "------------------" << endl;
 	
 		do
 		{    
 			printf ("%lu - %s\n", proc.th32ProcessID, proc.szExeFile );
 		}
 		while ( Process32Next ( hSysSnapshot, &proc ) );
 	}
 
 	CloseHandle ( hSysSnapshot );
 	cout << endl;
 }


Hier wird die PID und der Prozess ausgegeben.
Ich möchte aber das ich die Ausgabe in ein Array oder String speichern kann und zurückgeben nur wie lässt sich das in der do-Schleife realisieren?

z.B.
return proc.szExeFile;
 
Dazu benutze am besten std::string. Oder wenn du alle Prozesse willst, eine std::list mit std::string. Und dann machst du das besser nicht über den return-Wert sondern als Referenz-Übergabe:

void GetPID( std::list<std::string>& listProcesses )


Anmerkung:

system("exit"); ist ziemlich witzlos. Du machst einen weiteren Prozess auf und beendest ihn mit dem Aufruf von exit wieder. Das beeinflusst dein Programm überhaupt nicht. Generell würde ich system NIE benutzen (es sei denn du willst explizit andere Programme aufrufen, da würde ich unter Windows aber dann entweder WinExec oder CreateProcess benutzen).

Wenn du da dein Programm brutal beendet willst, dann ruf direkt exit( 0 ) auf. Finde ich aber auch nicht besonders sauber. Besser wäre da einfach ein return und die Liste der Prozesse bleibt leer.
 
Also beim mir meckert der Compiler wegen list es sei nicht deklariert hab auch using namespace std verwendet. Kannste mir bitte ein Beispiel zeigen wie man das mit der Liste machen kann
 
Hallo schaumal:
Code:
#include <iostream>
#include <list>

using namespace std;

int main(){
        list<string> str_list;
        str_list.push_back("Hallo");
        str_list.push_back("du");
        str_list.push_back("da!");
        while(!str_list.empty()){
                cout << str_list.front() << endl;
                str_list.pop_front();
        }
}

Falls du ne Referenz suchst:

http://www.sgi.com/tech/stl/table_of_contents.html


Gruß

RedWing
 
So hab jetzt ein bißchen geändert und es wird ein string zurückgegeben aber damit hab ich noch meine Probleme...
Code:
 string getProzess() 
 {
 	HANDLE hSysSnapshot = NULL;
 	PROCESSENTRY32 proc;
 
 	proc.dwSize = sizeof(proc);
 	hSysSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );
 	if ( hSysSnapshot == (HANDLE)-1 )
 	system("exit");
 	
 
 	
 	if ( Process32First ( hSysSnapshot, &proc ) )
 	{
 		proc.dwSize = sizeof(proc);
 		
 		do
 		{
 
 			list<string> str_list;
 		    str_list.push_back(proc.szExeFile);	   
 			while(!str_list.empty())
 			{
 		    	   cout << str_list.front() << endl;
 				   str_list.pop_front();
 		    	   //return str_list;    Hier soll die Liste dann zurückgegeben werden  
 			}
 		}
 		while ( Process32Next ( hSysSnapshot, &proc ) );
 	}
 	CloseHandle ( hSysSnapshot );
 }
 
endurion hat gesagt.:
Und dann machst du das besser nicht über den return-Wert sondern als Referenz-Übergabe:
Wie wäre es also damit:
Code:
 void getProzess(list<string>& proc_list) 
 {
 	HANDLE hSysSnapshot = NULL;
 	PROCESSENTRY32 proc;
 
 	proc.dwSize = sizeof(proc);
 	hSysSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );
 	if ( hSysSnapshot == (HANDLE)-1 )
 	system("exit");
 	
 
 	
 	if ( Process32First ( hSysSnapshot, &proc ) )
 	{
 		proc.dwSize = sizeof(proc);
 		
 		do
 		{
 		   proc_list.push_back(proc.szExeFile);	   
 		}
 		while ( Process32Next ( hSysSnapshot, &proc ) );
 	}
 	CloseHandle ( hSysSnapshot );
 }

Gruß

RedWing
 
Also nochmal ich möchte die ganze Liste der Prozessen in einer Variable haben, am besten string, die ich dann später per Sockets versende

@RedWing dein Beispiel gibt nichts zurück.

Also hier kurz was abgeschickt wird

Code:
 //Sendet an Client
 strcpy(status, Prozesse);
 			nRet = send(remoteSocket,	    		
 		    	status,	    		    	
 		    	strlen(status),	    	   
 				0);
 
Zurück