Jinxedgrimmm
Jinxedgrimmm

Reputation: 11

How to use unicode and multi byte character sets?

I have an app in C++ that needs to use the Unicode character set and multibyte. Is there a way to accomplish this? Let me note that having my project on multibyte is necessary and I also need to use this Unicode function

I have searched all over the internet and found nothing good.

DWORD GetProcId(const wchar_t *procName) // EXPERIMENTING
{
  DWORD procId = 0;
  HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if (hSnap != INVALID_HANDLE_VALUE) {
    PROCESSENTRY32 procEntry;
    procEntry.dwSize = sizeof(procEntry);

    if (Process32First(hSnap, &procEntry)) {
      do {
        if (!_wcsicmp(procEntry.szExeFile, procName)) // Error is here
        {
          procId = procEntry.th32ProcessID;
          break;
        }
      } while (Process32Next(hSnap, &procEntry));
    }
  }
  CloseHandle(hSnap);
  return procId;
}

the procEntry.szExeFile will only compile on Unicode character set but I need the application to use multibyte

Upvotes: 0

Views: 369

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598374

Your function takes a Unicode string as input, so you should explicitly use the Unicode version of the API, not the TCHAR version you are trying to use, eg:

DWORD GetProcId(const wchar_t *procName)
{
    DWORD procId = 0;
    HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    if (hSnap != INVALID_HANDLE_VALUE)
    {
        PROCESSENTRY32W procEntry;
        procEntry.dwSize = sizeof(procEntry);
        if (Process32FirstW(hSnap, &procEntry))
        {
            do
            {
                if (!_wcsicmp(procEntry.szExeFile, procName))
                {
                    procId = procEntry.th32ProcessID;
                    break;
                }
            }
            while (Process32NextW(hSnap, &procEntry));
        }
        CloseHandle(hSnap);
    }
    return procId;
}

Upvotes: 2

Related Questions