使用System :: AnsiString类 [英] Using System::AnsiString class

查看:194
本文介绍了使用System :: AnsiString类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从以下答案中导入代码:获取完整的进程列表(Visual C ++)

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);
    }
}



在Phil的回答中,他使用 System :: AnsiString 类和我不知道我可以包括在我的项目,即它是已安装的软件包,还是我需要下载并包括它?

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?

这个问题的扩展:是否有另一个替代,我可以使用实现与AnsiString相同的东西?

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

我的这个代码的最终目标是修改它,以便我可以获得正在运行的进程的当前列表,我正在寻找一个特定的进程终止,如果它正在运行。我尝试使用 ce :: string ,但由于 pe32.szExeFile 是类型 TCHAR [ 260] ,我无法将它传递给 ce :: string process_name的 ce :: string 声明; (这可能是为什么他使用 System :: 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).

我假设 pe32.szExeFile 将返回进程名称,因此我想将其与另一个声明的字符串与特定进程名称进行比较。

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.

推荐答案

好吧,所以,不清楚什么 AnsiString 您认为它是 Embarcadero 系统:: AnsiString class ,坦白地说,这看起来像一个合理的假设

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.

获得它,虽然。 我将专注于编写标准代码,切换到 std :: string / std :: wstring 适当的)。它应该是微不足道的,以适应作者的代码是便携式的。你必须玩一下并阅读该代码中使用的函数的文档,看看什么会工作,什么不会工作。 看起来像 System :: AnsiString 几乎或完全 std :: string ,但你只是不知道,直到你尝试它。

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.

我不能强调,这是多么重要,你不走下去的道路您的时间机器并在1950年打开它,一个充满指针的午餐盒和可怕的陈旧的C字符串比较功能。我真的不明白为什么有人会建议这样做。

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.

这篇关于使用System :: AnsiString类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆