|
|
1.1 root 1:
2: /******************************************************************************\
3: * This is a part of the Microsoft Source Code Samples.
4: * Copyright (C) 1993 Microsoft Corporation.
5: * All rights reserved.
6: * This source code is only intended as a supplement to
7: * Microsoft Development Tools and/or WinHelp documentation.
8: * See these sources for detailed information regarding the
9: * Microsoft samples programs.
10: \******************************************************************************/
11:
12:
13: /******************************************************************************
14:
15: P E R F O R M A N C E D A T A
16:
17: Name: perfdata.c
18:
19: Description:
20: This module together with objdata.c, instdata.c, and cntrdata.c
21: access the performance data.
22:
23: ******************************************************************************/
24:
25: #include <windows.h>
26: #include <winperf.h>
27: #include "perfdata.h"
28: #include <stdlib.h>
29:
30:
31:
32:
33: LPTSTR *gPerfTitleSz;
34: LPTSTR TitleData;
35:
36:
37:
38:
39: //*********************************************************************
40: //
41: // GetPerfData
42: //
43: // Get a new set of performance data.
44: //
45: // *ppData should be NULL initially.
46: // This function will allocate a buffer big enough to hold the
47: // data requested by szObjectIndex.
48: //
49: // *pDataSize specifies the initial buffer size. If the size is
50: // too small, the function will increase it until it is big enough
51: // then return the size through *pDataSize. Caller should
52: // deallocate *ppData if it is no longer being used.
53: //
54: // Returns ERROR_SUCCESS if no error occurs.
55: //
56: // Note: the trial and error loop is quite different from the normal
57: // registry operation. Normally if the buffer is too small,
58: // RegQueryValueEx returns the required size. In this case,
59: // the perflib, since the data is dynamic, a buffer big enough
60: // for the moment may not be enough for the next. Therefor,
61: // the required size is not returned.
62: //
63: // One should start with a resonable size to avoid the overhead
64: // of reallocation of memory.
65: //
66: DWORD GetPerfData (HKEY hPerfKey,
67: LPTSTR szObjectIndex,
68: PPERF_DATA *ppData,
69: DWORD *pDataSize)
70: {
71: DWORD DataSize;
72: DWORD dwR;
73: DWORD Type;
74:
75:
76: if (!*ppData)
77: *ppData = (PPERF_DATA) LocalAlloc (LMEM_FIXED, *pDataSize);
78:
79:
80: do {
81: DataSize = *pDataSize;
82: dwR = RegQueryValueEx (hPerfKey,
83: szObjectIndex,
84: NULL,
85: &Type,
86: (BYTE *)*ppData,
87: &DataSize);
88:
89: if (dwR == ERROR_MORE_DATA)
90: {
91: LocalFree (*ppData);
92: *pDataSize += 1024;
93: *ppData = (PPERF_DATA) LocalAlloc (LMEM_FIXED, *pDataSize);
94: }
95:
96: if (!*ppData)
97: {
98: LocalFree (*ppData);
99: return ERROR_NOT_ENOUGH_MEMORY;
100: }
101:
102: } while (dwR == ERROR_MORE_DATA);
103:
104: return dwR;
105: }
106:
107:
108:
109:
110: #ifdef UNICODE
111:
112: #define atoi atoiW
113:
114:
115: //*********************************************************************
116: //
117: // atoiW
118: //
119: // Unicode version of atoi.
120: //
121: INT atoiW (LPTSTR s)
122: {
123: INT i = 0;
124:
125: while (isdigit (*s))
126: {
127: i = i*10 + (BYTE)*s - TEXT('0');
128: s++;
129: }
130:
131: return i;
132: }
133:
134: #endif
135:
136:
137:
138:
139: //*********************************************************************
140: //
141: // GetPerfTitleSz
142: //
143: // Retrieves the performance data title strings.
144: //
145: // This call retrieves english version of the title strings. For
146: // other locale the registry key should be changed to ..."perflib\\NNN"
147: //
148: // Caller should provide two pointers, one for buffering the title
149: // strings the other for indexing the title strings. This function will
150: // allocate memory for the TitleBuffer and TitleSz. To get the title
151: // string for a particular title index one would just index the TitleSz.
152: // *TitleCount returns the highest index can be used. If TitleSz[N] is
153: // NULL then there is no Title for index N.
154: //
155: // Example: TitleSz[20] points to titile string for title index 20.
156: //
157: // When done with the TitleSz, caller should LocalFree(*TitleBuffer).
158: //
159: // This function returns ERROR_SUCCESS if no error.
160: //
161: DWORD GetPerfTitleSz (HKEY hKeyMachine,
162: LPTSTR *TitleBuffer,
163: LPTSTR *TitleSz[],
164: DWORD *TitleCount)
165: {
166: HKEY hKey;
167: DWORD Type;
168: DWORD DataSize;
169: DWORD dwR;
170: DWORD Len;
171: DWORD Index;
172: LPTSTR szTitle;
173:
174:
175:
176:
177: // Get the last counter's index so we know how much memory to allocate for TitleSz
178: dwR = RegOpenKeyEx (hKeyMachine,
179: TEXT("software\\microsoft\\windows nt\\currentversion\\perflib"),
180: 0,
181: KEY_READ,
182: &hKey);
183:
184: if (dwR != ERROR_SUCCESS)
185: return dwR;
186:
187:
188: DataSize = sizeof (DWORD);
189: dwR = RegQueryValueEx (hKey, TEXT("Last Counter"), 0, &Type, (LPBYTE)TitleCount, &DataSize);
190:
191: if (dwR != ERROR_SUCCESS)
192: return dwR;
193:
194:
195:
196:
197:
198: // Now get the counter names and indexes.
199: dwR = RegOpenKeyEx (hKeyMachine,
200: TEXT("software\\microsoft\\windows nt\\currentversion\\perflib\\009"),
201: 0,
202: KEY_READ,
203: &hKey);
204:
205: if (dwR != ERROR_SUCCESS)
206: return dwR;
207:
208:
209: // Find out the size of the data.
210: dwR = RegQueryValueEx (hKey, TEXT("Counters"), 0, &Type, 0, &DataSize);
211:
212:
213:
214: // Allocate memory
215: *TitleBuffer = (LPTSTR)LocalAlloc (LMEM_FIXED, DataSize);
216: if (!*TitleBuffer)
217: return ERROR_NOT_ENOUGH_MEMORY;
218:
219: *TitleSz = (LPTSTR *)LocalAlloc (LPTR, (*TitleCount) * sizeof (LPTSTR));
220: if (!*TitleSz)
221: {
222: LocalFree (*TitleBuffer);
223: return ERROR_NOT_ENOUGH_MEMORY;
224: }
225:
226:
227:
228:
229:
230: // Query the data
231: dwR = RegQueryValueEx (hKey, TEXT("Counters"), 0, &Type, (BYTE *)*TitleBuffer, &DataSize);
232:
233: if (dwR != ERROR_SUCCESS)
234: {
235: LocalFree (*TitleBuffer);
236: LocalFree (*TitleSz);
237: return dwR;
238: }
239:
240:
241:
242:
243: // Setup the TitleSz array of pointers to point to beginning of each title string.
244: // TitleBuffer is type REG_MULTI_SZ.
245:
246: szTitle = *TitleBuffer;
247:
248: while (Len = lstrlen (szTitle))
249: {
250: Index = atoi (szTitle);
251:
252: szTitle = szTitle + Len +1;
253:
254: (*TitleSz)[Index] = szTitle;
255:
256: szTitle = szTitle + lstrlen (szTitle) +1;
257: }
258:
259:
260: return dwR;
261:
262: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.