msxml3: Support ISequentialStream in domdoc_transformNodeToObject.
[wine.git] / dlls / pdh / pdh_main.c
blobd3bf6ab87069fc24d4a712affe3dd6f40faad124
1 /*
2 * Performance Data Helper (pdh.dll)
4 * Copyright 2007 Andrey Turkin
5 * Copyright 2007 Hans Leidekker
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <math.h>
25 #define NONAMELESSUNION
27 #include "windef.h"
28 #include "winbase.h"
30 #include "pdh.h"
31 #include "pdhmsg.h"
32 #include "winperf.h"
34 #include "wine/debug.h"
35 #include "wine/heap.h"
36 #include "wine/list.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(pdh);
40 static CRITICAL_SECTION pdh_handle_cs;
41 static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug =
43 0, 0, &pdh_handle_cs,
44 { &pdh_handle_cs_debug.ProcessLocksList,
45 &pdh_handle_cs_debug.ProcessLocksList },
46 0, 0, { (DWORD_PTR)(__FILE__ ": pdh_handle_cs") }
48 static CRITICAL_SECTION pdh_handle_cs = { &pdh_handle_cs_debug, -1, 0, 0, 0, 0 };
50 static inline WCHAR *pdh_strdup( const WCHAR *src )
52 WCHAR *dst;
54 if (!src) return NULL;
55 if ((dst = heap_alloc( (lstrlenW( src ) + 1) * sizeof(WCHAR) ))) lstrcpyW( dst, src );
56 return dst;
59 static inline WCHAR *pdh_strdup_aw( const char *src )
61 int len;
62 WCHAR *dst;
64 if (!src) return NULL;
65 len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
66 if ((dst = heap_alloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
67 return dst;
70 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
72 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
73 switch (fdwReason)
75 case DLL_WINE_PREATTACH:
76 return FALSE; /* prefer native version */
77 case DLL_PROCESS_ATTACH:
78 DisableThreadLibraryCalls(hinstDLL);
79 break;
80 case DLL_PROCESS_DETACH:
81 if (lpvReserved) break;
82 DeleteCriticalSection(&pdh_handle_cs);
83 break;
86 return TRUE;
89 union value
91 LONG longvalue;
92 double doublevalue;
93 LONGLONG largevalue;
96 struct counter
98 DWORD magic; /* signature */
99 struct list entry; /* list entry */
100 WCHAR *path; /* identifier */
101 DWORD type; /* counter type */
102 DWORD status; /* update status */
103 LONG scale; /* scale factor */
104 LONG defaultscale; /* default scale factor */
105 DWORD_PTR user; /* user data */
106 DWORD_PTR queryuser; /* query user data */
107 LONGLONG base; /* samples per second */
108 FILETIME stamp; /* time stamp */
109 void (CALLBACK *collect)( struct counter * ); /* collect callback */
110 union value one; /* first value */
111 union value two; /* second value */
114 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
116 static struct counter *create_counter( void )
118 struct counter *counter;
120 if ((counter = heap_alloc_zero( sizeof(struct counter) )))
122 counter->magic = PDH_MAGIC_COUNTER;
123 return counter;
125 return NULL;
128 static void destroy_counter( struct counter *counter )
130 counter->magic = 0;
131 heap_free( counter->path );
132 heap_free( counter );
135 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
137 struct query
139 DWORD magic; /* signature */
140 DWORD_PTR user; /* user data */
141 HANDLE thread; /* collect thread */
142 DWORD interval; /* collect interval */
143 HANDLE wait; /* wait event */
144 HANDLE stop; /* stop event */
145 struct list counters; /* counter list */
148 static struct query *create_query( void )
150 struct query *query;
152 if ((query = heap_alloc_zero( sizeof(struct query) )))
154 query->magic = PDH_MAGIC_QUERY;
155 list_init( &query->counters );
156 return query;
158 return NULL;
161 static void destroy_query( struct query *query )
163 query->magic = 0;
164 heap_free( query );
167 struct source
169 DWORD index; /* name index */
170 const WCHAR *path; /* identifier */
171 void (CALLBACK *collect)( struct counter * ); /* collect callback */
172 DWORD type; /* counter type */
173 LONG scale; /* default scale factor */
174 LONGLONG base; /* samples per second */
177 static void CALLBACK collect_processor_time( struct counter *counter )
179 counter->two.largevalue = 500000; /* FIXME */
180 counter->status = PDH_CSTATUS_VALID_DATA;
183 static void CALLBACK collect_uptime( struct counter *counter )
185 counter->two.largevalue = GetTickCount64();
186 counter->status = PDH_CSTATUS_VALID_DATA;
189 #define TYPE_PROCESSOR_TIME \
190 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
191 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
193 #define TYPE_UPTIME \
194 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
196 /* counter source registry */
197 static const struct source counter_sources[] =
199 { 6, L"\\Processor(_Total)\\% Processor Time", collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 },
200 { 674, L"\\System\\System Up Time", collect_uptime, TYPE_UPTIME, -3, 1000 }
203 static BOOL is_local_machine( const WCHAR *name, DWORD len )
205 WCHAR buf[MAX_COMPUTERNAME_LENGTH + 1];
206 DWORD buflen = ARRAY_SIZE(buf);
208 if (!GetComputerNameW( buf, &buflen )) return FALSE;
209 return len == buflen && !wcsnicmp( name, buf, buflen );
212 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
214 const WCHAR *p;
216 if (path[0] == '\\' && path[1] == '\\' && (p = wcschr( path + 2, '\\' )) &&
217 is_local_machine( path + 2, p - path - 2 ))
219 path += p - path;
221 if (wcschr( path, '\\' )) p = fullpath;
222 else p = wcsrchr( fullpath, '\\' ) + 1;
223 return !wcscmp( p, path );
226 /***********************************************************************
227 * PdhAddCounterA (PDH.@)
229 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
230 DWORD_PTR userdata, PDH_HCOUNTER *counter )
232 PDH_STATUS ret;
233 WCHAR *pathW;
235 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
237 if (!path) return PDH_INVALID_ARGUMENT;
239 if (!(pathW = pdh_strdup_aw( path )))
240 return PDH_MEMORY_ALLOCATION_FAILURE;
242 ret = PdhAddCounterW( query, pathW, userdata, counter );
244 heap_free( pathW );
245 return ret;
248 /***********************************************************************
249 * PdhAddCounterW (PDH.@)
251 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
252 DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
254 struct query *query = hquery;
255 struct counter *counter;
256 unsigned int i;
258 TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
260 if (!path || !hcounter) return PDH_INVALID_ARGUMENT;
262 EnterCriticalSection( &pdh_handle_cs );
263 if (!query || query->magic != PDH_MAGIC_QUERY)
265 LeaveCriticalSection( &pdh_handle_cs );
266 return PDH_INVALID_HANDLE;
269 *hcounter = NULL;
270 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
272 if (pdh_match_path( counter_sources[i].path, path ))
274 if ((counter = create_counter()))
276 counter->path = pdh_strdup( counter_sources[i].path );
277 counter->collect = counter_sources[i].collect;
278 counter->type = counter_sources[i].type;
279 counter->defaultscale = counter_sources[i].scale;
280 counter->base = counter_sources[i].base;
281 counter->queryuser = query->user;
282 counter->user = userdata;
284 list_add_tail( &query->counters, &counter->entry );
285 *hcounter = counter;
287 LeaveCriticalSection( &pdh_handle_cs );
288 return ERROR_SUCCESS;
290 LeaveCriticalSection( &pdh_handle_cs );
291 return PDH_MEMORY_ALLOCATION_FAILURE;
294 LeaveCriticalSection( &pdh_handle_cs );
295 return PDH_CSTATUS_NO_COUNTER;
298 /***********************************************************************
299 * PdhAddEnglishCounterA (PDH.@)
301 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
302 DWORD_PTR userdata, PDH_HCOUNTER *counter )
304 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
306 if (!query) return PDH_INVALID_ARGUMENT;
307 return PdhAddCounterA( query, path, userdata, counter );
310 /***********************************************************************
311 * PdhAddEnglishCounterW (PDH.@)
313 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
314 DWORD_PTR userdata, PDH_HCOUNTER *counter )
316 TRACE("%p %s %lx %p\n", query, debugstr_w(path), userdata, counter);
318 if (!query) return PDH_INVALID_ARGUMENT;
319 return PdhAddCounterW( query, path, userdata, counter );
322 /* caller must hold counter lock */
323 static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
324 union value *raw2, PDH_FMT_COUNTERVALUE *value )
326 LONG factor;
328 factor = counter->scale ? counter->scale : counter->defaultscale;
329 if (format & PDH_FMT_LONG)
331 if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000;
332 else value->u.longValue = raw2->longvalue * pow( 10, factor );
334 else if (format & PDH_FMT_LARGE)
336 if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000;
337 else value->u.largeValue = raw2->largevalue * pow( 10, factor );
339 else if (format & PDH_FMT_DOUBLE)
341 if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000;
342 else value->u.doubleValue = raw2->doublevalue * pow( 10, factor );
344 else
346 WARN("unknown format %x\n", format);
347 return PDH_INVALID_ARGUMENT;
349 return ERROR_SUCCESS;
352 /***********************************************************************
353 * PdhCalculateCounterFromRawValue (PDH.@)
355 PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
356 PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
357 PPDH_FMT_COUNTERVALUE value )
359 PDH_STATUS ret;
360 struct counter *counter = handle;
362 TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value);
364 if (!value) return PDH_INVALID_ARGUMENT;
366 EnterCriticalSection( &pdh_handle_cs );
367 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
369 LeaveCriticalSection( &pdh_handle_cs );
370 return PDH_INVALID_HANDLE;
373 ret = format_value( counter, format, (union value *)&raw1->SecondValue,
374 (union value *)&raw2->SecondValue, value );
376 LeaveCriticalSection( &pdh_handle_cs );
377 return ret;
381 /***********************************************************************
382 * PdhCloseQuery (PDH.@)
384 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
386 struct query *query = handle;
387 struct list *item, *next;
389 TRACE("%p\n", handle);
391 EnterCriticalSection( &pdh_handle_cs );
392 if (!query || query->magic != PDH_MAGIC_QUERY)
394 LeaveCriticalSection( &pdh_handle_cs );
395 return PDH_INVALID_HANDLE;
398 if (query->thread)
400 HANDLE thread = query->thread;
401 SetEvent( query->stop );
402 LeaveCriticalSection( &pdh_handle_cs );
404 WaitForSingleObject( thread, INFINITE );
406 EnterCriticalSection( &pdh_handle_cs );
407 if (query->magic != PDH_MAGIC_QUERY)
409 LeaveCriticalSection( &pdh_handle_cs );
410 return ERROR_SUCCESS;
412 CloseHandle( query->stop );
413 CloseHandle( query->thread );
414 query->thread = NULL;
417 LIST_FOR_EACH_SAFE( item, next, &query->counters )
419 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
421 list_remove( &counter->entry );
422 destroy_counter( counter );
425 destroy_query( query );
427 LeaveCriticalSection( &pdh_handle_cs );
428 return ERROR_SUCCESS;
431 /* caller must hold query lock */
432 static void collect_query_data( struct query *query )
434 struct list *item;
436 LIST_FOR_EACH( item, &query->counters )
438 SYSTEMTIME time;
439 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
441 counter->collect( counter );
443 GetLocalTime( &time );
444 SystemTimeToFileTime( &time, &counter->stamp );
448 /***********************************************************************
449 * PdhCollectQueryData (PDH.@)
451 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
453 struct query *query = handle;
455 TRACE("%p\n", handle);
457 EnterCriticalSection( &pdh_handle_cs );
458 if (!query || query->magic != PDH_MAGIC_QUERY)
460 LeaveCriticalSection( &pdh_handle_cs );
461 return PDH_INVALID_HANDLE;
464 if (list_empty( &query->counters ))
466 LeaveCriticalSection( &pdh_handle_cs );
467 return PDH_NO_DATA;
470 collect_query_data( query );
472 LeaveCriticalSection( &pdh_handle_cs );
473 return ERROR_SUCCESS;
476 static DWORD CALLBACK collect_query_thread( void *arg )
478 struct query *query = arg;
479 DWORD interval = query->interval;
480 HANDLE stop = query->stop;
482 for (;;)
484 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
486 EnterCriticalSection( &pdh_handle_cs );
487 if (query->magic != PDH_MAGIC_QUERY)
489 LeaveCriticalSection( &pdh_handle_cs );
490 ExitThread( PDH_INVALID_HANDLE );
493 collect_query_data( query );
495 if (!SetEvent( query->wait ))
497 LeaveCriticalSection( &pdh_handle_cs );
498 ExitThread( 0 );
500 LeaveCriticalSection( &pdh_handle_cs );
504 /***********************************************************************
505 * PdhCollectQueryDataEx (PDH.@)
507 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
509 PDH_STATUS ret;
510 struct query *query = handle;
512 TRACE("%p %d %p\n", handle, interval, event);
514 EnterCriticalSection( &pdh_handle_cs );
515 if (!query || query->magic != PDH_MAGIC_QUERY)
517 LeaveCriticalSection( &pdh_handle_cs );
518 return PDH_INVALID_HANDLE;
520 if (list_empty( &query->counters ))
522 LeaveCriticalSection( &pdh_handle_cs );
523 return PDH_NO_DATA;
525 if (query->thread)
527 HANDLE thread = query->thread;
528 SetEvent( query->stop );
529 LeaveCriticalSection( &pdh_handle_cs );
531 WaitForSingleObject( thread, INFINITE );
533 EnterCriticalSection( &pdh_handle_cs );
534 if (query->magic != PDH_MAGIC_QUERY)
536 LeaveCriticalSection( &pdh_handle_cs );
537 return PDH_INVALID_HANDLE;
539 CloseHandle( query->thread );
540 query->thread = NULL;
542 else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
544 ret = GetLastError();
545 LeaveCriticalSection( &pdh_handle_cs );
546 return ret;
548 query->wait = event;
549 query->interval = interval * 1000;
550 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
552 ret = GetLastError();
553 CloseHandle( query->stop );
555 LeaveCriticalSection( &pdh_handle_cs );
556 return ret;
559 LeaveCriticalSection( &pdh_handle_cs );
560 return ERROR_SUCCESS;
563 /***********************************************************************
564 * PdhCollectQueryDataWithTime (PDH.@)
566 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
568 struct query *query = handle;
569 struct counter *counter;
570 struct list *item;
572 TRACE("%p %p\n", handle, timestamp);
574 if (!timestamp) return PDH_INVALID_ARGUMENT;
576 EnterCriticalSection( &pdh_handle_cs );
577 if (!query || query->magic != PDH_MAGIC_QUERY)
579 LeaveCriticalSection( &pdh_handle_cs );
580 return PDH_INVALID_HANDLE;
582 if (list_empty( &query->counters ))
584 LeaveCriticalSection( &pdh_handle_cs );
585 return PDH_NO_DATA;
588 collect_query_data( query );
590 item = list_head( &query->counters );
591 counter = LIST_ENTRY( item, struct counter, entry );
593 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
595 LeaveCriticalSection( &pdh_handle_cs );
596 return ERROR_SUCCESS;
599 /***********************************************************************
600 * PdhExpandWildCardPathA (PDH.@)
602 PDH_STATUS WINAPI PdhExpandWildCardPathA( LPCSTR szDataSource, LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
604 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
605 return PDH_NOT_IMPLEMENTED;
608 /***********************************************************************
609 * PdhExpandWildCardPathW (PDH.@)
611 PDH_STATUS WINAPI PdhExpandWildCardPathW( LPCWSTR szDataSource, LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
613 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
614 return PDH_NOT_IMPLEMENTED;
617 /***********************************************************************
618 * PdhExpandCounterPathA (PDH.@)
620 PDH_STATUS WINAPI PdhExpandCounterPathA( LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength )
622 FIXME("%s, %p, %p: stub\n", debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength);
623 return PdhExpandWildCardPathA(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
626 /***********************************************************************
627 * PdhExpandCounterPathW (PDH.@)
629 PDH_STATUS WINAPI PdhExpandCounterPathW( LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength )
631 FIXME("%s, %p, %p: stub\n", debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength);
632 return PdhExpandWildCardPathW(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
635 /***********************************************************************
636 * PdhGetCounterInfoA (PDH.@)
638 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
640 struct counter *counter = handle;
642 TRACE("%p %d %p %p\n", handle, text, size, info);
644 EnterCriticalSection( &pdh_handle_cs );
645 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
647 LeaveCriticalSection( &pdh_handle_cs );
648 return PDH_INVALID_HANDLE;
650 if (!size)
652 LeaveCriticalSection( &pdh_handle_cs );
653 return PDH_INVALID_ARGUMENT;
655 if (*size < sizeof(PDH_COUNTER_INFO_A))
657 *size = sizeof(PDH_COUNTER_INFO_A);
658 LeaveCriticalSection( &pdh_handle_cs );
659 return PDH_MORE_DATA;
662 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
664 info->dwType = counter->type;
665 info->CStatus = counter->status;
666 info->lScale = counter->scale;
667 info->lDefaultScale = counter->defaultscale;
668 info->dwUserData = counter->user;
669 info->dwQueryUserData = counter->queryuser;
671 *size = sizeof(PDH_COUNTER_INFO_A);
673 LeaveCriticalSection( &pdh_handle_cs );
674 return ERROR_SUCCESS;
677 /***********************************************************************
678 * PdhGetCounterInfoW (PDH.@)
680 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
682 struct counter *counter = handle;
684 TRACE("%p %d %p %p\n", handle, text, size, info);
686 EnterCriticalSection( &pdh_handle_cs );
687 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
689 LeaveCriticalSection( &pdh_handle_cs );
690 return PDH_INVALID_HANDLE;
692 if (!size)
694 LeaveCriticalSection( &pdh_handle_cs );
695 return PDH_INVALID_ARGUMENT;
697 if (*size < sizeof(PDH_COUNTER_INFO_W))
699 *size = sizeof(PDH_COUNTER_INFO_W);
700 LeaveCriticalSection( &pdh_handle_cs );
701 return PDH_MORE_DATA;
704 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
706 info->dwType = counter->type;
707 info->CStatus = counter->status;
708 info->lScale = counter->scale;
709 info->lDefaultScale = counter->defaultscale;
710 info->dwUserData = counter->user;
711 info->dwQueryUserData = counter->queryuser;
713 *size = sizeof(PDH_COUNTER_INFO_W);
715 LeaveCriticalSection( &pdh_handle_cs );
716 return ERROR_SUCCESS;
719 /***********************************************************************
720 * PdhGetCounterTimeBase (PDH.@)
722 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
724 struct counter *counter = handle;
726 TRACE("%p %p\n", handle, base);
728 if (!base) return PDH_INVALID_ARGUMENT;
730 EnterCriticalSection( &pdh_handle_cs );
731 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
733 LeaveCriticalSection( &pdh_handle_cs );
734 return PDH_INVALID_HANDLE;
737 *base = counter->base;
739 LeaveCriticalSection( &pdh_handle_cs );
740 return ERROR_SUCCESS;
743 /***********************************************************************
744 * PdhGetDllVersion (PDH.@)
746 PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
748 if (!version)
749 return PDH_INVALID_ARGUMENT;
751 *version = PDH_VERSION;
753 return ERROR_SUCCESS;
756 /***********************************************************************
757 * PdhGetFormattedCounterValue (PDH.@)
759 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
760 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
762 PDH_STATUS ret;
763 struct counter *counter = handle;
765 TRACE("%p %x %p %p\n", handle, format, type, value);
767 if (!value) return PDH_INVALID_ARGUMENT;
769 EnterCriticalSection( &pdh_handle_cs );
770 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
772 LeaveCriticalSection( &pdh_handle_cs );
773 return PDH_INVALID_HANDLE;
775 if (counter->status)
777 LeaveCriticalSection( &pdh_handle_cs );
778 return PDH_INVALID_DATA;
780 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
782 value->CStatus = ERROR_SUCCESS;
783 if (type) *type = counter->type;
786 LeaveCriticalSection( &pdh_handle_cs );
787 return ret;
790 /***********************************************************************
791 * PdhGetRawCounterValue (PDH.@)
793 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
794 PPDH_RAW_COUNTER value )
796 struct counter *counter = handle;
798 TRACE("%p %p %p\n", handle, type, value);
800 if (!value) return PDH_INVALID_ARGUMENT;
802 EnterCriticalSection( &pdh_handle_cs );
803 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
805 LeaveCriticalSection( &pdh_handle_cs );
806 return PDH_INVALID_HANDLE;
809 value->CStatus = counter->status;
810 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
811 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
812 value->FirstValue = counter->one.largevalue;
813 value->SecondValue = counter->two.largevalue;
814 value->MultiCount = 1; /* FIXME */
816 if (type) *type = counter->type;
818 LeaveCriticalSection( &pdh_handle_cs );
819 return ERROR_SUCCESS;
822 /***********************************************************************
823 * PdhLookupPerfIndexByNameA (PDH.@)
825 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
827 PDH_STATUS ret;
828 WCHAR *machineW = NULL;
829 WCHAR *nameW;
831 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
833 if (!name) return PDH_INVALID_ARGUMENT;
835 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
837 if (!(nameW = pdh_strdup_aw( name )))
838 return PDH_MEMORY_ALLOCATION_FAILURE;
840 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
842 heap_free( nameW );
843 heap_free( machineW );
844 return ret;
847 /***********************************************************************
848 * PdhLookupPerfIndexByNameW (PDH.@)
850 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
852 unsigned int i;
854 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
856 if (!name || !index) return PDH_INVALID_ARGUMENT;
858 if (machine)
860 FIXME("remote machine not supported\n");
861 return PDH_CSTATUS_NO_MACHINE;
863 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
865 if (pdh_match_path( counter_sources[i].path, name ))
867 *index = counter_sources[i].index;
868 return ERROR_SUCCESS;
871 return PDH_STRING_NOT_FOUND;
874 /***********************************************************************
875 * PdhLookupPerfNameByIndexA (PDH.@)
877 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
879 PDH_STATUS ret;
880 WCHAR *machineW = NULL;
881 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
882 DWORD sizeW = ARRAY_SIZE(bufferW);
884 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
886 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
888 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
890 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
892 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
894 if (*size < required) ret = PDH_MORE_DATA;
895 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
896 *size = required;
898 heap_free( machineW );
899 return ret;
902 /***********************************************************************
903 * PdhLookupPerfNameByIndexW (PDH.@)
905 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
907 PDH_STATUS ret;
908 unsigned int i;
910 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
912 if (machine)
914 FIXME("remote machine not supported\n");
915 return PDH_CSTATUS_NO_MACHINE;
918 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
919 if (!index) return ERROR_SUCCESS;
921 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
923 if (counter_sources[i].index == index)
925 WCHAR *p = wcsrchr( counter_sources[i].path, '\\' ) + 1;
926 unsigned int required = lstrlenW( p ) + 1;
928 if (*size < required) ret = PDH_MORE_DATA;
929 else
931 lstrcpyW( buffer, p );
932 ret = ERROR_SUCCESS;
934 *size = required;
935 return ret;
938 return PDH_INVALID_ARGUMENT;
941 /***********************************************************************
942 * PdhOpenQueryA (PDH.@)
944 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
946 PDH_STATUS ret;
947 WCHAR *sourceW = NULL;
949 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
951 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
953 ret = PdhOpenQueryW( sourceW, userdata, query );
954 heap_free( sourceW );
956 return ret;
959 /***********************************************************************
960 * PdhOpenQueryW (PDH.@)
962 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
964 struct query *query;
966 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
968 if (!handle) return PDH_INVALID_ARGUMENT;
970 if (source)
972 FIXME("log file data source not supported\n");
973 return PDH_INVALID_ARGUMENT;
975 if ((query = create_query()))
977 query->user = userdata;
978 *handle = query;
980 return ERROR_SUCCESS;
982 return PDH_MEMORY_ALLOCATION_FAILURE;
985 /***********************************************************************
986 * PdhRemoveCounter (PDH.@)
988 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
990 struct counter *counter = handle;
992 TRACE("%p\n", handle);
994 EnterCriticalSection( &pdh_handle_cs );
995 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
997 LeaveCriticalSection( &pdh_handle_cs );
998 return PDH_INVALID_HANDLE;
1001 list_remove( &counter->entry );
1002 destroy_counter( counter );
1004 LeaveCriticalSection( &pdh_handle_cs );
1005 return ERROR_SUCCESS;
1008 /***********************************************************************
1009 * PdhSetCounterScaleFactor (PDH.@)
1011 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
1013 struct counter *counter = handle;
1015 TRACE("%p\n", handle);
1017 EnterCriticalSection( &pdh_handle_cs );
1018 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1020 LeaveCriticalSection( &pdh_handle_cs );
1021 return PDH_INVALID_HANDLE;
1023 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
1025 LeaveCriticalSection( &pdh_handle_cs );
1026 return PDH_INVALID_ARGUMENT;
1029 counter->scale = factor;
1031 LeaveCriticalSection( &pdh_handle_cs );
1032 return ERROR_SUCCESS;
1035 /***********************************************************************
1036 * PdhValidatePathA (PDH.@)
1038 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1040 PDH_STATUS ret;
1041 WCHAR *pathW;
1043 TRACE("%s\n", debugstr_a(path));
1045 if (!path) return PDH_INVALID_ARGUMENT;
1046 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1048 ret = PdhValidatePathW( pathW );
1050 heap_free( pathW );
1051 return ret;
1054 static PDH_STATUS validate_path( LPCWSTR path )
1056 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1057 if (*path++ != '\\' || !wcschr( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1058 return ERROR_SUCCESS;
1061 /***********************************************************************
1062 * PdhValidatePathW (PDH.@)
1064 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1066 PDH_STATUS ret;
1067 unsigned int i;
1069 TRACE("%s\n", debugstr_w(path));
1071 if ((ret = validate_path( path ))) return ret;
1073 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
1074 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1076 return PDH_CSTATUS_NO_COUNTER;
1079 /***********************************************************************
1080 * PdhVbAddCounter (PDH.@)
1082 PDH_STATUS WINAPI PdhVbAddCounter( PDH_HQUERY query, LPCSTR path, PDH_HCOUNTER *counter )
1084 FIXME("%p, %s, %p: stub!\n", query, debugstr_a(path), counter);
1086 if (!path) return PDH_INVALID_ARGUMENT;
1088 return PDH_NOT_IMPLEMENTED;
1091 /***********************************************************************
1092 * PdhValidatePathExA (PDH.@)
1094 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1096 TRACE("%p %s\n", source, debugstr_a(path));
1098 if (source)
1100 FIXME("log file data source not supported\n");
1101 return ERROR_SUCCESS;
1103 return PdhValidatePathA( path );
1106 /***********************************************************************
1107 * PdhValidatePathExW (PDH.@)
1109 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1111 TRACE("%p %s\n", source, debugstr_w(path));
1113 if (source)
1115 FIXME("log file data source not supported\n");
1116 return ERROR_SUCCESS;
1118 return PdhValidatePathW( path );
1121 /***********************************************************************
1122 * PdhMakeCounterPathA (PDH.@)
1124 PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1125 LPDWORD buflen, DWORD flags )
1127 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1128 PDH_COUNTER_PATH_ELEMENTS_W eW;
1129 WCHAR *bufferW;
1130 DWORD buflenW;
1132 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1134 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1136 memset( &eW, 0, sizeof(eW) );
1137 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1138 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1139 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1140 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1141 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1142 eW.dwInstanceIndex = e->dwInstanceIndex;
1144 buflenW = 0;
1145 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1146 if (ret == PDH_MORE_DATA)
1148 if ((bufferW = heap_alloc( buflenW * sizeof(WCHAR) )))
1150 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1152 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1153 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1154 else ret = PDH_MORE_DATA;
1155 *buflen = len;
1157 heap_free( bufferW );
1159 else
1160 ret = PDH_MEMORY_ALLOCATION_FAILURE;
1163 done:
1164 heap_free( eW.szMachineName );
1165 heap_free( eW.szObjectName );
1166 heap_free( eW.szInstanceName );
1167 heap_free( eW.szParentInstance );
1168 heap_free( eW.szCounterName );
1169 return ret;
1172 /***********************************************************************
1173 * PdhMakeCounterPathW (PDH.@)
1175 PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1176 LPDWORD buflen, DWORD flags )
1178 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1179 PDH_STATUS ret = ERROR_SUCCESS;
1180 DWORD len;
1182 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1184 if (flags) FIXME("unimplemented flags 0x%08x\n", flags);
1186 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1187 return PDH_INVALID_ARGUMENT;
1189 path[0] = 0;
1190 if (e->szMachineName)
1192 lstrcatW(path, L"\\\\");
1193 lstrcatW(path, e->szMachineName);
1195 lstrcatW(path, L"\\");
1196 lstrcatW(path, e->szObjectName);
1197 if (e->szInstanceName)
1199 lstrcatW(path, L"(");
1200 if (e->szParentInstance)
1202 lstrcatW(path, e->szParentInstance);
1203 lstrcatW(path, L"/");
1205 lstrcatW(path, e->szInstanceName);
1206 swprintf(instance, ARRAY_SIZE(instance), L"#%u", e->dwInstanceIndex);
1207 lstrcatW(path, instance);
1208 lstrcatW(path, L")");
1210 lstrcatW(path, L"\\");
1211 lstrcatW(path, e->szCounterName);
1213 len = lstrlenW(path) + 1;
1214 if (*buflen >= len) lstrcpyW(buffer, path);
1215 else ret = PDH_MORE_DATA;
1216 *buflen = len;
1217 return ret;
1220 /***********************************************************************
1221 * PdhEnumObjectItemsA (PDH.@)
1223 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1224 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1225 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1227 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1228 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1229 pcchInstanceListLength, dwDetailLevel, dwFlags);
1231 return PDH_NOT_IMPLEMENTED;
1234 /***********************************************************************
1235 * PdhEnumObjectItemsW (PDH.@)
1237 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1238 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1239 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1241 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1242 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1243 pcchInstanceListLength, dwDetailLevel, dwFlags);
1245 return PDH_NOT_IMPLEMENTED;
1248 /***********************************************************************
1249 * PdhSetDefaultRealTimeDataSource (PDH.@)
1251 PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1253 FIXME("%u\n", source);
1254 return ERROR_SUCCESS;
1257 /***********************************************************************
1258 * PdhGetLogFileTypeA (PDH.@)
1260 PDH_STATUS WINAPI PdhGetLogFileTypeA(const char *log, DWORD *type)
1262 FIXME("%s, %p: stub\n", debugstr_a(log), type);
1263 return PDH_NOT_IMPLEMENTED;
1266 /***********************************************************************
1267 * PdhGetLogFileTypeW (PDH.@)
1269 PDH_STATUS WINAPI PdhGetLogFileTypeW(const WCHAR *log, DWORD *type)
1271 FIXME("%s, %p: stub\n", debugstr_w(log), type);
1272 return PDH_NOT_IMPLEMENTED;
1275 /***********************************************************************
1276 * PdhBindInputDataSourceA (PDH.@)
1278 PDH_STATUS WINAPI PdhBindInputDataSourceA(PDH_HLOG *source, const char *filenamelist)
1280 FIXME("%p %s: stub\n", source, debugstr_a(filenamelist));
1281 return PDH_NOT_IMPLEMENTED;
1284 /***********************************************************************
1285 * PdhBindInputDataSourceW (PDH.@)
1287 PDH_STATUS WINAPI PdhBindInputDataSourceW(PDH_HLOG *source, const WCHAR *filenamelist)
1289 FIXME("%p %s: stub\n", source, debugstr_w(filenamelist));
1290 return PDH_NOT_IMPLEMENTED;