1

I am trying to import the code from the following answer: Get full running process list ( Visual C++ )

bool FindRunningProcess(AnsiString process) {
/*
Function takes in a string value for the process it is looking for like ST3Monitor.exe
then loops through all of the processes that are currently running on windows.
If the process is found it is running, therefore the function returns true.
*/
AnsiString compare;
bool procRunning = false;

HANDLE hProcessSnap;
PROCESSENTRY32 pe32;
hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (hProcessSnap == INVALID_HANDLE_VALUE) {
    procRunning = false;
} else {
    pe32.dwSize = sizeof(PROCESSENTRY32);
    if (Process32First(hProcessSnap, &pe32)) { // Gets first running process
        if (pe32.szExeFile == process) {
            procRunning = true;
        } else {
            // loop through all running processes looking for process
            while (Process32Next(hProcessSnap, &pe32)) { 
                // Set to an AnsiString instead of Char[] to make compare easier
                compare = pe32.szExeFile;
                if (compare == process) {
                    // if found process is running, set to true and break from loop
                    procRunning = true;
                    break;
                }
            }
        }
        // clean the snapshot object
        CloseHandle(hProcessSnap);
    }
}

In Phil's answer, he is using System::AnsiString class and im not sure how i can include this in my project, i.e. is it apart of the installed packages or do i need to download and include it?

An extension of this question: Is there another substitute i can use to achieve the same thing as AnsiString?

My end goal for this code is to modify it so that i can get the current list of running processes and i am looking for a specific process to terminate if it is running. I tried using a ce::string, but since pe32.szExeFile is type TCHAR [260], i am unable to pass it to a ce::string declaration of the following ce::string process_name; (which is probably why he is using System::AnsiString).

I assume pe32.szExeFile is going to return the process name, so i wanted to compare it to another declared string with the specific process name.

Community
  • 1
  • 1
Javia1492
  • 862
  • 11
  • 28

1 Answers1

5

Okay, so, it's not at all clear what AnsiString is; you've assumed it's the Embarcadero System::AnsiString class and, frankly, that looks like a reasonable assumption.

I wouldn't go about trying to obtain it, though. I would focus on writing standard code, switching to std::string/std::wstring (as appropriate). It should be near-trivial to adapt that author's code to be portable. You'll have to play around and read the documentation for the functions used in that code, to see what will work and what will not. It looks like System::AnsiString is almost or completely std::string-compatible, but you just won't know until you try it.

I cannot stress enough how important it is that you do not go down the road of stepping into your time machine and opening it up at 1950, with a lunch box full of pointers and horridly antiquated C-string comparison functions. I really don't get why anyone would suggest doing that.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I had already started to get away fron the `System::AnsiString` since i was not sure what it exactly was when originally posted. – Javia1492 Jun 02 '15 at 19:24
  • My only bump now is that I use`WINAPI::Process32Next()` to iterate through my running process list and it returns a `TCHAR [260]`, but i do not know how to handle this. I tried declaring a variable `TCHAR processname[260];` and pass the return variable to it, but it threw an error _'initializing': cannot convert from 'const char [12]' to 'TCHAR [260]'_. – Javia1492 Jun 02 '15 at 19:28
  • Just use the `PROCESSENTRY32::szExeFile` field it as-is, you don't need to copy it to a variable. And don't forget to process the value that is initially populated by `Process32First()`. – Remy Lebeau Jun 02 '15 at 19:30
  • @RemyLebeau How can i compare to an existing process's name if i know the name already? I.e., im not sure which types i need to use for initialization of the process's name. – Javia1492 Jun 02 '15 at 19:31
  • @Javia1492: `TCHAR[260]` will either be a C-string or a wide C-string, which may be directly compared to a `std::string` or `std::wstring` respectively. This is no problem. I honestly think that all you need to do here is to literally change the type of `process` and of `compare` from `AnsiString` to `std::string` or `std::wstring`. Did you try it yet? It would take all of a handful of seconds... – Lightness Races in Orbit Jun 02 '15 at 19:35
  • @Javia1492: What is `ce::string` actually declared as? Is it a typedef for `std::string` or `std::wstring`, or is it something else? You can replace `AnsiString` with `ce::string`, but then you should also replace `Process32First/Next()` with either `Process32FirstA/NextA()` or `Process32FirstW/NextW()` to match whatever data type `ce::string` is actually using for its character data, if it is not `TCHAR`. – Remy Lebeau Jun 02 '15 at 19:35
  • Not sure how you managed to get an error involving `const char[12]`. – Lightness Races in Orbit Jun 02 '15 at 19:36
  • @RemyLebeau `ce::string` is apart of the Windows Compact Embedded library. It is defined as `typedef _string_t string;` where `_string_t` is apart of the `ce` class. – Javia1492 Jun 02 '15 at 19:40
  • So i've managed to solve this issue which came down to my declaration style. – Javia1492 Jun 02 '15 at 20:26
  • @Javia1492: Great :) What did you use in the end? – Lightness Races in Orbit Jun 02 '15 at 20:27
  • I used a `TCHAR` array and compared it directly to the result of `pe32.szExefile` using `wcscmp` Compiler did not complain. – Javia1492 Jun 02 '15 at 20:30
  • @Javia1492: Why would you use an inferior solution? Especially after I went out of my way to warn you against doing so? – Lightness Races in Orbit Jun 02 '15 at 20:35
  • @Javia1492: since `ce::string` is using `char`, you should change the code to use `Process32FirstA()` and `Process32NextA()` directly, then you don't have to worry about `TCHAR` anymore, `pe32.szExeFile` will be a `char[]` array that you can compare using `strcmp()` or similar function, or `ce::string`'s own `==` operator (if it has one, which I assume it does). Don't deal with `TCHAR` unless you need to compile your code for both ANSI and UNICODE environments (which noone ever does anymore). – Remy Lebeau Jun 02 '15 at 20:38
  • @Remy: No-one? Really? – Lightness Races in Orbit Jun 02 '15 at 20:43
  • @LightnessRacesinOrbit That's because i get the following error when trying to use `string` or `wstring` _`wstring` ambiguous symbol_. – Javia1492 Jun 02 '15 at 20:47
  • @Javia1492: The proper response to that situation is to find out why that happens, and fix it (probably by simply writing `std::string` or `std::wstring`, as I told you to in the first place....). Not to resort to antiquated, error-prone and horrible old alternatives! – Lightness Races in Orbit Jun 02 '15 at 20:49
  • @LightnessRacesinOrbit Im sorry :(. There is no `string` or `wstring` declaration available in WEC according to [this](https://msdn.microsoft.com/en-us/library/ms927189.aspx), but i will try and use the other alternatives. – Javia1492 Jun 02 '15 at 20:51
  • @Javia1492: Those are the Windows API data types supported by Windows CE. Nothing to do with C++ itself. (As an obvious example, `bool` is not in that list, either.) If `wstring` is _ambiguous_ then you have _at least_ one definition of it, not zero. – Lightness Races in Orbit Jun 02 '15 at 20:52
  • @LightnessRacesinOrbit Didnt think of that. Found the proper declaration and it compiled. Im trying to take it but its hard to figure out everything when im not an expert. – Javia1492 Jun 02 '15 at 20:57
  • @Javia1492: Well I have told you exactly how to fix it :P But you still haven't tried that yet. – Lightness Races in Orbit Jun 02 '15 at 20:59
  • @LightnessRacesinOrbit I did manage to get it to work with `wstring`, which is what you suggested, right? – Javia1492 Jun 02 '15 at 21:00
  • @Javia1492: I said `std::wstring`, but you went off and plugged in `wstring`. Mine has five more characters! – Lightness Races in Orbit Jun 02 '15 at 21:01
  • @LightnessRacesinOrbit Eh, my mistake. By `wstring`, i meat `std::wstring`. – Javia1492 Jun 02 '15 at 21:02
  • @Javia1492: Eh, if `std::wstring` is being reported as "ambiguous", you've _really_ got trouble!! – Lightness Races in Orbit Jun 02 '15 at 21:03
  • @LightnessRacesinOrbit lol nono. I tried `wstring` originally, but i changed it to `std::wstring` which worked. – Javia1492 Jun 02 '15 at 21:03