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
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
34 #include "wine/debug.h"
35 #include "wine/list.h"
36 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(pdh);
40 static CRITICAL_SECTION pdh_handle_cs;
41 static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug =
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 void *pdh_alloc( SIZE_T size )
52 return HeapAlloc( GetProcessHeap(), 0, size );
55 static inline void *pdh_alloc_zero( SIZE_T size )
57 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
60 static inline void pdh_free( LPVOID mem )
62 HeapFree( GetProcessHeap(), 0, mem );
65 static inline WCHAR *pdh_strdup( const WCHAR *src )
69 if (!src) return NULL;
70 if ((dst = pdh_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src );
74 static inline WCHAR *pdh_strdup_aw( const char *src )
79 if (!src) return NULL;
80 len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
81 if ((dst = pdh_alloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
85 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
87 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
89 if (fdwReason == DLL_WINE_PREATTACH) return FALSE; /* prefer native version */
91 if (fdwReason == DLL_PROCESS_ATTACH)
93 DisableThreadLibraryCalls( hinstDLL );
101 DWORD magic; /* signature */
102 struct list entry; /* list entry */
103 WCHAR *path; /* identifier */
104 DWORD type; /* counter type */
105 DWORD status; /* update status */
106 LONG scale; /* scale factor */
107 LONG defaultscale; /* default scale factor */
108 DWORD_PTR user; /* user data */
109 DWORD_PTR queryuser; /* query user data */
110 LONGLONG base; /* samples per second */
111 FILETIME stamp; /* time stamp */
112 void (CALLBACK *collect)( struct counter * ); /* collect callback */
118 } one; /* first value */
124 } two; /* second value */
127 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
129 static struct counter *create_counter( void )
131 struct counter *counter;
133 if ((counter = pdh_alloc_zero( sizeof(struct counter) )))
135 counter->magic = PDH_MAGIC_COUNTER;
141 static void destroy_counter( struct counter *counter )
144 pdh_free( counter->path );
148 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
152 DWORD magic; /* signature */
153 DWORD_PTR user; /* user data */
154 HANDLE thread; /* collect thread */
155 DWORD interval; /* collect interval */
156 HANDLE wait; /* wait event */
157 HANDLE stop; /* stop event */
158 struct list counters; /* counter list */
161 static struct query *create_query( void )
165 if ((query = pdh_alloc_zero( sizeof(struct query) )))
167 query->magic = PDH_MAGIC_QUERY;
168 list_init( &query->counters );
174 static void destroy_query( struct query *query )
182 DWORD index; /* name index */
183 const WCHAR *path; /* identifier */
184 void (CALLBACK *collect)( struct counter * ); /* collect callback */
185 DWORD type; /* counter type */
186 LONG scale; /* default scale factor */
187 LONGLONG base; /* samples per second */
190 static const WCHAR path_processor_time[] =
191 {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
192 '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
193 static const WCHAR path_uptime[] =
194 {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
196 static void CALLBACK collect_processor_time( struct counter *counter )
198 counter->two.largevalue = 500000; /* FIXME */
199 counter->status = PDH_CSTATUS_VALID_DATA;
202 static void CALLBACK collect_uptime( struct counter *counter )
204 counter->two.largevalue = GetTickCount64();
205 counter->status = PDH_CSTATUS_VALID_DATA;
208 #define TYPE_PROCESSOR_TIME \
209 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
210 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
212 #define TYPE_UPTIME \
213 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
215 /* counter source registry */
216 static const struct source counter_sources[] =
218 { 6, path_processor_time, collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 },
219 { 674, path_uptime, collect_uptime, TYPE_UPTIME, -3, 1000 }
222 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
226 if (strchrW( path, '\\')) p = fullpath;
227 else p = strrchrW( fullpath, '\\' ) + 1;
228 if (strcmpW( p, path )) return FALSE;
232 /***********************************************************************
233 * PdhAddCounterA (PDH.@)
235 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
236 DWORD_PTR userdata, PDH_HCOUNTER *counter )
241 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
243 if (!path) return PDH_INVALID_ARGUMENT;
245 if (!(pathW = pdh_strdup_aw( path )))
246 return PDH_MEMORY_ALLOCATION_FAILURE;
248 ret = PdhAddCounterW( query, pathW, userdata, counter );
254 /***********************************************************************
255 * PdhAddCounterW (PDH.@)
257 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
258 DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
260 struct query *query = hquery;
261 struct counter *counter;
264 TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
266 if (!path || !hcounter) return PDH_INVALID_ARGUMENT;
268 EnterCriticalSection( &pdh_handle_cs );
269 if (!query || query->magic != PDH_MAGIC_QUERY)
271 LeaveCriticalSection( &pdh_handle_cs );
272 return PDH_INVALID_HANDLE;
276 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
278 if (pdh_match_path( counter_sources[i].path, path ))
280 if ((counter = create_counter()))
282 counter->path = pdh_strdup( counter_sources[i].path );
283 counter->collect = counter_sources[i].collect;
284 counter->type = counter_sources[i].type;
285 counter->defaultscale = counter_sources[i].scale;
286 counter->base = counter_sources[i].base;
287 counter->queryuser = query->user;
288 counter->user = userdata;
290 list_add_tail( &query->counters, &counter->entry );
293 LeaveCriticalSection( &pdh_handle_cs );
294 return ERROR_SUCCESS;
296 LeaveCriticalSection( &pdh_handle_cs );
297 return PDH_MEMORY_ALLOCATION_FAILURE;
300 LeaveCriticalSection( &pdh_handle_cs );
301 return PDH_CSTATUS_NO_COUNTER;
304 /***********************************************************************
305 * PdhAddEnglishCounterA (PDH.@)
307 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
308 DWORD_PTR userdata, PDH_HCOUNTER *counter )
310 return PdhAddCounterA( query, path, userdata, counter );
313 /***********************************************************************
314 * PdhAddEnglishCounterW (PDH.@)
316 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
317 DWORD_PTR userdata, PDH_HCOUNTER *counter )
319 return PdhAddCounterW( query, path, userdata, counter );
322 /* caller must hold query lock */
323 static void shutdown_query_thread( struct query *query )
325 SetEvent( query->stop );
326 WaitForSingleObject( query->thread, INFINITE );
328 CloseHandle( query->stop );
329 CloseHandle( query->thread );
331 query->thread = NULL;
334 /***********************************************************************
335 * PdhCloseQuery (PDH.@)
337 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
339 struct query *query = handle;
340 struct list *item, *next;
342 TRACE("%p\n", handle);
344 EnterCriticalSection( &pdh_handle_cs );
345 if (!query || query->magic != PDH_MAGIC_QUERY)
347 LeaveCriticalSection( &pdh_handle_cs );
348 return PDH_INVALID_HANDLE;
351 if (query->thread) shutdown_query_thread( query );
353 LIST_FOR_EACH_SAFE( item, next, &query->counters )
355 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
357 list_remove( &counter->entry );
358 destroy_counter( counter );
361 destroy_query( query );
363 LeaveCriticalSection( &pdh_handle_cs );
364 return ERROR_SUCCESS;
367 /* caller must hold query lock */
368 static void collect_query_data( struct query *query )
372 LIST_FOR_EACH( item, &query->counters )
375 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
377 counter->collect( counter );
379 GetLocalTime( &time );
380 SystemTimeToFileTime( &time, &counter->stamp );
384 /***********************************************************************
385 * PdhCollectQueryData (PDH.@)
387 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
389 struct query *query = handle;
391 TRACE("%p\n", handle);
393 EnterCriticalSection( &pdh_handle_cs );
394 if (!query || query->magic != PDH_MAGIC_QUERY)
396 LeaveCriticalSection( &pdh_handle_cs );
397 return PDH_INVALID_HANDLE;
400 if (list_empty( &query->counters ))
402 LeaveCriticalSection( &pdh_handle_cs );
406 collect_query_data( query );
408 LeaveCriticalSection( &pdh_handle_cs );
409 return ERROR_SUCCESS;
412 static DWORD CALLBACK collect_query_thread( void *arg )
414 struct query *query = arg;
415 DWORD interval = query->interval;
416 HANDLE stop = query->stop;
421 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
423 EnterCriticalSection( &pdh_handle_cs );
424 if (!query || query->magic != PDH_MAGIC_QUERY)
426 LeaveCriticalSection( &pdh_handle_cs );
427 ExitThread( PDH_INVALID_HANDLE );
430 collect_query_data( query );
432 if (!SetEvent( query->wait ))
434 LeaveCriticalSection( &pdh_handle_cs );
437 LeaveCriticalSection( &pdh_handle_cs );
441 /***********************************************************************
442 * PdhCollectQueryDataEx (PDH.@)
444 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
447 struct query *query = handle;
449 TRACE("%p %d %p\n", handle, interval, event);
451 EnterCriticalSection( &pdh_handle_cs );
452 if (!query || query->magic != PDH_MAGIC_QUERY)
454 LeaveCriticalSection( &pdh_handle_cs );
455 return PDH_INVALID_HANDLE;
457 if (list_empty( &query->counters ))
459 LeaveCriticalSection( &pdh_handle_cs );
462 if (query->thread) shutdown_query_thread( query );
463 if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
465 ret = GetLastError();
466 LeaveCriticalSection( &pdh_handle_cs );
470 query->interval = interval * 1000;
471 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
473 ret = GetLastError();
474 CloseHandle( query->stop );
476 LeaveCriticalSection( &pdh_handle_cs );
479 WaitForSingleObject( query->stop, INFINITE );
481 LeaveCriticalSection( &pdh_handle_cs );
482 return ERROR_SUCCESS;
485 /***********************************************************************
486 * PdhCollectQueryDataWithTime (PDH.@)
488 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
490 struct query *query = handle;
492 TRACE("%p %p\n", handle, timestamp);
494 EnterCriticalSection( &pdh_handle_cs );
495 if (!query || query->magic != PDH_MAGIC_QUERY)
497 LeaveCriticalSection( &pdh_handle_cs );
498 return PDH_INVALID_HANDLE;
500 if (list_empty( &query->counters ))
502 LeaveCriticalSection( &pdh_handle_cs );
506 collect_query_data( query );
510 struct list *item = list_head( &query->counters );
511 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
513 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
515 LeaveCriticalSection( &pdh_handle_cs );
516 return ERROR_SUCCESS;
519 /***********************************************************************
520 * PdhGetCounterInfoA (PDH.@)
522 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
524 struct counter *counter = handle;
526 TRACE("%p %d %p %p\n", handle, text, size, info);
528 EnterCriticalSection( &pdh_handle_cs );
529 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
531 LeaveCriticalSection( &pdh_handle_cs );
532 return PDH_INVALID_HANDLE;
536 LeaveCriticalSection( &pdh_handle_cs );
537 return PDH_INVALID_ARGUMENT;
539 if (*size < sizeof(PDH_COUNTER_INFO_A))
541 *size = sizeof(PDH_COUNTER_INFO_A);
542 LeaveCriticalSection( &pdh_handle_cs );
543 return PDH_MORE_DATA;
546 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
548 info->dwType = counter->type;
549 info->CStatus = counter->status;
550 info->lScale = counter->scale;
551 info->lDefaultScale = counter->defaultscale;
552 info->dwUserData = counter->user;
553 info->dwQueryUserData = counter->queryuser;
555 *size = sizeof(PDH_COUNTER_INFO_A);
557 LeaveCriticalSection( &pdh_handle_cs );
558 return ERROR_SUCCESS;
561 /***********************************************************************
562 * PdhGetCounterInfoW (PDH.@)
564 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
566 struct counter *counter = handle;
568 TRACE("%p %d %p %p\n", handle, text, size, info);
570 EnterCriticalSection( &pdh_handle_cs );
571 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
573 LeaveCriticalSection( &pdh_handle_cs );
574 return PDH_INVALID_HANDLE;
578 LeaveCriticalSection( &pdh_handle_cs );
579 return PDH_INVALID_ARGUMENT;
581 if (*size < sizeof(PDH_COUNTER_INFO_W))
583 *size = sizeof(PDH_COUNTER_INFO_W);
584 LeaveCriticalSection( &pdh_handle_cs );
585 return PDH_MORE_DATA;
588 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
590 info->dwType = counter->type;
591 info->CStatus = counter->status;
592 info->lScale = counter->scale;
593 info->lDefaultScale = counter->defaultscale;
594 info->dwUserData = counter->user;
595 info->dwQueryUserData = counter->queryuser;
597 *size = sizeof(PDH_COUNTER_INFO_W);
599 LeaveCriticalSection( &pdh_handle_cs );
600 return ERROR_SUCCESS;
603 /***********************************************************************
604 * PdhGetCounterTimeBase (PDH.@)
606 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
608 struct counter *counter = handle;
610 TRACE("%p %p\n", handle, base);
612 if (!base) return PDH_INVALID_ARGUMENT;
614 EnterCriticalSection( &pdh_handle_cs );
615 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
617 LeaveCriticalSection( &pdh_handle_cs );
618 return PDH_INVALID_HANDLE;
621 *base = counter->base;
623 LeaveCriticalSection( &pdh_handle_cs );
624 return ERROR_SUCCESS;
627 /***********************************************************************
628 * PdhGetFormattedCounterValue (PDH.@)
630 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
631 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
634 struct counter *counter = handle;
636 TRACE("%p %x %p %p\n", handle, format, type, value);
638 if (!value) return PDH_INVALID_ARGUMENT;
640 EnterCriticalSection( &pdh_handle_cs );
641 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
643 LeaveCriticalSection( &pdh_handle_cs );
644 return PDH_INVALID_HANDLE;
648 LeaveCriticalSection( &pdh_handle_cs );
649 return PDH_INVALID_DATA;
652 factor = counter->scale ? counter->scale : counter->defaultscale;
653 if (format & PDH_FMT_LONG)
655 if (format & PDH_FMT_1000) value->u.longValue = counter->two.longvalue * 1000;
656 else value->u.longValue = counter->two.longvalue * pow( 10, factor );
658 else if (format & PDH_FMT_LARGE)
660 if (format & PDH_FMT_1000) value->u.largeValue = counter->two.largevalue * 1000;
661 else value->u.largeValue = counter->two.largevalue * pow( 10, factor );
663 else if (format & PDH_FMT_DOUBLE)
665 if (format & PDH_FMT_1000) value->u.doubleValue = counter->two.doublevalue * 1000;
666 else value->u.doubleValue = counter->two.doublevalue * pow( 10, factor );
670 WARN("unknown format %x\n", format);
671 LeaveCriticalSection( &pdh_handle_cs );
672 return PDH_INVALID_ARGUMENT;
674 value->CStatus = ERROR_SUCCESS;
675 if (type) *type = counter->type;
677 LeaveCriticalSection( &pdh_handle_cs );
678 return ERROR_SUCCESS;
681 /***********************************************************************
682 * PdhGetRawCounterValue (PDH.@)
684 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
685 PPDH_RAW_COUNTER value )
687 struct counter *counter = handle;
689 TRACE("%p %p %p\n", handle, type, value);
691 if (!value) return PDH_INVALID_ARGUMENT;
693 EnterCriticalSection( &pdh_handle_cs );
694 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
696 LeaveCriticalSection( &pdh_handle_cs );
697 return PDH_INVALID_HANDLE;
700 value->CStatus = counter->status;
701 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
702 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
703 value->FirstValue = counter->one.largevalue;
704 value->SecondValue = counter->two.largevalue;
705 value->MultiCount = 1; /* FIXME */
707 if (type) *type = counter->type;
709 LeaveCriticalSection( &pdh_handle_cs );
710 return ERROR_SUCCESS;
713 /***********************************************************************
714 * PdhLookupPerfIndexByNameA (PDH.@)
716 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
721 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
723 if (!name || !index) return PDH_INVALID_ARGUMENT;
727 FIXME("remote machine not supported\n");
728 return PDH_CSTATUS_NO_MACHINE;
730 if (!(nameW = pdh_strdup_aw( name )))
731 return PDH_MEMORY_ALLOCATION_FAILURE;
733 ret = PdhLookupPerfIndexByNameW( NULL, nameW, index );
739 /***********************************************************************
740 * PdhLookupPerfIndexByNameW (PDH.@)
742 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
746 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
748 if (!name || !index) return PDH_INVALID_ARGUMENT;
752 FIXME("remote machine not supported\n");
753 return PDH_CSTATUS_NO_MACHINE;
755 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
757 if (pdh_match_path( counter_sources[i].path, name ))
759 *index = counter_sources[i].index;
760 return ERROR_SUCCESS;
763 return PDH_STRING_NOT_FOUND;
766 /***********************************************************************
767 * PdhLookupPerfNameByIndexA (PDH.@)
769 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
772 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
773 DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR);
775 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
779 FIXME("remote machine not supported\n");
780 return PDH_CSTATUS_NO_MACHINE;
783 if (!buffer && !size) return PDH_INVALID_ARGUMENT;
784 if (!index) return ERROR_SUCCESS;
786 if (!(ret = PdhLookupPerfNameByIndexW( NULL, index, bufferW, &sizeW )))
788 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
790 if (size && *size < required) ret = PDH_MORE_DATA;
791 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
792 if (size) *size = required;
797 /***********************************************************************
798 * PdhLookupPerfNameByIndexW (PDH.@)
800 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
805 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
809 FIXME("remote machine not supported\n");
810 return PDH_CSTATUS_NO_MACHINE;
813 if (!buffer && !size) return PDH_INVALID_ARGUMENT;
814 if (!index) return ERROR_SUCCESS;
816 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
818 if (counter_sources[i].index == index)
820 WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1;
821 unsigned int required = strlenW( p ) + 1;
823 if (*size < required) ret = PDH_MORE_DATA;
826 strcpyW( buffer, p );
833 return PDH_INVALID_ARGUMENT;
836 /***********************************************************************
837 * PdhOpenQueryA (PDH.@)
839 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
842 WCHAR *sourceW = NULL;
844 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
846 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
848 ret = PdhOpenQueryW( sourceW, userdata, query );
854 /***********************************************************************
855 * PdhOpenQueryW (PDH.@)
857 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
861 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
863 if (!handle) return PDH_INVALID_ARGUMENT;
867 FIXME("log file data source not supported\n");
868 return PDH_INVALID_ARGUMENT;
870 if ((query = create_query()))
872 query->user = userdata;
875 return ERROR_SUCCESS;
877 return PDH_MEMORY_ALLOCATION_FAILURE;
880 /***********************************************************************
881 * PdhRemoveCounter (PDH.@)
883 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
885 struct counter *counter = handle;
887 TRACE("%p\n", handle);
889 EnterCriticalSection( &pdh_handle_cs );
890 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
892 LeaveCriticalSection( &pdh_handle_cs );
893 return PDH_INVALID_HANDLE;
896 list_remove( &counter->entry );
897 destroy_counter( counter );
899 LeaveCriticalSection( &pdh_handle_cs );
900 return ERROR_SUCCESS;
903 /***********************************************************************
904 * PdhSetCounterScaleFactor (PDH.@)
906 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
908 struct counter *counter = handle;
910 TRACE("%p\n", handle);
912 EnterCriticalSection( &pdh_handle_cs );
913 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
915 LeaveCriticalSection( &pdh_handle_cs );
916 return PDH_INVALID_HANDLE;
918 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
920 LeaveCriticalSection( &pdh_handle_cs );
921 return PDH_INVALID_ARGUMENT;
924 counter->scale = factor;
926 LeaveCriticalSection( &pdh_handle_cs );
927 return ERROR_SUCCESS;
930 /***********************************************************************
931 * PdhValidatePathA (PDH.@)
933 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
938 TRACE("%s\n", debugstr_a(path));
940 if (!path) return PDH_INVALID_ARGUMENT;
941 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
943 ret = PdhValidatePathW( pathW );
949 static PDH_STATUS validate_path( LPCWSTR path )
951 if (!path || !*path) return PDH_INVALID_ARGUMENT;
952 if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
953 return ERROR_SUCCESS;
956 /***********************************************************************
957 * PdhValidatePathW (PDH.@)
959 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
964 TRACE("%s\n", debugstr_w(path));
966 if ((ret = validate_path( path ))) return ret;
968 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
969 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
971 return PDH_CSTATUS_NO_COUNTER;
974 /***********************************************************************
975 * PdhValidatePathExA (PDH.@)
977 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
979 TRACE("%p %s\n", source, debugstr_a(path));
983 FIXME("log file data source not supported\n");
984 return ERROR_SUCCESS;
986 return PdhValidatePathA( path );
989 /***********************************************************************
990 * PdhValidatePathExW (PDH.@)
992 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
994 TRACE("%p %s\n", source, debugstr_w(path));
998 FIXME("log file data source not supported\n");
999 return ERROR_SUCCESS;
1001 return PdhValidatePathW( path );