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 #define WIN32_NO_STATUS
27 #define NONAMELESSUNION
28 #define NONAMELESSSTRUCT
34 //#include "winperf.h"
36 #include <wine/debug.h>
37 #include <wine/list.h>
38 #include <wine/unicode.h>
40 WINE_DEFAULT_DEBUG_CHANNEL(pdh
);
42 static CRITICAL_SECTION pdh_handle_cs
;
43 static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug
=
46 { &pdh_handle_cs_debug
.ProcessLocksList
,
47 &pdh_handle_cs_debug
.ProcessLocksList
},
48 0, 0, { (DWORD_PTR
)(__FILE__
": pdh_handle_cs") }
50 static CRITICAL_SECTION pdh_handle_cs
= { &pdh_handle_cs_debug
, -1, 0, 0, 0, 0 };
52 static inline void *heap_alloc( SIZE_T size
)
54 return HeapAlloc( GetProcessHeap(), 0, size
);
57 static inline void *heap_alloc_zero( SIZE_T size
)
59 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, size
);
62 static inline void heap_free( LPVOID mem
)
64 HeapFree( GetProcessHeap(), 0, mem
);
67 static inline WCHAR
*pdh_strdup( const WCHAR
*src
)
71 if (!src
) return NULL
;
72 if ((dst
= heap_alloc( (strlenW( src
) + 1) * sizeof(WCHAR
) ))) strcpyW( dst
, src
);
76 static inline WCHAR
*pdh_strdup_aw( const char *src
)
81 if (!src
) return NULL
;
82 len
= MultiByteToWideChar( CP_ACP
, 0, src
, -1, NULL
, 0 );
83 if ((dst
= heap_alloc( len
* sizeof(WCHAR
) ))) MultiByteToWideChar( CP_ACP
, 0, src
, -1, dst
, len
);
87 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
89 TRACE("(0x%p, %d, %p)\n",hinstDLL
,fdwReason
,lpvReserved
);
91 if (fdwReason
== DLL_WINE_PREATTACH
) return FALSE
; /* prefer native version */
93 if (fdwReason
== DLL_PROCESS_ATTACH
)
95 DisableThreadLibraryCalls( hinstDLL
);
110 DWORD magic
; /* signature */
111 struct list entry
; /* list entry */
112 WCHAR
*path
; /* identifier */
113 DWORD type
; /* counter type */
114 DWORD status
; /* update status */
115 LONG scale
; /* scale factor */
116 LONG defaultscale
; /* default scale factor */
117 DWORD_PTR user
; /* user data */
118 DWORD_PTR queryuser
; /* query user data */
119 LONGLONG base
; /* samples per second */
120 FILETIME stamp
; /* time stamp */
121 void (CALLBACK
*collect
)( struct counter
* ); /* collect callback */
122 union value one
; /* first value */
123 union value two
; /* second value */
126 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
128 static struct counter
*create_counter( void )
130 struct counter
*counter
;
132 if ((counter
= heap_alloc_zero( sizeof(struct counter
) )))
134 counter
->magic
= PDH_MAGIC_COUNTER
;
140 static void destroy_counter( struct counter
*counter
)
143 heap_free( counter
->path
);
144 heap_free( counter
);
147 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
151 DWORD magic
; /* signature */
152 DWORD_PTR user
; /* user data */
153 HANDLE thread
; /* collect thread */
154 DWORD interval
; /* collect interval */
155 HANDLE wait
; /* wait event */
156 HANDLE stop
; /* stop event */
157 struct list counters
; /* counter list */
160 static struct query
*create_query( void )
164 if ((query
= heap_alloc_zero( sizeof(struct query
) )))
166 query
->magic
= PDH_MAGIC_QUERY
;
167 list_init( &query
->counters
);
173 static void destroy_query( struct query
*query
)
181 DWORD index
; /* name index */
182 const WCHAR
*path
; /* identifier */
183 void (CALLBACK
*collect
)( struct counter
* ); /* collect callback */
184 DWORD type
; /* counter type */
185 LONG scale
; /* default scale factor */
186 LONGLONG base
; /* samples per second */
189 static const WCHAR path_processor_time
[] =
190 {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
191 '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
192 static const WCHAR path_uptime
[] =
193 {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
195 static void CALLBACK
collect_processor_time( struct counter
*counter
)
197 counter
->two
.largevalue
= 500000; /* FIXME */
198 counter
->status
= PDH_CSTATUS_VALID_DATA
;
201 static void CALLBACK
collect_uptime( struct counter
*counter
)
203 counter
->two
.largevalue
= GetTickCount64();
204 counter
->status
= PDH_CSTATUS_VALID_DATA
;
207 #define TYPE_PROCESSOR_TIME \
208 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
209 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
211 #define TYPE_UPTIME \
212 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
214 /* counter source registry */
215 static const struct source counter_sources
[] =
217 { 6, path_processor_time
, collect_processor_time
, TYPE_PROCESSOR_TIME
, -5, 10000000 },
218 { 674, path_uptime
, collect_uptime
, TYPE_UPTIME
, -3, 1000 }
221 static BOOL
pdh_match_path( LPCWSTR fullpath
, LPCWSTR path
)
225 if (strchrW( path
, '\\')) p
= fullpath
;
226 else p
= strrchrW( fullpath
, '\\' ) + 1;
227 if (strcmpW( p
, path
)) return FALSE
;
231 /***********************************************************************
232 * PdhAddCounterA (PDH.@)
234 PDH_STATUS WINAPI
PdhAddCounterA( PDH_HQUERY query
, LPCSTR path
,
235 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
240 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
242 if (!path
) return PDH_INVALID_ARGUMENT
;
244 if (!(pathW
= pdh_strdup_aw( path
)))
245 return PDH_MEMORY_ALLOCATION_FAILURE
;
247 ret
= PdhAddCounterW( query
, pathW
, userdata
, counter
);
253 /***********************************************************************
254 * PdhAddCounterW (PDH.@)
256 PDH_STATUS WINAPI
PdhAddCounterW( PDH_HQUERY hquery
, LPCWSTR path
,
257 DWORD_PTR userdata
, PDH_HCOUNTER
*hcounter
)
259 struct query
*query
= hquery
;
260 struct counter
*counter
;
263 TRACE("%p %s %lx %p\n", hquery
, debugstr_w(path
), userdata
, hcounter
);
265 if (!path
|| !hcounter
) return PDH_INVALID_ARGUMENT
;
267 EnterCriticalSection( &pdh_handle_cs
);
268 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
270 LeaveCriticalSection( &pdh_handle_cs
);
271 return PDH_INVALID_HANDLE
;
275 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
277 if (pdh_match_path( counter_sources
[i
].path
, path
))
279 if ((counter
= create_counter()))
281 counter
->path
= pdh_strdup( counter_sources
[i
].path
);
282 counter
->collect
= counter_sources
[i
].collect
;
283 counter
->type
= counter_sources
[i
].type
;
284 counter
->defaultscale
= counter_sources
[i
].scale
;
285 counter
->base
= counter_sources
[i
].base
;
286 counter
->queryuser
= query
->user
;
287 counter
->user
= userdata
;
289 list_add_tail( &query
->counters
, &counter
->entry
);
292 LeaveCriticalSection( &pdh_handle_cs
);
293 return ERROR_SUCCESS
;
295 LeaveCriticalSection( &pdh_handle_cs
);
296 return PDH_MEMORY_ALLOCATION_FAILURE
;
299 LeaveCriticalSection( &pdh_handle_cs
);
300 return PDH_CSTATUS_NO_COUNTER
;
303 /***********************************************************************
304 * PdhAddEnglishCounterA (PDH.@)
306 PDH_STATUS WINAPI
PdhAddEnglishCounterA( PDH_HQUERY query
, LPCSTR path
,
307 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
309 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
311 if (!query
) return PDH_INVALID_ARGUMENT
;
312 return PdhAddCounterA( query
, path
, userdata
, counter
);
315 /***********************************************************************
316 * PdhAddEnglishCounterW (PDH.@)
318 PDH_STATUS WINAPI
PdhAddEnglishCounterW( PDH_HQUERY query
, LPCWSTR path
,
319 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
321 TRACE("%p %s %lx %p\n", query
, debugstr_w(path
), userdata
, counter
);
323 if (!query
) return PDH_INVALID_ARGUMENT
;
324 return PdhAddCounterW( query
, path
, userdata
, counter
);
327 /* caller must hold counter lock */
328 static PDH_STATUS
format_value( struct counter
*counter
, DWORD format
, union value
*raw1
,
329 union value
*raw2
, PDH_FMT_COUNTERVALUE
*value
)
333 factor
= counter
->scale
? counter
->scale
: counter
->defaultscale
;
334 if (format
& PDH_FMT_LONG
)
336 if (format
& PDH_FMT_1000
) value
->u
.longValue
= raw2
->longvalue
* 1000;
337 else value
->u
.longValue
= raw2
->longvalue
* pow( 10, factor
);
339 else if (format
& PDH_FMT_LARGE
)
341 if (format
& PDH_FMT_1000
) value
->u
.largeValue
= raw2
->largevalue
* 1000;
342 else value
->u
.largeValue
= raw2
->largevalue
* pow( 10, factor
);
344 else if (format
& PDH_FMT_DOUBLE
)
346 if (format
& PDH_FMT_1000
) value
->u
.doubleValue
= raw2
->doublevalue
* 1000;
347 else value
->u
.doubleValue
= raw2
->doublevalue
* pow( 10, factor
);
351 WARN("unknown format %x\n", format
);
352 return PDH_INVALID_ARGUMENT
;
354 return ERROR_SUCCESS
;
357 /***********************************************************************
358 * PdhCalculateCounterFromRawValue (PDH.@)
360 PDH_STATUS WINAPI
PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle
, DWORD format
,
361 PPDH_RAW_COUNTER raw1
, PPDH_RAW_COUNTER raw2
,
362 PPDH_FMT_COUNTERVALUE value
)
365 struct counter
*counter
= handle
;
367 TRACE("%p 0x%08x %p %p %p\n", handle
, format
, raw1
, raw2
, value
);
369 if (!value
) return PDH_INVALID_ARGUMENT
;
371 EnterCriticalSection( &pdh_handle_cs
);
372 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
374 LeaveCriticalSection( &pdh_handle_cs
);
375 return PDH_INVALID_HANDLE
;
378 ret
= format_value( counter
, format
, (union value
*)&raw1
->SecondValue
,
379 (union value
*)&raw2
->SecondValue
, value
);
381 LeaveCriticalSection( &pdh_handle_cs
);
386 /***********************************************************************
387 * PdhCloseQuery (PDH.@)
389 PDH_STATUS WINAPI
PdhCloseQuery( PDH_HQUERY handle
)
391 struct query
*query
= handle
;
392 struct list
*item
, *next
;
394 TRACE("%p\n", handle
);
396 EnterCriticalSection( &pdh_handle_cs
);
397 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
399 LeaveCriticalSection( &pdh_handle_cs
);
400 return PDH_INVALID_HANDLE
;
405 HANDLE thread
= query
->thread
;
406 SetEvent( query
->stop
);
407 LeaveCriticalSection( &pdh_handle_cs
);
409 WaitForSingleObject( thread
, INFINITE
);
411 EnterCriticalSection( &pdh_handle_cs
);
412 if (query
->magic
!= PDH_MAGIC_QUERY
)
414 LeaveCriticalSection( &pdh_handle_cs
);
415 return ERROR_SUCCESS
;
417 CloseHandle( query
->stop
);
418 CloseHandle( query
->thread
);
419 query
->thread
= NULL
;
422 LIST_FOR_EACH_SAFE( item
, next
, &query
->counters
)
424 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
426 list_remove( &counter
->entry
);
427 destroy_counter( counter
);
430 destroy_query( query
);
432 LeaveCriticalSection( &pdh_handle_cs
);
433 return ERROR_SUCCESS
;
436 /* caller must hold query lock */
437 static void collect_query_data( struct query
*query
)
441 LIST_FOR_EACH( item
, &query
->counters
)
444 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
446 counter
->collect( counter
);
448 GetLocalTime( &time
);
449 SystemTimeToFileTime( &time
, &counter
->stamp
);
453 /***********************************************************************
454 * PdhCollectQueryData (PDH.@)
456 PDH_STATUS WINAPI
PdhCollectQueryData( PDH_HQUERY handle
)
458 struct query
*query
= handle
;
460 TRACE("%p\n", handle
);
462 EnterCriticalSection( &pdh_handle_cs
);
463 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
465 LeaveCriticalSection( &pdh_handle_cs
);
466 return PDH_INVALID_HANDLE
;
469 if (list_empty( &query
->counters
))
471 LeaveCriticalSection( &pdh_handle_cs
);
475 collect_query_data( query
);
477 LeaveCriticalSection( &pdh_handle_cs
);
478 return ERROR_SUCCESS
;
481 static DWORD CALLBACK
collect_query_thread( void *arg
)
483 struct query
*query
= arg
;
484 DWORD interval
= query
->interval
;
485 HANDLE stop
= query
->stop
;
489 if (WaitForSingleObject( stop
, interval
) != WAIT_TIMEOUT
) ExitThread( 0 );
491 EnterCriticalSection( &pdh_handle_cs
);
492 if (query
->magic
!= PDH_MAGIC_QUERY
)
494 LeaveCriticalSection( &pdh_handle_cs
);
495 ExitThread( PDH_INVALID_HANDLE
);
498 collect_query_data( query
);
500 if (!SetEvent( query
->wait
))
502 LeaveCriticalSection( &pdh_handle_cs
);
505 LeaveCriticalSection( &pdh_handle_cs
);
509 /***********************************************************************
510 * PdhCollectQueryDataEx (PDH.@)
512 PDH_STATUS WINAPI
PdhCollectQueryDataEx( PDH_HQUERY handle
, DWORD interval
, HANDLE event
)
515 struct query
*query
= handle
;
517 TRACE("%p %d %p\n", handle
, interval
, event
);
519 EnterCriticalSection( &pdh_handle_cs
);
520 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
522 LeaveCriticalSection( &pdh_handle_cs
);
523 return PDH_INVALID_HANDLE
;
525 if (list_empty( &query
->counters
))
527 LeaveCriticalSection( &pdh_handle_cs
);
532 HANDLE thread
= query
->thread
;
533 SetEvent( query
->stop
);
534 LeaveCriticalSection( &pdh_handle_cs
);
536 WaitForSingleObject( thread
, INFINITE
);
538 EnterCriticalSection( &pdh_handle_cs
);
539 if (query
->magic
!= PDH_MAGIC_QUERY
)
541 LeaveCriticalSection( &pdh_handle_cs
);
542 return PDH_INVALID_HANDLE
;
544 CloseHandle( query
->thread
);
545 query
->thread
= NULL
;
547 else if (!(query
->stop
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
549 ret
= GetLastError();
550 LeaveCriticalSection( &pdh_handle_cs
);
554 query
->interval
= interval
* 1000;
555 if (!(query
->thread
= CreateThread( NULL
, 0, collect_query_thread
, query
, 0, NULL
)))
557 ret
= GetLastError();
558 CloseHandle( query
->stop
);
560 LeaveCriticalSection( &pdh_handle_cs
);
564 LeaveCriticalSection( &pdh_handle_cs
);
565 return ERROR_SUCCESS
;
568 /***********************************************************************
569 * PdhCollectQueryDataWithTime (PDH.@)
571 PDH_STATUS WINAPI
PdhCollectQueryDataWithTime( PDH_HQUERY handle
, LONGLONG
*timestamp
)
573 struct query
*query
= handle
;
574 struct counter
*counter
;
577 TRACE("%p %p\n", handle
, timestamp
);
579 if (!timestamp
) return PDH_INVALID_ARGUMENT
;
581 EnterCriticalSection( &pdh_handle_cs
);
582 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
584 LeaveCriticalSection( &pdh_handle_cs
);
585 return PDH_INVALID_HANDLE
;
587 if (list_empty( &query
->counters
))
589 LeaveCriticalSection( &pdh_handle_cs
);
593 collect_query_data( query
);
595 item
= list_head( &query
->counters
);
596 counter
= LIST_ENTRY( item
, struct counter
, entry
);
598 *timestamp
= ((LONGLONG
)counter
->stamp
.dwHighDateTime
<< 32) | counter
->stamp
.dwLowDateTime
;
600 LeaveCriticalSection( &pdh_handle_cs
);
601 return ERROR_SUCCESS
;
604 /***********************************************************************
605 * PdhExpandWildCardPathA (PDH.@)
607 PDH_STATUS WINAPI
PdhExpandWildCardPathA( LPCSTR szDataSource
, LPCSTR szWildCardPath
, LPSTR mszExpandedPathList
, LPDWORD pcchPathListLength
, DWORD dwFlags
)
609 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource
), debugstr_a(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
, dwFlags
);
610 return PDH_NOT_IMPLEMENTED
;
613 /***********************************************************************
614 * PdhExpandWildCardPathW (PDH.@)
616 PDH_STATUS WINAPI
PdhExpandWildCardPathW( LPCWSTR szDataSource
, LPCWSTR szWildCardPath
, LPWSTR mszExpandedPathList
, LPDWORD pcchPathListLength
, DWORD dwFlags
)
618 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource
), debugstr_w(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
, dwFlags
);
619 return PDH_NOT_IMPLEMENTED
;
622 /***********************************************************************
623 * PdhGetCounterInfoA (PDH.@)
625 PDH_STATUS WINAPI
PdhGetCounterInfoA( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_A info
)
627 struct counter
*counter
= handle
;
629 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
631 EnterCriticalSection( &pdh_handle_cs
);
632 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
634 LeaveCriticalSection( &pdh_handle_cs
);
635 return PDH_INVALID_HANDLE
;
639 LeaveCriticalSection( &pdh_handle_cs
);
640 return PDH_INVALID_ARGUMENT
;
642 if (*size
< sizeof(PDH_COUNTER_INFO_A
))
644 *size
= sizeof(PDH_COUNTER_INFO_A
);
645 LeaveCriticalSection( &pdh_handle_cs
);
646 return PDH_MORE_DATA
;
649 memset( info
, 0, sizeof(PDH_COUNTER_INFO_A
) );
651 info
->dwType
= counter
->type
;
652 info
->CStatus
= counter
->status
;
653 info
->lScale
= counter
->scale
;
654 info
->lDefaultScale
= counter
->defaultscale
;
655 info
->dwUserData
= counter
->user
;
656 info
->dwQueryUserData
= counter
->queryuser
;
658 *size
= sizeof(PDH_COUNTER_INFO_A
);
660 LeaveCriticalSection( &pdh_handle_cs
);
661 return ERROR_SUCCESS
;
664 /***********************************************************************
665 * PdhGetCounterInfoW (PDH.@)
667 PDH_STATUS WINAPI
PdhGetCounterInfoW( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_W info
)
669 struct counter
*counter
= handle
;
671 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
673 EnterCriticalSection( &pdh_handle_cs
);
674 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
676 LeaveCriticalSection( &pdh_handle_cs
);
677 return PDH_INVALID_HANDLE
;
681 LeaveCriticalSection( &pdh_handle_cs
);
682 return PDH_INVALID_ARGUMENT
;
684 if (*size
< sizeof(PDH_COUNTER_INFO_W
))
686 *size
= sizeof(PDH_COUNTER_INFO_W
);
687 LeaveCriticalSection( &pdh_handle_cs
);
688 return PDH_MORE_DATA
;
691 memset( info
, 0, sizeof(PDH_COUNTER_INFO_W
) );
693 info
->dwType
= counter
->type
;
694 info
->CStatus
= counter
->status
;
695 info
->lScale
= counter
->scale
;
696 info
->lDefaultScale
= counter
->defaultscale
;
697 info
->dwUserData
= counter
->user
;
698 info
->dwQueryUserData
= counter
->queryuser
;
700 *size
= sizeof(PDH_COUNTER_INFO_W
);
702 LeaveCriticalSection( &pdh_handle_cs
);
703 return ERROR_SUCCESS
;
706 /***********************************************************************
707 * PdhGetCounterTimeBase (PDH.@)
709 PDH_STATUS WINAPI
PdhGetCounterTimeBase( PDH_HCOUNTER handle
, LONGLONG
*base
)
711 struct counter
*counter
= handle
;
713 TRACE("%p %p\n", handle
, base
);
715 if (!base
) return PDH_INVALID_ARGUMENT
;
717 EnterCriticalSection( &pdh_handle_cs
);
718 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
720 LeaveCriticalSection( &pdh_handle_cs
);
721 return PDH_INVALID_HANDLE
;
724 *base
= counter
->base
;
726 LeaveCriticalSection( &pdh_handle_cs
);
727 return ERROR_SUCCESS
;
730 /***********************************************************************
731 * PdhGetDllVersion (PDH.@)
733 PDH_STATUS WINAPI
PdhGetDllVersion( LPDWORD version
)
736 return PDH_INVALID_ARGUMENT
;
738 *version
= PDH_VERSION
;
740 return ERROR_SUCCESS
;
743 /***********************************************************************
744 * PdhGetFormattedCounterValue (PDH.@)
746 PDH_STATUS WINAPI
PdhGetFormattedCounterValue( PDH_HCOUNTER handle
, DWORD format
,
747 LPDWORD type
, PPDH_FMT_COUNTERVALUE value
)
750 struct counter
*counter
= handle
;
752 TRACE("%p %x %p %p\n", handle
, format
, type
, value
);
754 if (!value
) return PDH_INVALID_ARGUMENT
;
756 EnterCriticalSection( &pdh_handle_cs
);
757 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
759 LeaveCriticalSection( &pdh_handle_cs
);
760 return PDH_INVALID_HANDLE
;
764 LeaveCriticalSection( &pdh_handle_cs
);
765 return PDH_INVALID_DATA
;
767 if (!(ret
= format_value( counter
, format
, &counter
->one
, &counter
->two
, value
)))
769 value
->CStatus
= ERROR_SUCCESS
;
770 if (type
) *type
= counter
->type
;
773 LeaveCriticalSection( &pdh_handle_cs
);
777 /***********************************************************************
778 * PdhGetRawCounterValue (PDH.@)
780 PDH_STATUS WINAPI
PdhGetRawCounterValue( PDH_HCOUNTER handle
, LPDWORD type
,
781 PPDH_RAW_COUNTER value
)
783 struct counter
*counter
= handle
;
785 TRACE("%p %p %p\n", handle
, type
, value
);
787 if (!value
) return PDH_INVALID_ARGUMENT
;
789 EnterCriticalSection( &pdh_handle_cs
);
790 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
792 LeaveCriticalSection( &pdh_handle_cs
);
793 return PDH_INVALID_HANDLE
;
796 value
->CStatus
= counter
->status
;
797 value
->TimeStamp
.dwLowDateTime
= counter
->stamp
.dwLowDateTime
;
798 value
->TimeStamp
.dwHighDateTime
= counter
->stamp
.dwHighDateTime
;
799 value
->FirstValue
= counter
->one
.largevalue
;
800 value
->SecondValue
= counter
->two
.largevalue
;
801 value
->MultiCount
= 1; /* FIXME */
803 if (type
) *type
= counter
->type
;
805 LeaveCriticalSection( &pdh_handle_cs
);
806 return ERROR_SUCCESS
;
809 /***********************************************************************
810 * PdhLookupPerfIndexByNameA (PDH.@)
812 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameA( LPCSTR machine
, LPCSTR name
, LPDWORD index
)
815 WCHAR
*machineW
= NULL
;
818 TRACE("%s %s %p\n", debugstr_a(machine
), debugstr_a(name
), index
);
820 if (!name
) return PDH_INVALID_ARGUMENT
;
822 if (machine
&& !(machineW
= pdh_strdup_aw( machine
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
824 if (!(nameW
= pdh_strdup_aw( name
)))
825 return PDH_MEMORY_ALLOCATION_FAILURE
;
827 ret
= PdhLookupPerfIndexByNameW( machineW
, nameW
, index
);
830 heap_free( machineW
);
834 /***********************************************************************
835 * PdhLookupPerfIndexByNameW (PDH.@)
837 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameW( LPCWSTR machine
, LPCWSTR name
, LPDWORD index
)
841 TRACE("%s %s %p\n", debugstr_w(machine
), debugstr_w(name
), index
);
843 if (!name
|| !index
) return PDH_INVALID_ARGUMENT
;
847 FIXME("remote machine not supported\n");
848 return PDH_CSTATUS_NO_MACHINE
;
850 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
852 if (pdh_match_path( counter_sources
[i
].path
, name
))
854 *index
= counter_sources
[i
].index
;
855 return ERROR_SUCCESS
;
858 return PDH_STRING_NOT_FOUND
;
861 /***********************************************************************
862 * PdhLookupPerfNameByIndexA (PDH.@)
864 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexA( LPCSTR machine
, DWORD index
, LPSTR buffer
, LPDWORD size
)
867 WCHAR
*machineW
= NULL
;
868 WCHAR bufferW
[PDH_MAX_COUNTER_NAME
];
869 DWORD sizeW
= sizeof(bufferW
) / sizeof(WCHAR
);
871 TRACE("%s %d %p %p\n", debugstr_a(machine
), index
, buffer
, size
);
873 if (!buffer
|| !size
) return PDH_INVALID_ARGUMENT
;
875 if (machine
&& !(machineW
= pdh_strdup_aw( machine
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
877 if (!(ret
= PdhLookupPerfNameByIndexW( machineW
, index
, bufferW
, &sizeW
)))
879 int required
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
881 if (size
&& *size
< required
) ret
= PDH_MORE_DATA
;
882 else WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, required
, NULL
, NULL
);
883 if (size
) *size
= required
;
885 heap_free( machineW
);
889 /***********************************************************************
890 * PdhLookupPerfNameByIndexW (PDH.@)
892 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexW( LPCWSTR machine
, DWORD index
, LPWSTR buffer
, LPDWORD size
)
897 TRACE("%s %d %p %p\n", debugstr_w(machine
), index
, buffer
, size
);
901 FIXME("remote machine not supported\n");
902 return PDH_CSTATUS_NO_MACHINE
;
905 if (!buffer
|| !size
) return PDH_INVALID_ARGUMENT
;
906 if (!index
) return ERROR_SUCCESS
;
908 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
910 if (counter_sources
[i
].index
== index
)
912 WCHAR
*p
= strrchrW( counter_sources
[i
].path
, '\\' ) + 1;
913 unsigned int required
= strlenW( p
) + 1;
915 if (*size
< required
) ret
= PDH_MORE_DATA
;
918 strcpyW( buffer
, p
);
925 return PDH_INVALID_ARGUMENT
;
928 /***********************************************************************
929 * PdhOpenQueryA (PDH.@)
931 PDH_STATUS WINAPI
PdhOpenQueryA( LPCSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*query
)
934 WCHAR
*sourceW
= NULL
;
936 TRACE("%s %lx %p\n", debugstr_a(source
), userdata
, query
);
938 if (source
&& !(sourceW
= pdh_strdup_aw( source
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
940 ret
= PdhOpenQueryW( sourceW
, userdata
, query
);
941 heap_free( sourceW
);
946 /***********************************************************************
947 * PdhOpenQueryW (PDH.@)
949 PDH_STATUS WINAPI
PdhOpenQueryW( LPCWSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*handle
)
953 TRACE("%s %lx %p\n", debugstr_w(source
), userdata
, handle
);
955 if (!handle
) return PDH_INVALID_ARGUMENT
;
959 FIXME("log file data source not supported\n");
960 return PDH_INVALID_ARGUMENT
;
962 if ((query
= create_query()))
964 query
->user
= userdata
;
967 return ERROR_SUCCESS
;
969 return PDH_MEMORY_ALLOCATION_FAILURE
;
972 /***********************************************************************
973 * PdhRemoveCounter (PDH.@)
975 PDH_STATUS WINAPI
PdhRemoveCounter( PDH_HCOUNTER handle
)
977 struct counter
*counter
= handle
;
979 TRACE("%p\n", handle
);
981 EnterCriticalSection( &pdh_handle_cs
);
982 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
984 LeaveCriticalSection( &pdh_handle_cs
);
985 return PDH_INVALID_HANDLE
;
988 list_remove( &counter
->entry
);
989 destroy_counter( counter
);
991 LeaveCriticalSection( &pdh_handle_cs
);
992 return ERROR_SUCCESS
;
995 /***********************************************************************
996 * PdhSetCounterScaleFactor (PDH.@)
998 PDH_STATUS WINAPI
PdhSetCounterScaleFactor( PDH_HCOUNTER handle
, LONG factor
)
1000 struct counter
*counter
= handle
;
1002 TRACE("%p\n", handle
);
1004 EnterCriticalSection( &pdh_handle_cs
);
1005 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
1007 LeaveCriticalSection( &pdh_handle_cs
);
1008 return PDH_INVALID_HANDLE
;
1010 if (factor
< PDH_MIN_SCALE
|| factor
> PDH_MAX_SCALE
)
1012 LeaveCriticalSection( &pdh_handle_cs
);
1013 return PDH_INVALID_ARGUMENT
;
1016 counter
->scale
= factor
;
1018 LeaveCriticalSection( &pdh_handle_cs
);
1019 return ERROR_SUCCESS
;
1022 /***********************************************************************
1023 * PdhValidatePathA (PDH.@)
1025 PDH_STATUS WINAPI
PdhValidatePathA( LPCSTR path
)
1030 TRACE("%s\n", debugstr_a(path
));
1032 if (!path
) return PDH_INVALID_ARGUMENT
;
1033 if (!(pathW
= pdh_strdup_aw( path
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
1035 ret
= PdhValidatePathW( pathW
);
1041 static PDH_STATUS
validate_path( LPCWSTR path
)
1043 if (!path
|| !*path
) return PDH_INVALID_ARGUMENT
;
1044 if (*path
++ != '\\' || !strchrW( path
, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME
;
1045 return ERROR_SUCCESS
;
1048 /***********************************************************************
1049 * PdhValidatePathW (PDH.@)
1051 PDH_STATUS WINAPI
PdhValidatePathW( LPCWSTR path
)
1056 TRACE("%s\n", debugstr_w(path
));
1058 if ((ret
= validate_path( path
))) return ret
;
1060 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
1061 if (pdh_match_path( counter_sources
[i
].path
, path
)) return ERROR_SUCCESS
;
1063 return PDH_CSTATUS_NO_COUNTER
;
1066 /***********************************************************************
1067 * PdhValidatePathExA (PDH.@)
1069 PDH_STATUS WINAPI
PdhValidatePathExA( PDH_HLOG source
, LPCSTR path
)
1071 TRACE("%p %s\n", source
, debugstr_a(path
));
1075 FIXME("log file data source not supported\n");
1076 return ERROR_SUCCESS
;
1078 return PdhValidatePathA( path
);
1081 /***********************************************************************
1082 * PdhValidatePathExW (PDH.@)
1084 PDH_STATUS WINAPI
PdhValidatePathExW( PDH_HLOG source
, LPCWSTR path
)
1086 TRACE("%p %s\n", source
, debugstr_w(path
));
1090 FIXME("log file data source not supported\n");
1091 return ERROR_SUCCESS
;
1093 return PdhValidatePathW( path
);
1096 /***********************************************************************
1097 * PdhMakeCounterPathA (PDH.@)
1099 PDH_STATUS WINAPI
PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A
*e
, LPSTR buffer
,
1100 LPDWORD buflen
, DWORD flags
)
1102 PDH_STATUS ret
= PDH_MEMORY_ALLOCATION_FAILURE
;
1103 PDH_COUNTER_PATH_ELEMENTS_W eW
;
1107 TRACE("%p %p %p 0x%08x\n", e
, buffer
, buflen
, flags
);
1109 if (!e
|| !buflen
) return PDH_INVALID_ARGUMENT
;
1111 memset( &eW
, 0, sizeof(eW
) );
1112 if (e
->szMachineName
&& !(eW
.szMachineName
= pdh_strdup_aw( e
->szMachineName
))) goto done
;
1113 if (e
->szObjectName
&& !(eW
.szObjectName
= pdh_strdup_aw( e
->szObjectName
))) goto done
;
1114 if (e
->szInstanceName
&& !(eW
.szInstanceName
= pdh_strdup_aw( e
->szInstanceName
))) goto done
;
1115 if (e
->szParentInstance
&& !(eW
.szParentInstance
= pdh_strdup_aw( e
->szParentInstance
))) goto done
;
1116 if (e
->szCounterName
&& !(eW
.szCounterName
= pdh_strdup_aw( e
->szCounterName
))) goto done
;
1117 eW
.dwInstanceIndex
= e
->dwInstanceIndex
;
1120 ret
= PdhMakeCounterPathW( &eW
, NULL
, &buflenW
, flags
);
1121 if (ret
== PDH_MORE_DATA
)
1123 if ((bufferW
= heap_alloc( buflenW
* sizeof(WCHAR
) )))
1125 if (!(ret
= PdhMakeCounterPathW( &eW
, bufferW
, &buflenW
, flags
)))
1127 int len
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
1128 if (*buflen
>= len
) WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, *buflen
, NULL
, NULL
);
1129 else ret
= PDH_MORE_DATA
;
1132 heap_free( bufferW
);
1135 ret
= PDH_MEMORY_ALLOCATION_FAILURE
;
1139 heap_free( eW
.szMachineName
);
1140 heap_free( eW
.szObjectName
);
1141 heap_free( eW
.szInstanceName
);
1142 heap_free( eW
.szParentInstance
);
1143 heap_free( eW
.szCounterName
);
1147 /***********************************************************************
1148 * PdhMakeCounterPathW (PDH.@)
1150 PDH_STATUS WINAPI
PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W
*e
, LPWSTR buffer
,
1151 LPDWORD buflen
, DWORD flags
)
1153 static const WCHAR bslash
[] = {'\\',0};
1154 static const WCHAR fslash
[] = {'/',0};
1155 static const WCHAR lparen
[] = {'(',0};
1156 static const WCHAR rparen
[] = {')',0};
1157 static const WCHAR fmt
[] = {'#','%','u',0};
1159 WCHAR path
[PDH_MAX_COUNTER_NAME
], instance
[12];
1160 PDH_STATUS ret
= ERROR_SUCCESS
;
1163 TRACE("%p %p %p 0x%08x\n", e
, buffer
, buflen
, flags
);
1165 if (flags
) FIXME("unimplemented flags 0x%08x\n", flags
);
1167 if (!e
|| !e
->szCounterName
|| !e
->szObjectName
|| !buflen
)
1168 return PDH_INVALID_ARGUMENT
;
1171 if (e
->szMachineName
)
1173 strcatW(path
, bslash
);
1174 strcatW(path
, bslash
);
1175 strcatW(path
, e
->szMachineName
);
1177 strcatW(path
, bslash
);
1178 strcatW(path
, e
->szObjectName
);
1179 if (e
->szInstanceName
)
1181 strcatW(path
, lparen
);
1182 if (e
->szParentInstance
)
1184 strcatW(path
, e
->szParentInstance
);
1185 strcatW(path
, fslash
);
1187 strcatW(path
, e
->szInstanceName
);
1188 sprintfW(instance
, fmt
, e
->dwInstanceIndex
);
1189 strcatW(path
, instance
);
1190 strcatW(path
, rparen
);
1192 strcatW(path
, bslash
);
1193 strcatW(path
, e
->szCounterName
);
1195 len
= strlenW(path
) + 1;
1196 if (*buflen
>= len
) strcpyW(buffer
, path
);
1197 else ret
= PDH_MORE_DATA
;
1202 /***********************************************************************
1203 * PdhEnumObjectItemsA (PDH.@)
1205 PDH_STATUS WINAPI
PdhEnumObjectItemsA(LPCSTR szDataSource
, LPCSTR szMachineName
, LPCSTR szObjectName
,
1206 LPSTR mszCounterList
, LPDWORD pcchCounterListLength
, LPSTR mszInstanceList
,
1207 LPDWORD pcchInstanceListLength
, DWORD dwDetailLevel
, DWORD dwFlags
)
1209 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource
), debugstr_a(szMachineName
),
1210 debugstr_a(szObjectName
), mszCounterList
, pcchCounterListLength
, mszInstanceList
,
1211 pcchInstanceListLength
, dwDetailLevel
, dwFlags
);
1213 return PDH_NOT_IMPLEMENTED
;
1216 /***********************************************************************
1217 * PdhEnumObjectItemsW (PDH.@)
1219 PDH_STATUS WINAPI
PdhEnumObjectItemsW(LPCWSTR szDataSource
, LPCWSTR szMachineName
, LPCWSTR szObjectName
,
1220 LPWSTR mszCounterList
, LPDWORD pcchCounterListLength
, LPWSTR mszInstanceList
,
1221 LPDWORD pcchInstanceListLength
, DWORD dwDetailLevel
, DWORD dwFlags
)
1223 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource
), debugstr_w(szMachineName
),
1224 debugstr_w(szObjectName
), mszCounterList
, pcchCounterListLength
, mszInstanceList
,
1225 pcchInstanceListLength
, dwDetailLevel
, dwFlags
);
1227 return PDH_NOT_IMPLEMENTED
;
1230 /***********************************************************************
1231 * PdhSetDefaultRealTimeDataSource (PDH.@)
1233 PDH_STATUS WINAPI
PdhSetDefaultRealTimeDataSource( DWORD source
)
1235 FIXME("%u\n", source
);
1236 return ERROR_SUCCESS
;