2

Found this useful code below, here in forum but it raised an error at this line

if (stricmp(entry.szExeFile, "target.exe") == 0)

says

Argument type "WCHAR *" is incompatible with parameter of type "const char *".

I'm using Visual Studio 2019.

#include <cstdio>
#include <windows.h>
#include <tlhelp32.h>

void EnableDebugPriv()
{
    HANDLE hToken;
    LUID luid;
    TOKEN_PRIVILEGES tkp;

    OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);

    LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid);

    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Luid = luid;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), NULL, NULL);

    CloseHandle(hToken);
}

int main(int, char* [])
{
    EnableDebugPriv();

    PROCESSENTRY32 entry;
    entry.dwSize = sizeof(PROCESSENTRY32);

    HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);

    if (Process32First(snapshot, &entry) == TRUE)
    {
        while (Process32Next(snapshot, &entry) == TRUE)
        {
            if (stricmp(entry.szExeFile, "target.exe") == 0)
            {
                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, entry.th32ProcessID);

                // Do stuff..

                CloseHandle(hProcess);
            }
        }
    }

    CloseHandle(snapshot);

    return 0;
}
GSerg
  • 76,472
  • 17
  • 159
  • 346
itTech
  • 346
  • 1
  • 7
  • 22
  • You're not using the `w*` prefixed version of `stricmp`, like [`_wcsicmp`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/stricmp-wcsicmp-mbsicmp-stricmp-l-wcsicmp-l-mbsicmp-l?view=vs-2019). – tadman Jul 30 '20 at 04:34
  • I think the bigger issue is that you are not familiar with what that error means. Knowing the difference between wide characters, narrow characters, ASCII, Unicode, UTF-8, etc. are huge topics. You really can't skip over knowing what character encoding and handling is all about. – PaulMcKenzie Jul 30 '20 at 04:39
  • @PaulMcKenzie yes am totally out of it because this is my first time to use c++ and it is very urgent. But what i didn't understand, it was a correct answer to this thread or should i say the best answer to what i need. https://stackoverflow.com/questions/865152/how-can-i-get-a-process-handle-by-its-name-in-c – itTech Jul 30 '20 at 05:09
  • (_tcsicmp(entry.szExeFile, _T("target.exe")) == 0) i miss this thing, it fixed the error :) – itTech Jul 30 '20 at 05:15
  • Does this answer your question? ['The argument of type WCHAR\* is not compatible with "const char\*"'](https://stackoverflow.com/questions/34276789/the-argument-of-type-wchar-is-not-compatible-with-const-char) – GSerg May 16 '21 at 09:58

1 Answers1

3

You are compiling your project with its Character Set option set to Unicode, so Process32First() and Process32Next() map to their Unicode versions, thus entry.szExeFile is a WCHAR[] array.

But stricmp() takes char input instead, which is why the code fails to compile.

You need to use the Unicode version of stricmp instead, which is wcsicmp() or _wcsicmp(), eg:

if (wcsicmp(entry.szExeFile, L"target.exe") == 0)

Or better, use _tcsicmp() in <tchar.h>, since you are actually using the TCHAR version of the Process32... functions, eg:

if (_tcsicmp(entry.szExeFile, _T("target.exe")) == 0)
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770