How to check process name help

Hello i will show the threads and process but i want to check a single process like notepad and show this only.
Complete code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
void PrintProcessNameAndID( DWORD processID )
{
    TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");

    // Get a handle to the process.

    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, processID );

    // Get the process name.

    if (NULL != hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
             &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName,
                               sizeof(szProcessName)/sizeof(TCHAR) );
        }
    }

    // Print the process name and identifier.
    //_tprintf( TEXT("%s  (PID: %u)\n"), szProcessName, processID );

	if(szProcessName=="chrome.exe"){
		printf("\n");
	}
    // Release the handle to the process.

    CloseHandle( hProcess );
}

int main( void )
{
    // Get the list of process identifiers.

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

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return 1;
    }


    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the name and process identifier for each process.

    for ( i = 0; i < cProcesses; i++ )
    {
        if( aProcesses[i] != 0 )
        {
            PrintProcessNameAndID( aProcesses[i] );
        }
    }

    return 0;
}


I want when szProcessName is notepad show it. But when i do this:
1
2
3
if(szProcessName=="notepad.exe"){
		printf("\n");
	}

Doesnt show me nothing why?? How i can check szProcessName with the name example notepad and show maybe szProcessName is not a string? When i do without checking obviously show me notepad.. in console.. and the other processes..
Last edited on
To compare TCHAR[] you need to use _tcscmp, the operator == will only compare the pointers, not their values.
http://stackoverflow.com/questions/35302605/compare-tchar-and-values-from-a-tchar

Also worth reading:
https://www.codeproject.com/Articles/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc
Last edited on
I did this and worked.
(!lstrcmp(szProcessName,"notepad.exe"))

But i dont understand when i do !lstrcmp the ! doesnt mean false? or for what is used the exclamation in strcmp(comparing) which is the utilify of this exclamation????????
You could have found this out by looking up lstrcmp() in a reference.
https://msdn.microsoft.com/en-us/library/windows/desktop/ms647488(v=vs.85).aspx

! is operator NOT, i.e., boolean negation.

lstrcmp() returns 0 (boolean false) when the strings are equivalent. Otherwise it returns a positive or negative integer (boolean true) depending on whether the compared string is less or greater than (according to Windows' CompareString() function).
Last edited on
So well then because in my if must to be true to print the message so if are equivalent i negate and then i got true and at last will print the equivalents.. example:
1
2
3
4
5
if(!lstrcmp(szProcessName,"notepad.exe")){
    // print equivalents
   printf(szProcessName);
   printf("\n");
}


if they are equivalents then return false so i must to negate to be true. Other explication for this i dont have.
Last edited on
Might be clearer to write the code like this:
1
2
3
4
5
6
const int EQUAL = 0;
if (lstrcmp(szProcessName,"notepad.exe") == EQUAL)
{
  printf(szProcessName);
  printf("\n");
}
Topic archived. No new replies allowed.