s***@gmail.com
2007-12-03 08:14:11 UTC
Hi All,
The following sample code works well on single byte languages and
prints the list of running processes in the box. But on double byte
languages like chinese, korean etc, it doesn't print the full process
name and it prints partially on a process which have space in the
process name.
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE 1
#endif
#define tmain wmain
#else
#define tmain main
#endif
#include <windows.h>
#include <winperf.h>
#include <malloc.h>
#include <stdio.h>
#include <tchar.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <stdlib.h>
int tmain ()
{
PDH_STATUS pdhStatus = ERROR_SUCCESS;
LPTSTR szCounterListBuffer = NULL;
DWORD dwCounterListSize = 0;
LPTSTR szInstanceListBuffer = NULL;
DWORD dwInstanceListSize = 0;
LPCSTR szThisInstance = NULL;
// Determine the required buffer size for the data.
pdhStatus = PdhEnumObjectItems (
NULL, // real time source
NULL, // local machine
TEXT("Process"), // object to enumerate
szCounterListBuffer, // pass NULL and 0
&dwCounterListSize, // to get length required
szInstanceListBuffer, // buffer size
&dwInstanceListSize, //
PERF_DETAIL_WIZARD, // counter detail level
0);
if (pdhStatus == PDH_MORE_DATA || pdhStatus == ERROR_SUCCESS)
{
// Allocate the buffers and try the call again.
szCounterListBuffer = (LPTSTR)malloc (
(dwCounterListSize * sizeof (TCHAR)));
szInstanceListBuffer = (LPTSTR)malloc (
(dwInstanceListSize * sizeof (TCHAR)));
if ((szCounterListBuffer != NULL) &&
(szInstanceListBuffer != NULL))
{
pdhStatus = PdhEnumObjectItems (
NULL, // real time source
NULL, // local machine
TEXT("Process"), // object to enumerate
szCounterListBuffer, // buffer to receive counter
list
&dwCounterListSize,
szInstanceListBuffer, // buffer to receive instance
list
&dwInstanceListSize,
PERF_DETAIL_WIZARD, // counter detail level
0);
if (pdhStatus == ERROR_SUCCESS)
{
_tprintf (TEXT("\nEnumerating Processes:"));
// Walk the instance list. The list can contain one
// or more null-terminated strings. The last string
// is followed by a second null-terminator.
szThisInstance = szInstanceListBuffer;
while(*szThisInstance)
{
_tprintf(TEXT("\n%s"),szThisInstance);
szThisInstance = szThisInstance + _tcslen(szThisInstance) *
sizeof(TCHAR);
szThisInstance = _tcsinc(szThisInstance);
}
}
else
{
_tprintf(TEXT("\nPdhEnumObjectItems failed1 with
%ld."), pdhStatus);
}
}
else
{
_tprintf (TEXT("\nUnable to allocate buffers"));
pdhStatus = ERROR_OUTOFMEMORY;
}
if (szCounterListBuffer != NULL)
free (szCounterListBuffer);
if (szInstanceListBuffer != NULL)
free (szInstanceListBuffer);
}
else
{
//_tprintf(TEXT("\nPdhEnumObjectItems failed2 with %ld."),
pdhStatus);
}
return pdhStatus;
}
Please help me to solve the issue.
The following sample code works well on single byte languages and
prints the list of running processes in the box. But on double byte
languages like chinese, korean etc, it doesn't print the full process
name and it prints partially on a process which have space in the
process name.
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#ifdef UNICODE
#ifndef _UNICODE
#define _UNICODE 1
#endif
#define tmain wmain
#else
#define tmain main
#endif
#include <windows.h>
#include <winperf.h>
#include <malloc.h>
#include <stdio.h>
#include <tchar.h>
#include <pdh.h>
#include <pdhmsg.h>
#include <stdlib.h>
int tmain ()
{
PDH_STATUS pdhStatus = ERROR_SUCCESS;
LPTSTR szCounterListBuffer = NULL;
DWORD dwCounterListSize = 0;
LPTSTR szInstanceListBuffer = NULL;
DWORD dwInstanceListSize = 0;
LPCSTR szThisInstance = NULL;
// Determine the required buffer size for the data.
pdhStatus = PdhEnumObjectItems (
NULL, // real time source
NULL, // local machine
TEXT("Process"), // object to enumerate
szCounterListBuffer, // pass NULL and 0
&dwCounterListSize, // to get length required
szInstanceListBuffer, // buffer size
&dwInstanceListSize, //
PERF_DETAIL_WIZARD, // counter detail level
0);
if (pdhStatus == PDH_MORE_DATA || pdhStatus == ERROR_SUCCESS)
{
// Allocate the buffers and try the call again.
szCounterListBuffer = (LPTSTR)malloc (
(dwCounterListSize * sizeof (TCHAR)));
szInstanceListBuffer = (LPTSTR)malloc (
(dwInstanceListSize * sizeof (TCHAR)));
if ((szCounterListBuffer != NULL) &&
(szInstanceListBuffer != NULL))
{
pdhStatus = PdhEnumObjectItems (
NULL, // real time source
NULL, // local machine
TEXT("Process"), // object to enumerate
szCounterListBuffer, // buffer to receive counter
list
&dwCounterListSize,
szInstanceListBuffer, // buffer to receive instance
list
&dwInstanceListSize,
PERF_DETAIL_WIZARD, // counter detail level
0);
if (pdhStatus == ERROR_SUCCESS)
{
_tprintf (TEXT("\nEnumerating Processes:"));
// Walk the instance list. The list can contain one
// or more null-terminated strings. The last string
// is followed by a second null-terminator.
szThisInstance = szInstanceListBuffer;
while(*szThisInstance)
{
_tprintf(TEXT("\n%s"),szThisInstance);
szThisInstance = szThisInstance + _tcslen(szThisInstance) *
sizeof(TCHAR);
szThisInstance = _tcsinc(szThisInstance);
}
}
else
{
_tprintf(TEXT("\nPdhEnumObjectItems failed1 with
%ld."), pdhStatus);
}
}
else
{
_tprintf (TEXT("\nUnable to allocate buffers"));
pdhStatus = ERROR_OUTOFMEMORY;
}
if (szCounterListBuffer != NULL)
free (szCounterListBuffer);
if (szInstanceListBuffer != NULL)
free (szInstanceListBuffer);
}
else
{
//_tprintf(TEXT("\nPdhEnumObjectItems failed2 with %ld."),
pdhStatus);
}
return pdhStatus;
}
Please help me to solve the issue.