Question

I am trying to return process ID, and I have this code. What I am doing wrong?

Code is:

DWORD GetProcId(char* ProcName)//Get ProcessId By Name
{
    PROCESSENTRY32   pe32;
    HANDLE         hSnapshot = NULL;
    pe32.dwSize = sizeof( PROCESSENTRY32 );
    hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

    if( Process32First( hSnapshot, &pe32 ) )
    {
        do{
            if( strcmp( pe32.szExeFile, ProcName ) == 0 )
            {
                return pe32.th32ProcessID;
            }
        }while( Process32Next( hSnapshot, &pe32 ) );
    }

    if( hSnapshot != INVALID_HANDLE_VALUE )
        CloseHandle( hSnapshot );
    return 0;

}

And the error is 'strcmp' : cannot convert parameter 1 from 'WCHAR [260]' to 'const char *'

How I can fix it, I tried few different conversions but couldn't do it.

Was it helpful?

Solution 2

You're trying to compare a wide character string with a narrow character string. Since ProcName is the narrow character string, it must be that pe32.szExeFile is a wide character string. Not surprising since Windows uses wide characters internally. You should change char* ProcName to wchar_t* ProcName, and use wcscmp instead of strcmp.

OTHER TIPS

pe32.szExeFile is apparently a WCHAR string, rather than a normal char * C string. You will need to convert it before comparing. Alternately, convert ProcName to a WCHAR string and then use a wide string comparison function.

A quick check of the Visual Studio documentation shows that you might be looking for the wcscmp function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top