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 *heap_alloc( SIZE_T size )
52 return HeapAlloc( GetProcessHeap(), 0, size );
55 static inline void *heap_alloc_zero( SIZE_T size )
57 return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
60 static inline void heap_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 = heap_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 = heap_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);
90 case DLL_WINE_PREATTACH:
91 return FALSE; /* prefer native version */
92 case DLL_PROCESS_ATTACH:
93 DisableThreadLibraryCalls(hinstDLL);
95 case DLL_PROCESS_DETACH:
96 DeleteCriticalSection(&pdh_handle_cs);
112 DWORD magic; /* signature */
113 struct list entry; /* list entry */
114 WCHAR *path; /* identifier */
115 DWORD type; /* counter type */
116 DWORD status; /* update status */
117 LONG scale; /* scale factor */
118 LONG defaultscale; /* default scale factor */
119 DWORD_PTR user; /* user data */
120 DWORD_PTR queryuser; /* query user data */
121 LONGLONG base; /* samples per second */
122 FILETIME stamp; /* time stamp */
123 void (CALLBACK *collect)( struct counter * ); /* collect callback */
124 union value one; /* first value */
125 union value two; /* second value */
128 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
130 static struct counter *create_counter( void )
132 struct counter *counter;
134 if ((counter = heap_alloc_zero( sizeof(struct counter) )))
136 counter->magic = PDH_MAGIC_COUNTER;
142 static void destroy_counter( struct counter *counter )
145 heap_free( counter->path );
146 heap_free( counter );
149 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
153 DWORD magic; /* signature */
154 DWORD_PTR user; /* user data */
155 HANDLE thread; /* collect thread */
156 DWORD interval; /* collect interval */
157 HANDLE wait; /* wait event */
158 HANDLE stop; /* stop event */
159 struct list counters; /* counter list */
162 static struct query *create_query( void )
166 if ((query = heap_alloc_zero( sizeof(struct query) )))
168 query->magic = PDH_MAGIC_QUERY;
169 list_init( &query->counters );
175 static void destroy_query( struct query *query )
183 DWORD index; /* name index */
184 const WCHAR *path; /* identifier */
185 void (CALLBACK *collect)( struct counter * ); /* collect callback */
186 DWORD type; /* counter type */
187 LONG scale; /* default scale factor */
188 LONGLONG base; /* samples per second */
191 static const WCHAR path_processor_time[] =
192 {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
193 '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
194 static const WCHAR path_uptime[] =
195 {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
197 static void CALLBACK collect_processor_time( struct counter *counter )
199 counter->two.largevalue = 500000; /* FIXME */
200 counter->status = PDH_CSTATUS_VALID_DATA;
203 static void CALLBACK collect_uptime( struct counter *counter )
205 counter->two.largevalue = GetTickCount64();
206 counter->status = PDH_CSTATUS_VALID_DATA;
209 #define TYPE_PROCESSOR_TIME \
210 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
211 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
213 #define TYPE_UPTIME \
214 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
216 /* counter source registry */
217 static const struct source counter_sources[] =
219 { 6, path_processor_time, collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 },
220 { 674, path_uptime, collect_uptime, TYPE_UPTIME, -3, 1000 }
223 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
227 if (strchrW( path, '\\')) p = fullpath;
228 else p = strrchrW( fullpath, '\\' ) + 1;
229 if (strcmpW( p, path )) return FALSE;
233 /***********************************************************************
234 * PdhAddCounterA (PDH.@)
236 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
237 DWORD_PTR userdata, PDH_HCOUNTER *counter )
242 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
244 if (!path) return PDH_INVALID_ARGUMENT;
246 if (!(pathW = pdh_strdup_aw( path )))
247 return PDH_MEMORY_ALLOCATION_FAILURE;
249 ret = PdhAddCounterW( query, pathW, userdata, counter );
255 /***********************************************************************
256 * PdhAddCounterW (PDH.@)
258 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
259 DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
261 struct query *query = hquery;
262 struct counter *counter;
265 TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
267 if (!path || !hcounter) return PDH_INVALID_ARGUMENT;
269 EnterCriticalSection( &pdh_handle_cs );
270 if (!query || query->magic != PDH_MAGIC_QUERY)
272 LeaveCriticalSection( &pdh_handle_cs );
273 return PDH_INVALID_HANDLE;
277 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
279 if (pdh_match_path( counter_sources[i].path, path ))
281 if ((counter = create_counter()))
283 counter->path = pdh_strdup( counter_sources[i].path );
284 counter->collect = counter_sources[i].collect;
285 counter->type = counter_sources[i].type;
286 counter->defaultscale = counter_sources[i].scale;
287 counter->base = counter_sources[i].base;
288 counter->queryuser = query->user;
289 counter->user = userdata;
291 list_add_tail( &query->counters, &counter->entry );
294 LeaveCriticalSection( &pdh_handle_cs );
295 return ERROR_SUCCESS;
297 LeaveCriticalSection( &pdh_handle_cs );
298 return PDH_MEMORY_ALLOCATION_FAILURE;
301 LeaveCriticalSection( &pdh_handle_cs );
302 return PDH_CSTATUS_NO_COUNTER;
305 /***********************************************************************
306 * PdhAddEnglishCounterA (PDH.@)
308 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
309 DWORD_PTR userdata, PDH_HCOUNTER *counter )
311 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
313 if (!query) return PDH_INVALID_ARGUMENT;
314 return PdhAddCounterA( query, path, userdata, counter );
317 /***********************************************************************
318 * PdhAddEnglishCounterW (PDH.@)
320 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
321 DWORD_PTR userdata, PDH_HCOUNTER *counter )
323 TRACE("%p %s %lx %p\n", query, debugstr_w(path), userdata, counter);
325 if (!query) return PDH_INVALID_ARGUMENT;
326 return PdhAddCounterW( query, path, userdata, counter );
329 /* caller must hold counter lock */
330 static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
331 union value *raw2, PDH_FMT_COUNTERVALUE *value )
335 factor = counter->scale ? counter->scale : counter->defaultscale;
336 if (format & PDH_FMT_LONG)
338 if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000;
339 else value->u.longValue = raw2->longvalue * pow( 10, factor );
341 else if (format & PDH_FMT_LARGE)
343 if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000;
344 else value->u.largeValue = raw2->largevalue * pow( 10, factor );
346 else if (format & PDH_FMT_DOUBLE)
348 if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000;
349 else value->u.doubleValue = raw2->doublevalue * pow( 10, factor );
353 WARN("unknown format %x\n", format);
354 return PDH_INVALID_ARGUMENT;
356 return ERROR_SUCCESS;
359 /***********************************************************************
360 * PdhCalculateCounterFromRawValue (PDH.@)
362 PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
363 PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
364 PPDH_FMT_COUNTERVALUE value )
367 struct counter *counter = handle;
369 TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value);
371 if (!value) return PDH_INVALID_ARGUMENT;
373 EnterCriticalSection( &pdh_handle_cs );
374 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
376 LeaveCriticalSection( &pdh_handle_cs );
377 return PDH_INVALID_HANDLE;
380 ret = format_value( counter, format, (union value *)&raw1->SecondValue,
381 (union value *)&raw2->SecondValue, value );
383 LeaveCriticalSection( &pdh_handle_cs );
388 /***********************************************************************
389 * PdhCloseQuery (PDH.@)
391 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
393 struct query *query = handle;
394 struct list *item, *next;
396 TRACE("%p\n", handle);
398 EnterCriticalSection( &pdh_handle_cs );
399 if (!query || query->magic != PDH_MAGIC_QUERY)
401 LeaveCriticalSection( &pdh_handle_cs );
402 return PDH_INVALID_HANDLE;
407 HANDLE thread = query->thread;
408 SetEvent( query->stop );
409 LeaveCriticalSection( &pdh_handle_cs );
411 WaitForSingleObject( thread, INFINITE );
413 EnterCriticalSection( &pdh_handle_cs );
414 if (query->magic != PDH_MAGIC_QUERY)
416 LeaveCriticalSection( &pdh_handle_cs );
417 return ERROR_SUCCESS;
419 CloseHandle( query->stop );
420 CloseHandle( query->thread );
421 query->thread = NULL;
424 LIST_FOR_EACH_SAFE( item, next, &query->counters )
426 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
428 list_remove( &counter->entry );
429 destroy_counter( counter );
432 destroy_query( query );
434 LeaveCriticalSection( &pdh_handle_cs );
435 return ERROR_SUCCESS;
438 /* caller must hold query lock */
439 static void collect_query_data( struct query *query )
443 LIST_FOR_EACH( item, &query->counters )
446 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
448 counter->collect( counter );
450 GetLocalTime( &time );
451 SystemTimeToFileTime( &time, &counter->stamp );
455 /***********************************************************************
456 * PdhCollectQueryData (PDH.@)
458 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
460 struct query *query = handle;
462 TRACE("%p\n", handle);
464 EnterCriticalSection( &pdh_handle_cs );
465 if (!query || query->magic != PDH_MAGIC_QUERY)
467 LeaveCriticalSection( &pdh_handle_cs );
468 return PDH_INVALID_HANDLE;
471 if (list_empty( &query->counters ))
473 LeaveCriticalSection( &pdh_handle_cs );
477 collect_query_data( query );
479 LeaveCriticalSection( &pdh_handle_cs );
480 return ERROR_SUCCESS;
483 static DWORD CALLBACK collect_query_thread( void *arg )
485 struct query *query = arg;
486 DWORD interval = query->interval;
487 HANDLE stop = query->stop;
491 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
493 EnterCriticalSection( &pdh_handle_cs );
494 if (query->magic != PDH_MAGIC_QUERY)
496 LeaveCriticalSection( &pdh_handle_cs );
497 ExitThread( PDH_INVALID_HANDLE );
500 collect_query_data( query );
502 if (!SetEvent( query->wait ))
504 LeaveCriticalSection( &pdh_handle_cs );
507 LeaveCriticalSection( &pdh_handle_cs );
511 /***********************************************************************
512 * PdhCollectQueryDataEx (PDH.@)
514 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
517 struct query *query = handle;
519 TRACE("%p %d %p\n", handle, interval, event);
521 EnterCriticalSection( &pdh_handle_cs );
522 if (!query || query->magic != PDH_MAGIC_QUERY)
524 LeaveCriticalSection( &pdh_handle_cs );
525 return PDH_INVALID_HANDLE;
527 if (list_empty( &query->counters ))
529 LeaveCriticalSection( &pdh_handle_cs );
534 HANDLE thread = query->thread;
535 SetEvent( query->stop );
536 LeaveCriticalSection( &pdh_handle_cs );
538 WaitForSingleObject( thread, INFINITE );
540 EnterCriticalSection( &pdh_handle_cs );
541 if (query->magic != PDH_MAGIC_QUERY)
543 LeaveCriticalSection( &pdh_handle_cs );
544 return PDH_INVALID_HANDLE;
546 CloseHandle( query->thread );
547 query->thread = NULL;
549 else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
551 ret = GetLastError();
552 LeaveCriticalSection( &pdh_handle_cs );
556 query->interval = interval * 1000;
557 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
559 ret = GetLastError();
560 CloseHandle( query->stop );
562 LeaveCriticalSection( &pdh_handle_cs );
566 LeaveCriticalSection( &pdh_handle_cs );
567 return ERROR_SUCCESS;
570 /***********************************************************************
571 * PdhCollectQueryDataWithTime (PDH.@)
573 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
575 struct query *query = handle;
576 struct counter *counter;
579 TRACE("%p %p\n", handle, timestamp);
581 if (!timestamp) return PDH_INVALID_ARGUMENT;
583 EnterCriticalSection( &pdh_handle_cs );
584 if (!query || query->magic != PDH_MAGIC_QUERY)
586 LeaveCriticalSection( &pdh_handle_cs );
587 return PDH_INVALID_HANDLE;
589 if (list_empty( &query->counters ))
591 LeaveCriticalSection( &pdh_handle_cs );
595 collect_query_data( query );
597 item = list_head( &query->counters );
598 counter = LIST_ENTRY( item, struct counter, entry );
600 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
602 LeaveCriticalSection( &pdh_handle_cs );
603 return ERROR_SUCCESS;
606 /***********************************************************************
607 * PdhExpandWildCardPathA (PDH.@)
609 PDH_STATUS WINAPI PdhExpandWildCardPathA( LPCSTR szDataSource, LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
611 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
612 return PDH_NOT_IMPLEMENTED;
615 /***********************************************************************
616 * PdhExpandWildCardPathW (PDH.@)
618 PDH_STATUS WINAPI PdhExpandWildCardPathW( LPCWSTR szDataSource, LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
620 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
621 return PDH_NOT_IMPLEMENTED;
624 /***********************************************************************
625 * PdhGetCounterInfoA (PDH.@)
627 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
629 struct counter *counter = handle;
631 TRACE("%p %d %p %p\n", handle, text, size, info);
633 EnterCriticalSection( &pdh_handle_cs );
634 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
636 LeaveCriticalSection( &pdh_handle_cs );
637 return PDH_INVALID_HANDLE;
641 LeaveCriticalSection( &pdh_handle_cs );
642 return PDH_INVALID_ARGUMENT;
644 if (*size < sizeof(PDH_COUNTER_INFO_A))
646 *size = sizeof(PDH_COUNTER_INFO_A);
647 LeaveCriticalSection( &pdh_handle_cs );
648 return PDH_MORE_DATA;
651 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
653 info->dwType = counter->type;
654 info->CStatus = counter->status;
655 info->lScale = counter->scale;
656 info->lDefaultScale = counter->defaultscale;
657 info->dwUserData = counter->user;
658 info->dwQueryUserData = counter->queryuser;
660 *size = sizeof(PDH_COUNTER_INFO_A);
662 LeaveCriticalSection( &pdh_handle_cs );
663 return ERROR_SUCCESS;
666 /***********************************************************************
667 * PdhGetCounterInfoW (PDH.@)
669 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
671 struct counter *counter = handle;
673 TRACE("%p %d %p %p\n", handle, text, size, info);
675 EnterCriticalSection( &pdh_handle_cs );
676 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
678 LeaveCriticalSection( &pdh_handle_cs );
679 return PDH_INVALID_HANDLE;
683 LeaveCriticalSection( &pdh_handle_cs );
684 return PDH_INVALID_ARGUMENT;
686 if (*size < sizeof(PDH_COUNTER_INFO_W))
688 *size = sizeof(PDH_COUNTER_INFO_W);
689 LeaveCriticalSection( &pdh_handle_cs );
690 return PDH_MORE_DATA;
693 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
695 info->dwType = counter->type;
696 info->CStatus = counter->status;
697 info->lScale = counter->scale;
698 info->lDefaultScale = counter->defaultscale;
699 info->dwUserData = counter->user;
700 info->dwQueryUserData = counter->queryuser;
702 *size = sizeof(PDH_COUNTER_INFO_W);
704 LeaveCriticalSection( &pdh_handle_cs );
705 return ERROR_SUCCESS;
708 /***********************************************************************
709 * PdhGetCounterTimeBase (PDH.@)
711 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
713 struct counter *counter = handle;
715 TRACE("%p %p\n", handle, base);
717 if (!base) return PDH_INVALID_ARGUMENT;
719 EnterCriticalSection( &pdh_handle_cs );
720 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
722 LeaveCriticalSection( &pdh_handle_cs );
723 return PDH_INVALID_HANDLE;
726 *base = counter->base;
728 LeaveCriticalSection( &pdh_handle_cs );
729 return ERROR_SUCCESS;
732 /***********************************************************************
733 * PdhGetDllVersion (PDH.@)
735 PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
738 return PDH_INVALID_ARGUMENT;
740 *version = PDH_VERSION;
742 return ERROR_SUCCESS;
745 /***********************************************************************
746 * PdhGetFormattedCounterValue (PDH.@)
748 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
749 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
752 struct counter *counter = handle;
754 TRACE("%p %x %p %p\n", handle, format, type, value);
756 if (!value) return PDH_INVALID_ARGUMENT;
758 EnterCriticalSection( &pdh_handle_cs );
759 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
761 LeaveCriticalSection( &pdh_handle_cs );
762 return PDH_INVALID_HANDLE;
766 LeaveCriticalSection( &pdh_handle_cs );
767 return PDH_INVALID_DATA;
769 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
771 value->CStatus = ERROR_SUCCESS;
772 if (type) *type = counter->type;
775 LeaveCriticalSection( &pdh_handle_cs );
779 /***********************************************************************
780 * PdhGetRawCounterValue (PDH.@)
782 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
783 PPDH_RAW_COUNTER value )
785 struct counter *counter = handle;
787 TRACE("%p %p %p\n", handle, type, value);
789 if (!value) return PDH_INVALID_ARGUMENT;
791 EnterCriticalSection( &pdh_handle_cs );
792 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
794 LeaveCriticalSection( &pdh_handle_cs );
795 return PDH_INVALID_HANDLE;
798 value->CStatus = counter->status;
799 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
800 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
801 value->FirstValue = counter->one.largevalue;
802 value->SecondValue = counter->two.largevalue;
803 value->MultiCount = 1; /* FIXME */
805 if (type) *type = counter->type;
807 LeaveCriticalSection( &pdh_handle_cs );
808 return ERROR_SUCCESS;
811 /***********************************************************************
812 * PdhLookupPerfIndexByNameA (PDH.@)
814 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
817 WCHAR *machineW = NULL;
820 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
822 if (!name) return PDH_INVALID_ARGUMENT;
824 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
826 if (!(nameW = pdh_strdup_aw( name )))
827 return PDH_MEMORY_ALLOCATION_FAILURE;
829 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
832 heap_free( machineW );
836 /***********************************************************************
837 * PdhLookupPerfIndexByNameW (PDH.@)
839 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
843 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
845 if (!name || !index) return PDH_INVALID_ARGUMENT;
849 FIXME("remote machine not supported\n");
850 return PDH_CSTATUS_NO_MACHINE;
852 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
854 if (pdh_match_path( counter_sources[i].path, name ))
856 *index = counter_sources[i].index;
857 return ERROR_SUCCESS;
860 return PDH_STRING_NOT_FOUND;
863 /***********************************************************************
864 * PdhLookupPerfNameByIndexA (PDH.@)
866 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
869 WCHAR *machineW = NULL;
870 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
871 DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR);
873 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
875 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
877 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
879 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
881 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
883 if (size && *size < required) ret = PDH_MORE_DATA;
884 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
885 if (size) *size = required;
887 heap_free( machineW );
891 /***********************************************************************
892 * PdhLookupPerfNameByIndexW (PDH.@)
894 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
899 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
903 FIXME("remote machine not supported\n");
904 return PDH_CSTATUS_NO_MACHINE;
907 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
908 if (!index) return ERROR_SUCCESS;
910 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
912 if (counter_sources[i].index == index)
914 WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1;
915 unsigned int required = strlenW( p ) + 1;
917 if (*size < required) ret = PDH_MORE_DATA;
920 strcpyW( buffer, p );
927 return PDH_INVALID_ARGUMENT;
930 /***********************************************************************
931 * PdhOpenQueryA (PDH.@)
933 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
936 WCHAR *sourceW = NULL;
938 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
940 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
942 ret = PdhOpenQueryW( sourceW, userdata, query );
943 heap_free( sourceW );
948 /***********************************************************************
949 * PdhOpenQueryW (PDH.@)
951 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
955 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
957 if (!handle) return PDH_INVALID_ARGUMENT;
961 FIXME("log file data source not supported\n");
962 return PDH_INVALID_ARGUMENT;
964 if ((query = create_query()))
966 query->user = userdata;
969 return ERROR_SUCCESS;
971 return PDH_MEMORY_ALLOCATION_FAILURE;
974 /***********************************************************************
975 * PdhRemoveCounter (PDH.@)
977 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
979 struct counter *counter = handle;
981 TRACE("%p\n", handle);
983 EnterCriticalSection( &pdh_handle_cs );
984 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
986 LeaveCriticalSection( &pdh_handle_cs );
987 return PDH_INVALID_HANDLE;
990 list_remove( &counter->entry );
991 destroy_counter( counter );
993 LeaveCriticalSection( &pdh_handle_cs );
994 return ERROR_SUCCESS;
997 /***********************************************************************
998 * PdhSetCounterScaleFactor (PDH.@)
1000 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
1002 struct counter *counter = handle;
1004 TRACE("%p\n", handle);
1006 EnterCriticalSection( &pdh_handle_cs );
1007 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1009 LeaveCriticalSection( &pdh_handle_cs );
1010 return PDH_INVALID_HANDLE;
1012 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
1014 LeaveCriticalSection( &pdh_handle_cs );
1015 return PDH_INVALID_ARGUMENT;
1018 counter->scale = factor;
1020 LeaveCriticalSection( &pdh_handle_cs );
1021 return ERROR_SUCCESS;
1024 /***********************************************************************
1025 * PdhValidatePathA (PDH.@)
1027 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1032 TRACE("%s\n", debugstr_a(path));
1034 if (!path) return PDH_INVALID_ARGUMENT;
1035 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1037 ret = PdhValidatePathW( pathW );
1043 static PDH_STATUS validate_path( LPCWSTR path )
1045 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1046 if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1047 return ERROR_SUCCESS;
1050 /***********************************************************************
1051 * PdhValidatePathW (PDH.@)
1053 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1058 TRACE("%s\n", debugstr_w(path));
1060 if ((ret = validate_path( path ))) return ret;
1062 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
1063 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1065 return PDH_CSTATUS_NO_COUNTER;
1068 /***********************************************************************
1069 * PdhValidatePathExA (PDH.@)
1071 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1073 TRACE("%p %s\n", source, debugstr_a(path));
1077 FIXME("log file data source not supported\n");
1078 return ERROR_SUCCESS;
1080 return PdhValidatePathA( path );
1083 /***********************************************************************
1084 * PdhValidatePathExW (PDH.@)
1086 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1088 TRACE("%p %s\n", source, debugstr_w(path));
1092 FIXME("log file data source not supported\n");
1093 return ERROR_SUCCESS;
1095 return PdhValidatePathW( path );
1098 /***********************************************************************
1099 * PdhMakeCounterPathA (PDH.@)
1101 PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1102 LPDWORD buflen, DWORD flags )
1104 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1105 PDH_COUNTER_PATH_ELEMENTS_W eW;
1109 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1111 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1113 memset( &eW, 0, sizeof(eW) );
1114 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1115 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1116 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1117 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1118 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1119 eW.dwInstanceIndex = e->dwInstanceIndex;
1122 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1123 if (ret == PDH_MORE_DATA)
1125 if ((bufferW = heap_alloc( buflenW * sizeof(WCHAR) )))
1127 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1129 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1130 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1131 else ret = PDH_MORE_DATA;
1134 heap_free( bufferW );
1137 ret = PDH_MEMORY_ALLOCATION_FAILURE;
1141 heap_free( eW.szMachineName );
1142 heap_free( eW.szObjectName );
1143 heap_free( eW.szInstanceName );
1144 heap_free( eW.szParentInstance );
1145 heap_free( eW.szCounterName );
1149 /***********************************************************************
1150 * PdhMakeCounterPathW (PDH.@)
1152 PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1153 LPDWORD buflen, DWORD flags )
1155 static const WCHAR bslash[] = {'\\',0};
1156 static const WCHAR fslash[] = {'/',0};
1157 static const WCHAR lparen[] = {'(',0};
1158 static const WCHAR rparen[] = {')',0};
1159 static const WCHAR fmt[] = {'#','%','u',0};
1161 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1162 PDH_STATUS ret = ERROR_SUCCESS;
1165 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1167 if (flags) FIXME("unimplemented flags 0x%08x\n", flags);
1169 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1170 return PDH_INVALID_ARGUMENT;
1173 if (e->szMachineName)
1175 strcatW(path, bslash);
1176 strcatW(path, bslash);
1177 strcatW(path, e->szMachineName);
1179 strcatW(path, bslash);
1180 strcatW(path, e->szObjectName);
1181 if (e->szInstanceName)
1183 strcatW(path, lparen);
1184 if (e->szParentInstance)
1186 strcatW(path, e->szParentInstance);
1187 strcatW(path, fslash);
1189 strcatW(path, e->szInstanceName);
1190 sprintfW(instance, fmt, e->dwInstanceIndex);
1191 strcatW(path, instance);
1192 strcatW(path, rparen);
1194 strcatW(path, bslash);
1195 strcatW(path, e->szCounterName);
1197 len = strlenW(path) + 1;
1198 if (*buflen >= len) strcpyW(buffer, path);
1199 else ret = PDH_MORE_DATA;
1204 /***********************************************************************
1205 * PdhEnumObjectItemsA (PDH.@)
1207 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1208 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1209 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1211 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1212 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1213 pcchInstanceListLength, dwDetailLevel, dwFlags);
1215 return PDH_NOT_IMPLEMENTED;
1218 /***********************************************************************
1219 * PdhEnumObjectItemsW (PDH.@)
1221 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1222 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1223 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1225 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1226 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1227 pcchInstanceListLength, dwDetailLevel, dwFlags);
1229 return PDH_NOT_IMPLEMENTED;
1232 /***********************************************************************
1233 * PdhSetDefaultRealTimeDataSource (PDH.@)
1235 PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1237 FIXME("%u\n", source);
1238 return ERROR_SUCCESS;