从循环值中找到一个值并进行比较

时间:2017-11-30 06:50:07

标签: c++ winapi

我在名为b.txt的文本文件中有一个可执行文件名列表,例如:

notepad.exe, chrome.exe

我需要将它们与当前进程名称进行比较,然后在应用程序匹配时执行某些操作:

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded);

    cProcesses = cbNeeded / sizeof(DWORD);

    for (i = 0; i < cProcesses; i++)
    {
        if (aProcesses[i] != 0)
        {
            DWORD processID = aProcesses[i];
            wchar_t szProcessName[MAX_PATH] = TEXT("<unknown>");
            HMODULE hMod;
            DWORD cbNeeded;


            HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
                PROCESS_VM_READ,
                FALSE, processID);
            if (EnumProcessModules(hProcess, &hMod, sizeof(hMod),
                &cbNeeded))
            {
                GetModuleBaseName(hProcess, hMod, szProcessName,
                    sizeof(szProcessName) / sizeof(TCHAR));

                _wcslwr_s(szProcessName, wcslen(szProcessName) + 1);

                FILE *pFile;
                wchar_t *file = L"c:\\b.txt";
                wchar_t line[100][44];
                unsigned int i = 0;
                if (_wfopen_s(&pFile, file, L"r, ccs = UNICODE") == 0)
                {
                    while (fgetws(line[i], 100, pFile))
                    {
                        i++;
                    }
                }

                if (line[i] == szProcessName)
                {
                    cout << szProcessName + "It's Found";
                }

            }


        }
    }

我不知道为什么这段代码不起作用。如果我按std::wcout测试每个,它输出正确,但比较总是失败。

1 个答案:

答案 0 :(得分:2)

该行:

if (line[i] == szProcessName)

仅比较两个字符串的地址,这当然是不同的。您想要比较这些地址的内容。请尝试使用wcsncmp()

if (wcsncmp(line[I], szProcessName, 44) == 0)