Hello to enumerate the processes I use the following function:

void ProcessClass::GetProcessList() { m_count = 0; HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); if (snapshot != INVALID_HANDLE_VALUE) { PROCESSENTRY32 process; process.dwSize = sizeof(PROCESSENTRY32); if (Process32First(snapshot, &process)) { do { m_count++; //printf("%d) PID: %d, Base Address: %x, Size: %x, Name: %s, CountThread: %d\n", count, process.th32ProcessID, process.dwSize, process.dwSize, process.szExeFile, process.cntThreads); } while (Process32Next(snapshot, &process)); } else { } } else { } CloseHandle(snapshot); return; } 

This function iterates the data and displays it. How to separate the output from busting and execute it in the program, not in the classroom. Because there are cases when you need to use console applications, window and everywhere output can be different, that is, in this class data is prepared and transmitted, and the program already decides what to do with this data

  • Well, why not save info about each process in some kind of vector or list? And already with this list / vector to work as you want. If I understand you correctly, of course. - WenSiL
  • Correctly understood !!! But can I do this without using STL standard variables, pointers? I could make an array of PROCESSENTRY32 dynamic write everything there, transfer it to the program and then free it, but its size is not known, you can drive the cycle to empty then create this array and start writing to it, but the number of processes may change by this time. And in general, this approach is correct, if we do for example an updated list of processes every 5 seconds, suppose? - helldrg
  • And why you do not want to use stl? After all, it will be much more convenient and less hemorrhoids will be than with an array. And at the expense of your idea: If you do as you want and you will clear the memory for the array immediately after transferring it to DOS. the program, then the pointer in this program itself will refer to a non-existent memory. - WenSiL
  • I would do this: create a list <PROCESSENTRY32>, create a private function that will clear the list and refill it with fresh processes, and we wrote some kind of const list & GetProcesses () method and it would have the same update method. then your program, when accessing the GetProcesses () method, will have the most current processes and no hemorrhoids with memory, etc. Well, or the update method can be made public and you can simply call it if necessary, so that your list inside the OS program is relevant. - WenSiL
  • It is much more convenient to write in C # if we are talking about convenience. Why does everyone use char * and not string? I don’t like to use fancy things, I use PROCESSENTRY32 only because it is winapi, that is, I use C ++ to work with the OS, and the STL boost library is not mine, although I myself am going to use stb_truetype =) I don’t know how I be. Perhaps the allocator is needed to change the size of the PROCESSENTRY32 array, if it cannot be deleted - helldrg

1 answer 1

I would do this: create a list<PROCESSENTRY32> , create a private update() method that will clear the list and refill it with fresh processes, and write some kind of const list& GetProcesses() method and in it would be calling the update() method, then your program will have the most current processes when accessing the GetProcesses() method. Well, or you can make the update() method public and simply call it if necessary, so that your list inside the main program is relevant.

  • and in the program you need to create a variable of type list <PROCESSENTRY32> and initialize list <PROCESSENTRY32> listProcess = Process.GetProcesses (); for (std :: list <int> :: iterator iter = listProcess.begin (); iter! = listProcess.end (); ++ iter) std :: cout << * iter << ""; So or something different? And clear through clear? - helldrg
  • Clear through clear() , yes. Your initialization is not quite correct. If you followed my method, the GetProcesses() method returns a reference to the list, hence it is better to accept the reference. list<PROCESSENTRY32> &listProcess = Process.GetProcesses(); and in your case GetProcesses() returns the link, but since the receiving object is not a link, a copy is created that was returned by GetProcesses() - WenSiL
  • The list<PROCESSENTRY32> &listProcess = Process.GetProcesses(); this record ( list<PROCESSENTRY32> &listProcess = Process.GetProcesses(); ) is that if the update() method is not private, it will be sufficient to use this record only once, and if necessary, only update() data update() As for running through the list: if it’s not convenient for you to use iterators, you can use vector<PROCESSENTRY32> instead of a list, it has a more familiar indexing operation [ ] . You can also use the for(auto x : listProcess) (if I'm not mistaken in the record), this cycle will run through all elements of the collection itself. - WenSiL
  • list <PROCESSENTRY32> & listProcess about this thanks !!! I figured out the rest, especially when there was a need for a cycle to do not begin to end, but vice versa - I learned a lot of new things =) Now I’ll use the list when using the listView, thanks to you. Good or bad we will see, I hope good =) - helldrg
  • I also advise you to familiarize yourself with this directory , perhaps you will find something else useful for your program. - WenSiL