Jump to content
Tuts 4 You

(c++) simpe output


deepzero

Recommended Posts

hi,

this program is supposed to output the pids & names of all running processes:


PROCESSENTRY32 pe32;
HANDLE procsnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
pe32.dwSize = sizeof(PROCESSENTRY32);
Process32First(procsnap, &pe32);
while(Process32Next(procsnap, &pe32))
{
printf( "PID: %d [%s]\n", pe32.th32ProcessID, pe32.szExeFile);
}

However, only the first char of the process name is printed. :(

The PROCESSENTRY32 structure can be found here:
/>http://msdn.microsoft.com/en-us/library/ms684839%28v=VS.85%29.aspx

:unsure:

Edited by deepzero
Link to comment

there is a UNICODE and ANSI version of Process32First and Process32Next. You are using the UNICODE version...

solution:

use wprintf, change project to multi byte, convert wchar[] to char[], some more...

Link to comment

yes, you´re absolutely right.

looking at the definitions:

#define Process32First Process32FirstW

(then why offer a Process32First in the first place?)

So i can just use wprintf:

while(Process32Next(procsnap, &pe32))
{
//cout << (char *) pe32.szExeFile << endl;
::wprintf(L"PID: %d [ %s ]\n", pe32.th32ProcessID, pe32.szExeFile);
}

And now it works. Big thx.:hug:

edit:

any idea, though, how to print the output into a variable?

Edited by deepzero
Link to comment

I guess this is a mistake by microsoft developers. Normally you can easily choose the ANSI or UNICODE version of an api by adding an A or a W at the end of the name. Process32Next/Process32First is the sole exception (at least I dont know any other).

Link to comment

yes, you´re absolutely right.

looking at the definitions:

#define Process32First Process32FirstW

(then why offer a Process32First in the first place?)

So i can just use wprintf:

while(Process32Next(procsnap, &pe32))
{
//cout << (char *) pe32.szExeFile << endl;
::wprintf(L"PID: %d [ %s ]\n", pe32.th32ProcessID, pe32.szExeFile);
}

And now it works. Big thx.:hug:

edit:

any idea, though, how to print the output into a variable?

Use wsprintf and probably wcscat.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...