windows process list problem

Hi

I am using the code below to print a process name and id.

This works fine but I would like to get the process name into a variable rather than printing with _tprintf.

If I cout *szProcessName I get the unicode value of the first character, this is where i'm getting confused despite researching unicode and _tprintf. How could i get that value into a variable?

any pointers in the right direction (also regarding unicode and _tprintf) would be much appreciated.

TYIA

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
void PrintProcessName( 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 ); 

    CloseHandle( hProcess );
}
If you want to print out wide strings, use wcout instead.

if you want to return the process name, I suggest that you make the function return a std::wstring. But since you are using TCHAR, _tprintf(), etc. (which are the "character width-independent" data types), let's also make the choice between std::wstring and std::string an automatic thing too.

In a header that all other headers or codefiles include, add the following:

1
2
3
4
5
6
7
8
#ifdef _UNICODE
typedef std::wstring tstring;
#define tcin wcin
#define tcout wcout
#else
typedef std::string tstring;
#define tcin cin
#define tcout cout 


Now re-write PrintProcessName() and also change its name:

1
2
3
4
5
6
7
tstring GetProcessName(DWORD processID)
{
    ...
    //Instead of the _tprintf() line, just return a tstring, but before you do, close the process handle.
    CloseHandle(hProcess);
    return tstring(szProcessName);
}


Did not test it, but should work.
Last edited on
@ webJose: Just curious but why not just redefine the function as a template? Or even easier then both just cast TCHAR to a wide string? Not that your way is wrong, I just don't see precompiler instructions used for stuff like this often.
I just got used to take this approach because I am a Windows guy. As such, I get a precompiled header 99% of the time, and as you may know, every CPP in the project must include it. It is therefore a simple approach to me to typedef the type of string based on the Unicode choice.

Besides, a templated function adds typing when I use it, right? This way I don't have to type the <wstring> or <string> part. :-)
but why not just redefine the function as a template?
Two reasons:
1. you don't really want both kinds of strings in the same program, you're just going with the current type of string
2. Windows has set the precident for this sort of thing, you're just complying with their method. Take a look at _tcslen, _tprintf, and so on.
Topic archived. No new replies allowed.