-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtlist.cpp
More file actions
193 lines (162 loc) · 5.85 KB
/
tlist.cpp
File metadata and controls
193 lines (162 loc) · 5.85 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "header.h"
#define TITLE_SIZE 64
#define PROCESS_SIZE MAX_PATH
#define INITIAL_SIZE 51200
#define EXTEND_SIZE 25600
#define REGKEY_PERF "software\\microsoft\\windows nt\\currentversion\\perflib"
#define REGSUBKEY_COUNTERS "Counters"
#define PROCESS_COUNTER "process"
#define PROCESSID_COUNTER "id process"
#define UNKNOWN_TASK "Unknown"
struct TLIST {
DWORD dwProcessId;
CHAR ProcessName[PROCESS_SIZE];
struct TLIST *next;
};
int GetTaskList(struct TLIST *tlist)
{
DWORD rc;
HKEY hKeyNames;
DWORD dwType;
DWORD dwSize;
LPBYTE buf = NULL;
CHAR szSubKey[1024];
LANGID lid;
LPSTR p;
LPSTR p2;
PPERF_DATA_BLOCK pPerf;
PPERF_OBJECT_TYPE pObj;
PPERF_INSTANCE_DEFINITION pInst;
PPERF_COUNTER_BLOCK pCounter;
PPERF_COUNTER_DEFINITION pCounterDef;
DWORD i;
DWORD dwProcessIdTitle;
DWORD dwProcessIdCounter;
CHAR szProcessName[MAX_PATH];
DWORD dwNumTasks;
// this returns "009" on my system
lid = MAKELANGID( LANG_ENGLISH, SUBLANG_NEUTRAL );
wsprintf( szSubKey, "%s\\%03x", REGKEY_PERF, lid );
// Get handle to "khlm\sw\ms\winnt\cv\perflib\009"
rc = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
szSubKey,
0,
KEY_READ,
&hKeyNames
);
if (rc != ERROR_SUCCESS) {
goto exit;
}
// get the buffer size for the counter value
rc = RegQueryValueEx( hKeyNames, REGSUBKEY_COUNTERS, NULL, &dwType, NULL, &dwSize);
if (rc != ERROR_SUCCESS) goto exit;
// allocate the counter names buffer
buf = (LPBYTE) malloc(dwSize);
if (buf == NULL) goto exit;
memset( (void *) buf, 0, dwSize );
// read the counter names from the registry
rc = RegQueryValueEx(hKeyNames, REGSUBKEY_COUNTERS,NULL, &dwType, buf, &dwSize);
if (rc != ERROR_SUCCESS) {
goto exit;
}
p = (char *) buf;
dwProcessIdTitle = 0; // added by me as dwProcessIdTitle may be used without init
while (*p) {
if (p > (char *) buf) {
for( p2=p-2; isdigit(*p2); p2--) ;
}
if (lstrcmpi(p, PROCESS_COUNTER) == 0) {
for( p2=p-2; isdigit(*p2); p2--) ;
lstrcpy( szSubKey, p2+1 );
}
else
if (lstrcmpi(p, PROCESSID_COUNTER) == 0) {
for( p2=p-2; isdigit(*p2); p2--) ;
dwProcessIdTitle = atol( p2+1 );
}
p += (lstrlen(p) + 1);
}
free( buf );
dwSize = INITIAL_SIZE;
buf = (LPBYTE) malloc( dwSize );
if (buf == NULL) {
goto exit;
}
memset( buf, 0, dwSize );
while (TRUE) {
rc = RegQueryValueEx( HKEY_PERFORMANCE_DATA,szSubKey,NULL,&dwType,buf,&dwSize);
pPerf = (PPERF_DATA_BLOCK) buf;
if ((rc == ERROR_SUCCESS) &&
(dwSize > 0) &&
(pPerf)->Signature[0] == (WCHAR)'P' &&
(pPerf)->Signature[1] == (WCHAR)'E' &&
(pPerf)->Signature[2] == (WCHAR)'R' &&
(pPerf)->Signature[3] == (WCHAR)'F' ) {
break;
}
if (rc == ERROR_MORE_DATA) {
dwSize += EXTEND_SIZE;
buf = (LPBYTE) realloc( buf, dwSize );
memset( buf, 0, dwSize );
}
else {
goto exit;
}
}
dwProcessIdCounter = 0; /* added by me as var could be used without init */
pObj = (PPERF_OBJECT_TYPE) ((DWORD)pPerf + pPerf->HeaderLength);
pCounterDef = (PPERF_COUNTER_DEFINITION) ((DWORD)pObj + pObj->HeaderLength);
for (i=0; i<(DWORD)pObj->NumCounters; i++) {
if (pCounterDef->CounterNameTitleIndex == dwProcessIdTitle) {
dwProcessIdCounter = pCounterDef->CounterOffset;
break;
}
pCounterDef++;
}
dwNumTasks = (DWORD) pObj->NumInstances;
pInst = (PPERF_INSTANCE_DEFINITION) ((DWORD)pObj + pObj->DefinitionLength);
for (i=0; i<dwNumTasks; i++) {
p = (LPSTR) ((DWORD)pInst + pInst->NameOffset);
rc = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)p, -1, szProcessName,
sizeof(szProcessName), NULL, NULL);
if (!rc) {
lstrcpy( tlist->ProcessName, UNKNOWN_TASK );
}
// load process name into tlist
lstrcpy( tlist->ProcessName, szProcessName );
// load Pid into tlist
pCounter = (PPERF_COUNTER_BLOCK) ((DWORD)pInst + pInst->ByteLength);
tlist->dwProcessId = *((LPDWORD) ((DWORD)pCounter + dwProcessIdCounter));
pInst = (PPERF_INSTANCE_DEFINITION) ((DWORD)pCounter + pCounter->ByteLength);
// create a new tlist record
tlist->next = (struct TLIST *) HeapAlloc(GetProcessHeap(), 0, sizeof(struct TLIST));
if(!(tlist->next)) return(1);
tlist = tlist->next; // point to the new record
tlist->next = NULL; // in case this is last record, null serves as a flag
// note, last record of tlist will be undefined except next which is NULL
}
return(0);
exit:
if(buf) free(buf);
RegCloseKey( hKeyNames );
RegCloseKey( HKEY_PERFORMANCE_DATA );
return(1);
}
char *GetImageName(long pid)
{
static int init;
static struct TLIST tlist;
struct TLIST *t;
static char Unknown[] = "Unknown";
if(!init++) {
tlist.next = NULL;
if(GetTaskList(&tlist)) return NULL;
// build list of tasks
}
t = &tlist;
while(t->next) {
if(t->dwProcessId == (DWORD) pid) return t->ProcessName;
t = t->next;
}
return Unknown;
}