-1

EDIT2: For those who think this is a duplicate, no this isn't. This was made in different context, knowing if a proccess is running and i did not knew i needed to convert till here 'martin' gives me the answer :p

EDIT: Answer was found! https://stackoverflow.com/a/12637971/4908011

I'm using Windows 7 with Visual Studio 2013 Deluxe. I can't seem to find any way to make a compatible x32 console application finding a process.

I need to know if a process is running, e.g. RobloxPlayerBeta.exe.

I keep getting the error:

WCHAR* incompatible with const char*

At 'strcmp(procEntry.szExeFile, name)'

The code I use:

bool ProcessRunning(const char* name)
{
HANDLE SnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

if (SnapShot == INVALID_HANDLE_VALUE)
    return false;

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

if (!Process32First(SnapShot, &procEntry))
    return false;

do
{
    if (strcmp(procEntry.szExeFile, name) == 0)
        return true;
} while (Process32Next(SnapShot, &procEntry));

return false;
}
Community
  • 1
  • 1

2 Answers2

3

TCHAR is a wide character type if your programs is compiled for Unicode, a narrow character type otherwise. When you create a new project with VS2013, it will be set for Unicode, so TCHAR is most likely a wide character type. The simplest solution is to change the project settings to compile for non-Unicode instead.

  1. Right-click on the project in the solution explorer window and select "Properties".
  2. Select "Configuration Properties" and then "General".
  3. Change the "Character Set" entry from "Unicode" to "Not Set" or "Multi-Byte".

When you compile now, TCHAR will be set to a narrow character type and your code should compile.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • That is a nice answer, when writing my typedef comment, I thought about that, too, but dismissed that again. This is probably the easiest solution. – martin May 31 '15 at 11:43
2

Well, the compiler message is clear:

in the PROCESSENTRY32 struct, szExeFile is of type tchar[] while your input to the function is a const char*. You get the error because strcmp expects char * and not a wide character. You need to either

  1. convert the char to a tchar string ( see, e.g., here)
  2. provide the input as a tchar string
  3. compare with an appropriate function (see, e.g., in here)

However, since you are using c++, why don't you use a std::wstring (and perform the conversion according to this question)

Community
  • 1
  • 1
martin
  • 3,149
  • 1
  • 24
  • 35
  • How do i convert it to tchar? EDIT: I'm a newbie to C++, and I am still learning, I'm in hope someone teaches me how to do those conversions and fix this... – realmaster42 May 31 '15 at 11:18
  • Uh oh, it talks about TCHAR and my problem is WCHAR. EDIT: Alright thanks then, i found out conversion and it worked. – realmaster42 May 31 '15 at 11:28
  • Well, since you compile with unicode, `TCHAR` is an typedef to `wchar_t`, @realmaster42 – martin May 31 '15 at 11:30