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
);
92 case DLL_WINE_PREATTACH
:
93 return FALSE
; /* prefer native version */
94 case DLL_PROCESS_ATTACH
:
95 DisableThreadLibraryCalls(hinstDLL
);
97 case DLL_PROCESS_DETACH
:
98 if (lpvReserved
) break;
99 DeleteCriticalSection(&pdh_handle_cs
);
115 DWORD magic
; /* signature */
116 struct list entry
; /* list entry */
117 WCHAR
*path
; /* identifier */
118 DWORD type
; /* counter type */
119 DWORD status
; /* update status */
120 LONG scale
; /* scale factor */
121 LONG defaultscale
; /* default scale factor */
122 DWORD_PTR user
; /* user data */
123 DWORD_PTR queryuser
; /* query user data */
124 LONGLONG base
; /* samples per second */
125 FILETIME stamp
; /* time stamp */
126 void (CALLBACK
*collect
)( struct counter
* ); /* collect callback */
127 union value one
; /* first value */
128 union value two
; /* second value */
131 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
133 static struct counter
*create_counter( void )
135 struct counter
*counter
;
137 if ((counter
= heap_alloc_zero( sizeof(struct counter
) )))
139 counter
->magic
= PDH_MAGIC_COUNTER
;
145 static void destroy_counter( struct counter
*counter
)
148 heap_free( counter
->path
);
149 heap_free( counter
);
152 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
156 DWORD magic
; /* signature */
157 DWORD_PTR user
; /* user data */
158 HANDLE thread
; /* collect thread */
159 DWORD interval
; /* collect interval */
160 HANDLE wait
; /* wait event */
161 HANDLE stop
; /* stop event */
162 struct list counters
; /* counter list */
165 static struct query
*create_query( void )
169 if ((query
= heap_alloc_zero( sizeof(struct query
) )))
171 query
->magic
= PDH_MAGIC_QUERY
;
172 list_init( &query
->counters
);
178 static void destroy_query( struct query
*query
)
186 DWORD index
; /* name index */
187 const WCHAR
*path
; /* identifier */
188 void (CALLBACK
*collect
)( struct counter
* ); /* collect callback */
189 DWORD type
; /* counter type */
190 LONG scale
; /* default scale factor */
191 LONGLONG base
; /* samples per second */
194 static const WCHAR path_processor_time
[] =
195 {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
196 '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
197 static const WCHAR path_uptime
[] =
198 {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
200 static void CALLBACK
collect_processor_time( struct counter
*counter
)
202 counter
->two
.largevalue
= 500000; /* FIXME */
203 counter
->status
= PDH_CSTATUS_VALID_DATA
;
206 static void CALLBACK
collect_uptime( struct counter
*counter
)
208 counter
->two
.largevalue
= GetTickCount64();
209 counter
->status
= PDH_CSTATUS_VALID_DATA
;
212 #define TYPE_PROCESSOR_TIME \
213 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
214 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
216 #define TYPE_UPTIME \
217 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
219 /* counter source registry */
220 static const struct source counter_sources
[] =
222 { 6, path_processor_time
, collect_processor_time
, TYPE_PROCESSOR_TIME
, -5, 10000000 },
223 { 674, path_uptime
, collect_uptime
, TYPE_UPTIME
, -3, 1000 }
226 static BOOL
is_local_machine( const WCHAR
*name
, DWORD len
)
228 WCHAR buf
[MAX_COMPUTERNAME_LENGTH
+ 1];
229 DWORD buflen
= sizeof(buf
) / sizeof(buf
[0]);
231 if (!GetComputerNameW( buf
, &buflen
)) return FALSE
;
232 return len
== buflen
&& !memicmpW( name
, buf
, buflen
);
235 static BOOL
pdh_match_path( LPCWSTR fullpath
, LPCWSTR path
)
239 if (path
[0] == '\\' && path
[1] == '\\' && (p
= strchrW( path
+ 2, '\\' )) &&
240 is_local_machine( path
+ 2, p
- path
- 2 ))
244 if (strchrW( path
, '\\' )) p
= fullpath
;
245 else p
= strrchrW( fullpath
, '\\' ) + 1;
246 return !strcmpW( p
, path
);
249 /***********************************************************************
250 * PdhAddCounterA (PDH.@)
252 PDH_STATUS WINAPI
PdhAddCounterA( PDH_HQUERY query
, LPCSTR path
,
253 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
258 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
260 if (!path
) return PDH_INVALID_ARGUMENT
;
262 if (!(pathW
= pdh_strdup_aw( path
)))
263 return PDH_MEMORY_ALLOCATION_FAILURE
;
265 ret
= PdhAddCounterW( query
, pathW
, userdata
, counter
);
271 /***********************************************************************
272 * PdhAddCounterW (PDH.@)
274 PDH_STATUS WINAPI
PdhAddCounterW( PDH_HQUERY hquery
, LPCWSTR path
,
275 DWORD_PTR userdata
, PDH_HCOUNTER
*hcounter
)
277 struct query
*query
= hquery
;
278 struct counter
*counter
;
281 TRACE("%p %s %lx %p\n", hquery
, debugstr_w(path
), userdata
, hcounter
);
283 if (!path
|| !hcounter
) return PDH_INVALID_ARGUMENT
;
285 EnterCriticalSection( &pdh_handle_cs
);
286 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
288 LeaveCriticalSection( &pdh_handle_cs
);
289 return PDH_INVALID_HANDLE
;
293 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
295 if (pdh_match_path( counter_sources
[i
].path
, path
))
297 if ((counter
= create_counter()))
299 counter
->path
= pdh_strdup( counter_sources
[i
].path
);
300 counter
->collect
= counter_sources
[i
].collect
;
301 counter
->type
= counter_sources
[i
].type
;
302 counter
->defaultscale
= counter_sources
[i
].scale
;
303 counter
->base
= counter_sources
[i
].base
;
304 counter
->queryuser
= query
->user
;
305 counter
->user
= userdata
;
307 list_add_tail( &query
->counters
, &counter
->entry
);
310 LeaveCriticalSection( &pdh_handle_cs
);
311 return ERROR_SUCCESS
;
313 LeaveCriticalSection( &pdh_handle_cs
);
314 return PDH_MEMORY_ALLOCATION_FAILURE
;
317 LeaveCriticalSection( &pdh_handle_cs
);
318 return PDH_CSTATUS_NO_COUNTER
;
321 /***********************************************************************
322 * PdhAddEnglishCounterA (PDH.@)
324 PDH_STATUS WINAPI
PdhAddEnglishCounterA( PDH_HQUERY query
, LPCSTR path
,
325 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
327 TRACE("%p %s %lx %p\n", query
, debugstr_a(path
), userdata
, counter
);
329 if (!query
) return PDH_INVALID_ARGUMENT
;
330 return PdhAddCounterA( query
, path
, userdata
, counter
);
333 /***********************************************************************
334 * PdhAddEnglishCounterW (PDH.@)
336 PDH_STATUS WINAPI
PdhAddEnglishCounterW( PDH_HQUERY query
, LPCWSTR path
,
337 DWORD_PTR userdata
, PDH_HCOUNTER
*counter
)
339 TRACE("%p %s %lx %p\n", query
, debugstr_w(path
), userdata
, counter
);
341 if (!query
) return PDH_INVALID_ARGUMENT
;
342 return PdhAddCounterW( query
, path
, userdata
, counter
);
345 /* caller must hold counter lock */
346 static PDH_STATUS
format_value( struct counter
*counter
, DWORD format
, union value
*raw1
,
347 union value
*raw2
, PDH_FMT_COUNTERVALUE
*value
)
351 factor
= counter
->scale
? counter
->scale
: counter
->defaultscale
;
352 if (format
& PDH_FMT_LONG
)
354 if (format
& PDH_FMT_1000
) value
->u
.longValue
= raw2
->longvalue
* 1000;
355 else value
->u
.longValue
= raw2
->longvalue
* pow( 10, factor
);
357 else if (format
& PDH_FMT_LARGE
)
359 if (format
& PDH_FMT_1000
) value
->u
.largeValue
= raw2
->largevalue
* 1000;
360 else value
->u
.largeValue
= raw2
->largevalue
* pow( 10, factor
);
362 else if (format
& PDH_FMT_DOUBLE
)
364 if (format
& PDH_FMT_1000
) value
->u
.doubleValue
= raw2
->doublevalue
* 1000;
365 else value
->u
.doubleValue
= raw2
->doublevalue
* pow( 10, factor
);
369 WARN("unknown format %x\n", format
);
370 return PDH_INVALID_ARGUMENT
;
372 return ERROR_SUCCESS
;
375 /***********************************************************************
376 * PdhCalculateCounterFromRawValue (PDH.@)
378 PDH_STATUS WINAPI
PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle
, DWORD format
,
379 PPDH_RAW_COUNTER raw1
, PPDH_RAW_COUNTER raw2
,
380 PPDH_FMT_COUNTERVALUE value
)
383 struct counter
*counter
= handle
;
385 TRACE("%p 0x%08x %p %p %p\n", handle
, format
, raw1
, raw2
, value
);
387 if (!value
) return PDH_INVALID_ARGUMENT
;
389 EnterCriticalSection( &pdh_handle_cs
);
390 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
392 LeaveCriticalSection( &pdh_handle_cs
);
393 return PDH_INVALID_HANDLE
;
396 ret
= format_value( counter
, format
, (union value
*)&raw1
->SecondValue
,
397 (union value
*)&raw2
->SecondValue
, value
);
399 LeaveCriticalSection( &pdh_handle_cs
);
404 /***********************************************************************
405 * PdhCloseQuery (PDH.@)
407 PDH_STATUS WINAPI
PdhCloseQuery( PDH_HQUERY handle
)
409 struct query
*query
= handle
;
410 struct list
*item
, *next
;
412 TRACE("%p\n", handle
);
414 EnterCriticalSection( &pdh_handle_cs
);
415 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
417 LeaveCriticalSection( &pdh_handle_cs
);
418 return PDH_INVALID_HANDLE
;
423 HANDLE thread
= query
->thread
;
424 SetEvent( query
->stop
);
425 LeaveCriticalSection( &pdh_handle_cs
);
427 WaitForSingleObject( thread
, INFINITE
);
429 EnterCriticalSection( &pdh_handle_cs
);
430 if (query
->magic
!= PDH_MAGIC_QUERY
)
432 LeaveCriticalSection( &pdh_handle_cs
);
433 return ERROR_SUCCESS
;
435 CloseHandle( query
->stop
);
436 CloseHandle( query
->thread
);
437 query
->thread
= NULL
;
440 LIST_FOR_EACH_SAFE( item
, next
, &query
->counters
)
442 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
444 list_remove( &counter
->entry
);
445 destroy_counter( counter
);
448 destroy_query( query
);
450 LeaveCriticalSection( &pdh_handle_cs
);
451 return ERROR_SUCCESS
;
454 /* caller must hold query lock */
455 static void collect_query_data( struct query
*query
)
459 LIST_FOR_EACH( item
, &query
->counters
)
462 struct counter
*counter
= LIST_ENTRY( item
, struct counter
, entry
);
464 counter
->collect( counter
);
466 GetLocalTime( &time
);
467 SystemTimeToFileTime( &time
, &counter
->stamp
);
471 /***********************************************************************
472 * PdhCollectQueryData (PDH.@)
474 PDH_STATUS WINAPI
PdhCollectQueryData( PDH_HQUERY handle
)
476 struct query
*query
= handle
;
478 TRACE("%p\n", handle
);
480 EnterCriticalSection( &pdh_handle_cs
);
481 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
483 LeaveCriticalSection( &pdh_handle_cs
);
484 return PDH_INVALID_HANDLE
;
487 if (list_empty( &query
->counters
))
489 LeaveCriticalSection( &pdh_handle_cs
);
493 collect_query_data( query
);
495 LeaveCriticalSection( &pdh_handle_cs
);
496 return ERROR_SUCCESS
;
499 static DWORD CALLBACK
collect_query_thread( void *arg
)
501 struct query
*query
= arg
;
502 DWORD interval
= query
->interval
;
503 HANDLE stop
= query
->stop
;
507 if (WaitForSingleObject( stop
, interval
) != WAIT_TIMEOUT
) ExitThread( 0 );
509 EnterCriticalSection( &pdh_handle_cs
);
510 if (query
->magic
!= PDH_MAGIC_QUERY
)
512 LeaveCriticalSection( &pdh_handle_cs
);
513 ExitThread( PDH_INVALID_HANDLE
);
516 collect_query_data( query
);
518 if (!SetEvent( query
->wait
))
520 LeaveCriticalSection( &pdh_handle_cs
);
523 LeaveCriticalSection( &pdh_handle_cs
);
527 /***********************************************************************
528 * PdhCollectQueryDataEx (PDH.@)
530 PDH_STATUS WINAPI
PdhCollectQueryDataEx( PDH_HQUERY handle
, DWORD interval
, HANDLE event
)
533 struct query
*query
= handle
;
535 TRACE("%p %d %p\n", handle
, interval
, event
);
537 EnterCriticalSection( &pdh_handle_cs
);
538 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
540 LeaveCriticalSection( &pdh_handle_cs
);
541 return PDH_INVALID_HANDLE
;
543 if (list_empty( &query
->counters
))
545 LeaveCriticalSection( &pdh_handle_cs
);
550 HANDLE thread
= query
->thread
;
551 SetEvent( query
->stop
);
552 LeaveCriticalSection( &pdh_handle_cs
);
554 WaitForSingleObject( thread
, INFINITE
);
556 EnterCriticalSection( &pdh_handle_cs
);
557 if (query
->magic
!= PDH_MAGIC_QUERY
)
559 LeaveCriticalSection( &pdh_handle_cs
);
560 return PDH_INVALID_HANDLE
;
562 CloseHandle( query
->thread
);
563 query
->thread
= NULL
;
565 else if (!(query
->stop
= CreateEventW( NULL
, FALSE
, FALSE
, NULL
)))
567 ret
= GetLastError();
568 LeaveCriticalSection( &pdh_handle_cs
);
572 query
->interval
= interval
* 1000;
573 if (!(query
->thread
= CreateThread( NULL
, 0, collect_query_thread
, query
, 0, NULL
)))
575 ret
= GetLastError();
576 CloseHandle( query
->stop
);
578 LeaveCriticalSection( &pdh_handle_cs
);
582 LeaveCriticalSection( &pdh_handle_cs
);
583 return ERROR_SUCCESS
;
586 /***********************************************************************
587 * PdhCollectQueryDataWithTime (PDH.@)
589 PDH_STATUS WINAPI
PdhCollectQueryDataWithTime( PDH_HQUERY handle
, LONGLONG
*timestamp
)
591 struct query
*query
= handle
;
592 struct counter
*counter
;
595 TRACE("%p %p\n", handle
, timestamp
);
597 if (!timestamp
) return PDH_INVALID_ARGUMENT
;
599 EnterCriticalSection( &pdh_handle_cs
);
600 if (!query
|| query
->magic
!= PDH_MAGIC_QUERY
)
602 LeaveCriticalSection( &pdh_handle_cs
);
603 return PDH_INVALID_HANDLE
;
605 if (list_empty( &query
->counters
))
607 LeaveCriticalSection( &pdh_handle_cs
);
611 collect_query_data( query
);
613 item
= list_head( &query
->counters
);
614 counter
= LIST_ENTRY( item
, struct counter
, entry
);
616 *timestamp
= ((LONGLONG
)counter
->stamp
.dwHighDateTime
<< 32) | counter
->stamp
.dwLowDateTime
;
618 LeaveCriticalSection( &pdh_handle_cs
);
619 return ERROR_SUCCESS
;
622 /***********************************************************************
623 * PdhExpandWildCardPathA (PDH.@)
625 PDH_STATUS WINAPI
PdhExpandWildCardPathA( LPCSTR szDataSource
, LPCSTR szWildCardPath
, LPSTR mszExpandedPathList
, LPDWORD pcchPathListLength
, DWORD dwFlags
)
627 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource
), debugstr_a(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
, dwFlags
);
628 return PDH_NOT_IMPLEMENTED
;
631 /***********************************************************************
632 * PdhExpandWildCardPathW (PDH.@)
634 PDH_STATUS WINAPI
PdhExpandWildCardPathW( LPCWSTR szDataSource
, LPCWSTR szWildCardPath
, LPWSTR mszExpandedPathList
, LPDWORD pcchPathListLength
, DWORD dwFlags
)
636 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource
), debugstr_w(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
, dwFlags
);
637 return PDH_NOT_IMPLEMENTED
;
640 /***********************************************************************
641 * PdhExpandCounterPathA (PDH.@)
643 PDH_STATUS WINAPI
PdhExpandCounterPathA( LPCSTR szWildCardPath
, LPSTR mszExpandedPathList
, LPDWORD pcchPathListLength
)
645 FIXME("%s, %p, %p: stub\n", debugstr_a(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
);
646 return PdhExpandWildCardPathA(NULL
, szWildCardPath
, mszExpandedPathList
, pcchPathListLength
, 0);
649 /***********************************************************************
650 * PdhExpandCounterPathW (PDH.@)
652 PDH_STATUS WINAPI
PdhExpandCounterPathW( LPCWSTR szWildCardPath
, LPWSTR mszExpandedPathList
, LPDWORD pcchPathListLength
)
654 FIXME("%s, %p, %p: stub\n", debugstr_w(szWildCardPath
), mszExpandedPathList
, pcchPathListLength
);
655 return PdhExpandWildCardPathW(NULL
, szWildCardPath
, mszExpandedPathList
, pcchPathListLength
, 0);
658 /***********************************************************************
659 * PdhGetCounterInfoA (PDH.@)
661 PDH_STATUS WINAPI
PdhGetCounterInfoA( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_A info
)
663 struct counter
*counter
= handle
;
665 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
667 EnterCriticalSection( &pdh_handle_cs
);
668 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
670 LeaveCriticalSection( &pdh_handle_cs
);
671 return PDH_INVALID_HANDLE
;
675 LeaveCriticalSection( &pdh_handle_cs
);
676 return PDH_INVALID_ARGUMENT
;
678 if (*size
< sizeof(PDH_COUNTER_INFO_A
))
680 *size
= sizeof(PDH_COUNTER_INFO_A
);
681 LeaveCriticalSection( &pdh_handle_cs
);
682 return PDH_MORE_DATA
;
685 memset( info
, 0, sizeof(PDH_COUNTER_INFO_A
) );
687 info
->dwType
= counter
->type
;
688 info
->CStatus
= counter
->status
;
689 info
->lScale
= counter
->scale
;
690 info
->lDefaultScale
= counter
->defaultscale
;
691 info
->dwUserData
= counter
->user
;
692 info
->dwQueryUserData
= counter
->queryuser
;
694 *size
= sizeof(PDH_COUNTER_INFO_A
);
696 LeaveCriticalSection( &pdh_handle_cs
);
697 return ERROR_SUCCESS
;
700 /***********************************************************************
701 * PdhGetCounterInfoW (PDH.@)
703 PDH_STATUS WINAPI
PdhGetCounterInfoW( PDH_HCOUNTER handle
, BOOLEAN text
, LPDWORD size
, PPDH_COUNTER_INFO_W info
)
705 struct counter
*counter
= handle
;
707 TRACE("%p %d %p %p\n", handle
, text
, size
, info
);
709 EnterCriticalSection( &pdh_handle_cs
);
710 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
712 LeaveCriticalSection( &pdh_handle_cs
);
713 return PDH_INVALID_HANDLE
;
717 LeaveCriticalSection( &pdh_handle_cs
);
718 return PDH_INVALID_ARGUMENT
;
720 if (*size
< sizeof(PDH_COUNTER_INFO_W
))
722 *size
= sizeof(PDH_COUNTER_INFO_W
);
723 LeaveCriticalSection( &pdh_handle_cs
);
724 return PDH_MORE_DATA
;
727 memset( info
, 0, sizeof(PDH_COUNTER_INFO_W
) );
729 info
->dwType
= counter
->type
;
730 info
->CStatus
= counter
->status
;
731 info
->lScale
= counter
->scale
;
732 info
->lDefaultScale
= counter
->defaultscale
;
733 info
->dwUserData
= counter
->user
;
734 info
->dwQueryUserData
= counter
->queryuser
;
736 *size
= sizeof(PDH_COUNTER_INFO_W
);
738 LeaveCriticalSection( &pdh_handle_cs
);
739 return ERROR_SUCCESS
;
742 /***********************************************************************
743 * PdhGetCounterTimeBase (PDH.@)
745 PDH_STATUS WINAPI
PdhGetCounterTimeBase( PDH_HCOUNTER handle
, LONGLONG
*base
)
747 struct counter
*counter
= handle
;
749 TRACE("%p %p\n", handle
, base
);
751 if (!base
) return PDH_INVALID_ARGUMENT
;
753 EnterCriticalSection( &pdh_handle_cs
);
754 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
756 LeaveCriticalSection( &pdh_handle_cs
);
757 return PDH_INVALID_HANDLE
;
760 *base
= counter
->base
;
762 LeaveCriticalSection( &pdh_handle_cs
);
763 return ERROR_SUCCESS
;
766 /***********************************************************************
767 * PdhGetDllVersion (PDH.@)
769 PDH_STATUS WINAPI
PdhGetDllVersion( LPDWORD version
)
772 return PDH_INVALID_ARGUMENT
;
774 *version
= PDH_VERSION
;
776 return ERROR_SUCCESS
;
779 /***********************************************************************
780 * PdhGetFormattedCounterValue (PDH.@)
782 PDH_STATUS WINAPI
PdhGetFormattedCounterValue( PDH_HCOUNTER handle
, DWORD format
,
783 LPDWORD type
, PPDH_FMT_COUNTERVALUE value
)
786 struct counter
*counter
= handle
;
788 TRACE("%p %x %p %p\n", handle
, format
, type
, value
);
790 if (!value
) return PDH_INVALID_ARGUMENT
;
792 EnterCriticalSection( &pdh_handle_cs
);
793 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
795 LeaveCriticalSection( &pdh_handle_cs
);
796 return PDH_INVALID_HANDLE
;
800 LeaveCriticalSection( &pdh_handle_cs
);
801 return PDH_INVALID_DATA
;
803 if (!(ret
= format_value( counter
, format
, &counter
->one
, &counter
->two
, value
)))
805 value
->CStatus
= ERROR_SUCCESS
;
806 if (type
) *type
= counter
->type
;
809 LeaveCriticalSection( &pdh_handle_cs
);
813 /***********************************************************************
814 * PdhGetRawCounterValue (PDH.@)
816 PDH_STATUS WINAPI
PdhGetRawCounterValue( PDH_HCOUNTER handle
, LPDWORD type
,
817 PPDH_RAW_COUNTER value
)
819 struct counter
*counter
= handle
;
821 TRACE("%p %p %p\n", handle
, type
, value
);
823 if (!value
) return PDH_INVALID_ARGUMENT
;
825 EnterCriticalSection( &pdh_handle_cs
);
826 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
828 LeaveCriticalSection( &pdh_handle_cs
);
829 return PDH_INVALID_HANDLE
;
832 value
->CStatus
= counter
->status
;
833 value
->TimeStamp
.dwLowDateTime
= counter
->stamp
.dwLowDateTime
;
834 value
->TimeStamp
.dwHighDateTime
= counter
->stamp
.dwHighDateTime
;
835 value
->FirstValue
= counter
->one
.largevalue
;
836 value
->SecondValue
= counter
->two
.largevalue
;
837 value
->MultiCount
= 1; /* FIXME */
839 if (type
) *type
= counter
->type
;
841 LeaveCriticalSection( &pdh_handle_cs
);
842 return ERROR_SUCCESS
;
845 /***********************************************************************
846 * PdhLookupPerfIndexByNameA (PDH.@)
848 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameA( LPCSTR machine
, LPCSTR name
, LPDWORD index
)
851 WCHAR
*machineW
= NULL
;
854 TRACE("%s %s %p\n", debugstr_a(machine
), debugstr_a(name
), index
);
856 if (!name
) return PDH_INVALID_ARGUMENT
;
858 if (machine
&& !(machineW
= pdh_strdup_aw( machine
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
860 if (!(nameW
= pdh_strdup_aw( name
)))
861 return PDH_MEMORY_ALLOCATION_FAILURE
;
863 ret
= PdhLookupPerfIndexByNameW( machineW
, nameW
, index
);
866 heap_free( machineW
);
870 /***********************************************************************
871 * PdhLookupPerfIndexByNameW (PDH.@)
873 PDH_STATUS WINAPI
PdhLookupPerfIndexByNameW( LPCWSTR machine
, LPCWSTR name
, LPDWORD index
)
877 TRACE("%s %s %p\n", debugstr_w(machine
), debugstr_w(name
), index
);
879 if (!name
|| !index
) return PDH_INVALID_ARGUMENT
;
883 FIXME("remote machine not supported\n");
884 return PDH_CSTATUS_NO_MACHINE
;
886 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
888 if (pdh_match_path( counter_sources
[i
].path
, name
))
890 *index
= counter_sources
[i
].index
;
891 return ERROR_SUCCESS
;
894 return PDH_STRING_NOT_FOUND
;
897 /***********************************************************************
898 * PdhLookupPerfNameByIndexA (PDH.@)
900 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexA( LPCSTR machine
, DWORD index
, LPSTR buffer
, LPDWORD size
)
903 WCHAR
*machineW
= NULL
;
904 WCHAR bufferW
[PDH_MAX_COUNTER_NAME
];
905 DWORD sizeW
= sizeof(bufferW
) / sizeof(WCHAR
);
907 TRACE("%s %d %p %p\n", debugstr_a(machine
), index
, buffer
, size
);
909 if (!buffer
|| !size
) return PDH_INVALID_ARGUMENT
;
911 if (machine
&& !(machineW
= pdh_strdup_aw( machine
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
913 if (!(ret
= PdhLookupPerfNameByIndexW( machineW
, index
, bufferW
, &sizeW
)))
915 int required
= WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
917 if (size
&& *size
< required
) ret
= PDH_MORE_DATA
;
918 else WideCharToMultiByte( CP_ACP
, 0, bufferW
, -1, buffer
, required
, NULL
, NULL
);
919 if (size
) *size
= required
;
921 heap_free( machineW
);
925 /***********************************************************************
926 * PdhLookupPerfNameByIndexW (PDH.@)
928 PDH_STATUS WINAPI
PdhLookupPerfNameByIndexW( LPCWSTR machine
, DWORD index
, LPWSTR buffer
, LPDWORD size
)
933 TRACE("%s %d %p %p\n", debugstr_w(machine
), index
, buffer
, size
);
937 FIXME("remote machine not supported\n");
938 return PDH_CSTATUS_NO_MACHINE
;
941 if (!buffer
|| !size
) return PDH_INVALID_ARGUMENT
;
942 if (!index
) return ERROR_SUCCESS
;
944 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
946 if (counter_sources
[i
].index
== index
)
948 WCHAR
*p
= strrchrW( counter_sources
[i
].path
, '\\' ) + 1;
949 unsigned int required
= strlenW( p
) + 1;
951 if (*size
< required
) ret
= PDH_MORE_DATA
;
954 strcpyW( buffer
, p
);
961 return PDH_INVALID_ARGUMENT
;
964 /***********************************************************************
965 * PdhOpenQueryA (PDH.@)
967 PDH_STATUS WINAPI
PdhOpenQueryA( LPCSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*query
)
970 WCHAR
*sourceW
= NULL
;
972 TRACE("%s %lx %p\n", debugstr_a(source
), userdata
, query
);
974 if (source
&& !(sourceW
= pdh_strdup_aw( source
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
976 ret
= PdhOpenQueryW( sourceW
, userdata
, query
);
977 heap_free( sourceW
);
982 /***********************************************************************
983 * PdhOpenQueryW (PDH.@)
985 PDH_STATUS WINAPI
PdhOpenQueryW( LPCWSTR source
, DWORD_PTR userdata
, PDH_HQUERY
*handle
)
989 TRACE("%s %lx %p\n", debugstr_w(source
), userdata
, handle
);
991 if (!handle
) return PDH_INVALID_ARGUMENT
;
995 FIXME("log file data source not supported\n");
996 return PDH_INVALID_ARGUMENT
;
998 if ((query
= create_query()))
1000 query
->user
= userdata
;
1003 return ERROR_SUCCESS
;
1005 return PDH_MEMORY_ALLOCATION_FAILURE
;
1008 /***********************************************************************
1009 * PdhRemoveCounter (PDH.@)
1011 PDH_STATUS WINAPI
PdhRemoveCounter( PDH_HCOUNTER handle
)
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
;
1024 list_remove( &counter
->entry
);
1025 destroy_counter( counter
);
1027 LeaveCriticalSection( &pdh_handle_cs
);
1028 return ERROR_SUCCESS
;
1031 /***********************************************************************
1032 * PdhSetCounterScaleFactor (PDH.@)
1034 PDH_STATUS WINAPI
PdhSetCounterScaleFactor( PDH_HCOUNTER handle
, LONG factor
)
1036 struct counter
*counter
= handle
;
1038 TRACE("%p\n", handle
);
1040 EnterCriticalSection( &pdh_handle_cs
);
1041 if (!counter
|| counter
->magic
!= PDH_MAGIC_COUNTER
)
1043 LeaveCriticalSection( &pdh_handle_cs
);
1044 return PDH_INVALID_HANDLE
;
1046 if (factor
< PDH_MIN_SCALE
|| factor
> PDH_MAX_SCALE
)
1048 LeaveCriticalSection( &pdh_handle_cs
);
1049 return PDH_INVALID_ARGUMENT
;
1052 counter
->scale
= factor
;
1054 LeaveCriticalSection( &pdh_handle_cs
);
1055 return ERROR_SUCCESS
;
1058 /***********************************************************************
1059 * PdhValidatePathA (PDH.@)
1061 PDH_STATUS WINAPI
PdhValidatePathA( LPCSTR path
)
1066 TRACE("%s\n", debugstr_a(path
));
1068 if (!path
) return PDH_INVALID_ARGUMENT
;
1069 if (!(pathW
= pdh_strdup_aw( path
))) return PDH_MEMORY_ALLOCATION_FAILURE
;
1071 ret
= PdhValidatePathW( pathW
);
1077 static PDH_STATUS
validate_path( LPCWSTR path
)
1079 if (!path
|| !*path
) return PDH_INVALID_ARGUMENT
;
1080 if (*path
++ != '\\' || !strchrW( path
, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME
;
1081 return ERROR_SUCCESS
;
1084 /***********************************************************************
1085 * PdhValidatePathW (PDH.@)
1087 PDH_STATUS WINAPI
PdhValidatePathW( LPCWSTR path
)
1092 TRACE("%s\n", debugstr_w(path
));
1094 if ((ret
= validate_path( path
))) return ret
;
1096 for (i
= 0; i
< sizeof(counter_sources
) / sizeof(counter_sources
[0]); i
++)
1097 if (pdh_match_path( counter_sources
[i
].path
, path
)) return ERROR_SUCCESS
;
1099 return PDH_CSTATUS_NO_COUNTER
;
1102 /***********************************************************************
1103 * PdhValidatePathExA (PDH.@)
1105 PDH_STATUS WINAPI
PdhValidatePathExA( PDH_HLOG source
, LPCSTR path
)
1107 TRACE("%p %s\n", source
, debugstr_a(path
));
1111 FIXME("log file data source not supported\n");
1112 return ERROR_SUCCESS
;
1114 return PdhValidatePathA( path
);
1117 /***********************************************************************
1118 * PdhValidatePathExW (PDH.@)
1120 PDH_STATUS WINAPI
PdhValidatePathExW( PDH_HLOG source
, LPCWSTR path
)
1122 TRACE("%p %s\n", source
, debugstr_w(path
));
1126 FIXME("log file data source not supported\n");
1127 return ERROR_SUCCESS
;
1129 return PdhValidatePathW( path
);
1132 /***********************************************************************
1133 * PdhMakeCounterPathA (PDH.@)
1135 PDH_STATUS WINAPI
PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A
*e
, LPSTR buffer
,
1136 LPDWORD buflen
, DWORD flags
)
1138 PDH_STATUS ret
= PDH_MEMORY_ALLOCATION_FAILURE
;
1139 PDH_COUNTER_PATH_ELEMENTS_W eW
;
1143 TRACE("%p %p %p 0x%08x\n", e
, buffer
, buflen
, flags
);
1145 if (!e
|| !buflen
) return PDH_INVALID_ARGUMENT
;
1147 memset( &eW
, 0, sizeof(eW
) );
1148 if (e
->szMachineName
&& !(eW
.szMachineName
= pdh_strdup_aw( e
->szMachineName
))) goto done
;
1149 if (e
->szObjectName
&& !(eW
.szObjectName
= pdh_strdup_aw( e
->szObjectName
))) goto done
;
1150 if (e
->szInstanceName
&& !(eW
.szInstanceName
= pdh_strdup_aw( e
->szInstanceName
))) goto done
;
1151 if (e
->szParentInstance
&& !(eW
.szParentInstance
= pdh_strdup_aw( e
->szParentInstance
))) goto done
;
1152 if (e
->szCounterName
&& !(eW
.szCounterName
= pdh_strdup_aw( e
->szCounterName
))) goto done
;
1153 eW
.dwInstanceIndex
= e
->dwInstanceIndex
;
1156 ret
= PdhMakeCounterPathW( &eW
, NULL
, &buflenW
, flags
);
1157 if (ret
== PDH_MORE_DATA
)
1159 if ((bufferW
= heap_alloc( buflenW
* sizeof(WCHAR
) )))
1161 if (!(ret
= PdhMakeCounterPathW( &eW
, bufferW
, &buflenW
, flags
)))
1163 int len
= WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, NULL
, 0, NULL
, NULL
);
1164 if (*buflen
>= len
) WideCharToMultiByte(CP_ACP
, 0, bufferW
, -1, buffer
, *buflen
, NULL
, NULL
);
1165 else ret
= PDH_MORE_DATA
;
1168 heap_free( bufferW
);
1171 ret
= PDH_MEMORY_ALLOCATION_FAILURE
;
1175 heap_free( eW
.szMachineName
);
1176 heap_free( eW
.szObjectName
);
1177 heap_free( eW
.szInstanceName
);
1178 heap_free( eW
.szParentInstance
);
1179 heap_free( eW
.szCounterName
);
1183 /***********************************************************************
1184 * PdhMakeCounterPathW (PDH.@)
1186 PDH_STATUS WINAPI
PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W
*e
, LPWSTR buffer
,
1187 LPDWORD buflen
, DWORD flags
)
1189 static const WCHAR bslash
[] = {'\\',0};
1190 static const WCHAR fslash
[] = {'/',0};
1191 static const WCHAR lparen
[] = {'(',0};
1192 static const WCHAR rparen
[] = {')',0};
1193 static const WCHAR fmt
[] = {'#','%','u',0};
1195 WCHAR path
[PDH_MAX_COUNTER_NAME
], instance
[12];
1196 PDH_STATUS ret
= ERROR_SUCCESS
;
1199 TRACE("%p %p %p 0x%08x\n", e
, buffer
, buflen
, flags
);
1201 if (flags
) FIXME("unimplemented flags 0x%08x\n", flags
);
1203 if (!e
|| !e
->szCounterName
|| !e
->szObjectName
|| !buflen
)
1204 return PDH_INVALID_ARGUMENT
;
1207 if (e
->szMachineName
)
1209 strcatW(path
, bslash
);
1210 strcatW(path
, bslash
);
1211 strcatW(path
, e
->szMachineName
);
1213 strcatW(path
, bslash
);
1214 strcatW(path
, e
->szObjectName
);
1215 if (e
->szInstanceName
)
1217 strcatW(path
, lparen
);
1218 if (e
->szParentInstance
)
1220 strcatW(path
, e
->szParentInstance
);
1221 strcatW(path
, fslash
);
1223 strcatW(path
, e
->szInstanceName
);
1224 sprintfW(instance
, fmt
, e
->dwInstanceIndex
);
1225 strcatW(path
, instance
);
1226 strcatW(path
, rparen
);
1228 strcatW(path
, bslash
);
1229 strcatW(path
, e
->szCounterName
);
1231 len
= strlenW(path
) + 1;
1232 if (*buflen
>= len
) strcpyW(buffer
, path
);
1233 else ret
= PDH_MORE_DATA
;
1238 /***********************************************************************
1239 * PdhEnumObjectItemsA (PDH.@)
1241 PDH_STATUS WINAPI
PdhEnumObjectItemsA(LPCSTR szDataSource
, LPCSTR szMachineName
, LPCSTR szObjectName
,
1242 LPSTR mszCounterList
, LPDWORD pcchCounterListLength
, LPSTR mszInstanceList
,
1243 LPDWORD pcchInstanceListLength
, DWORD dwDetailLevel
, DWORD dwFlags
)
1245 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource
), debugstr_a(szMachineName
),
1246 debugstr_a(szObjectName
), mszCounterList
, pcchCounterListLength
, mszInstanceList
,
1247 pcchInstanceListLength
, dwDetailLevel
, dwFlags
);
1249 return PDH_NOT_IMPLEMENTED
;
1252 /***********************************************************************
1253 * PdhEnumObjectItemsW (PDH.@)
1255 PDH_STATUS WINAPI
PdhEnumObjectItemsW(LPCWSTR szDataSource
, LPCWSTR szMachineName
, LPCWSTR szObjectName
,
1256 LPWSTR mszCounterList
, LPDWORD pcchCounterListLength
, LPWSTR mszInstanceList
,
1257 LPDWORD pcchInstanceListLength
, DWORD dwDetailLevel
, DWORD dwFlags
)
1259 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource
), debugstr_w(szMachineName
),
1260 debugstr_w(szObjectName
), mszCounterList
, pcchCounterListLength
, mszInstanceList
,
1261 pcchInstanceListLength
, dwDetailLevel
, dwFlags
);
1263 return PDH_NOT_IMPLEMENTED
;
1266 /***********************************************************************
1267 * PdhSetDefaultRealTimeDataSource (PDH.@)
1269 PDH_STATUS WINAPI
PdhSetDefaultRealTimeDataSource( DWORD source
)
1271 FIXME("%u\n", source
);
1272 return ERROR_SUCCESS
;