pdh: Add PdhGetLogFileTypeA/W stubs.
[wine.git] / dlls / pdh / pdh_main.c
blob036d28b47d9caa60c4189893cb8274b94c0ad06d
1 /*
2 * Performance Data Helper (pdh.dll)
4 * Copyright 2007 Andrey Turkin
5 * Copyright 2007 Hans Leidekker
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <stdarg.h>
23 #include <math.h>
25 #define NONAMELESSUNION
27 #include "windef.h"
28 #include "winbase.h"
30 #include "pdh.h"
31 #include "pdhmsg.h"
32 #include "winperf.h"
34 #include "wine/debug.h"
35 #include "wine/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 =
43 0, 0, &pdh_handle_cs,
44 { &pdh_handle_cs_debug.ProcessLocksList,
45 &pdh_handle_cs_debug.ProcessLocksList },
46 0, 0, { (DWORD_PTR)(__FILE__ ": pdh_handle_cs") }
48 static CRITICAL_SECTION pdh_handle_cs = { &pdh_handle_cs_debug, -1, 0, 0, 0, 0 };
50 static inline 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 )
67 WCHAR *dst;
69 if (!src) return NULL;
70 if ((dst = heap_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src );
71 return dst;
74 static inline WCHAR *pdh_strdup_aw( const char *src )
76 int len;
77 WCHAR *dst;
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 );
82 return dst;
85 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
87 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
88 switch (fdwReason)
90 case DLL_WINE_PREATTACH:
91 return FALSE; /* prefer native version */
92 case DLL_PROCESS_ATTACH:
93 DisableThreadLibraryCalls(hinstDLL);
94 break;
95 case DLL_PROCESS_DETACH:
96 if (lpvReserved) break;
97 DeleteCriticalSection(&pdh_handle_cs);
98 break;
101 return TRUE;
104 union value
106 LONG longvalue;
107 double doublevalue;
108 LONGLONG largevalue;
111 struct counter
113 DWORD magic; /* signature */
114 struct list entry; /* list entry */
115 WCHAR *path; /* identifier */
116 DWORD type; /* counter type */
117 DWORD status; /* update status */
118 LONG scale; /* scale factor */
119 LONG defaultscale; /* default scale factor */
120 DWORD_PTR user; /* user data */
121 DWORD_PTR queryuser; /* query user data */
122 LONGLONG base; /* samples per second */
123 FILETIME stamp; /* time stamp */
124 void (CALLBACK *collect)( struct counter * ); /* collect callback */
125 union value one; /* first value */
126 union value two; /* second value */
129 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
131 static struct counter *create_counter( void )
133 struct counter *counter;
135 if ((counter = heap_alloc_zero( sizeof(struct counter) )))
137 counter->magic = PDH_MAGIC_COUNTER;
138 return counter;
140 return NULL;
143 static void destroy_counter( struct counter *counter )
145 counter->magic = 0;
146 heap_free( counter->path );
147 heap_free( counter );
150 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
152 struct query
154 DWORD magic; /* signature */
155 DWORD_PTR user; /* user data */
156 HANDLE thread; /* collect thread */
157 DWORD interval; /* collect interval */
158 HANDLE wait; /* wait event */
159 HANDLE stop; /* stop event */
160 struct list counters; /* counter list */
163 static struct query *create_query( void )
165 struct query *query;
167 if ((query = heap_alloc_zero( sizeof(struct query) )))
169 query->magic = PDH_MAGIC_QUERY;
170 list_init( &query->counters );
171 return query;
173 return NULL;
176 static void destroy_query( struct query *query )
178 query->magic = 0;
179 heap_free( query );
182 struct source
184 DWORD index; /* name index */
185 const WCHAR *path; /* identifier */
186 void (CALLBACK *collect)( struct counter * ); /* collect callback */
187 DWORD type; /* counter type */
188 LONG scale; /* default scale factor */
189 LONGLONG base; /* samples per second */
192 static const WCHAR path_processor_time[] =
193 {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
194 '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
195 static const WCHAR path_uptime[] =
196 {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
198 static void CALLBACK collect_processor_time( struct counter *counter )
200 counter->two.largevalue = 500000; /* FIXME */
201 counter->status = PDH_CSTATUS_VALID_DATA;
204 static void CALLBACK collect_uptime( struct counter *counter )
206 counter->two.largevalue = GetTickCount64();
207 counter->status = PDH_CSTATUS_VALID_DATA;
210 #define TYPE_PROCESSOR_TIME \
211 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
212 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
214 #define TYPE_UPTIME \
215 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
217 /* counter source registry */
218 static const struct source counter_sources[] =
220 { 6, path_processor_time, collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 },
221 { 674, path_uptime, collect_uptime, TYPE_UPTIME, -3, 1000 }
224 static BOOL is_local_machine( const WCHAR *name, DWORD len )
226 WCHAR buf[MAX_COMPUTERNAME_LENGTH + 1];
227 DWORD buflen = sizeof(buf) / sizeof(buf[0]);
229 if (!GetComputerNameW( buf, &buflen )) return FALSE;
230 return len == buflen && !memicmpW( name, buf, buflen );
233 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
235 const WCHAR *p;
237 if (path[0] == '\\' && path[1] == '\\' && (p = strchrW( path + 2, '\\' )) &&
238 is_local_machine( path + 2, p - path - 2 ))
240 path += p - path;
242 if (strchrW( path, '\\' )) p = fullpath;
243 else p = strrchrW( fullpath, '\\' ) + 1;
244 return !strcmpW( p, path );
247 /***********************************************************************
248 * PdhAddCounterA (PDH.@)
250 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
251 DWORD_PTR userdata, PDH_HCOUNTER *counter )
253 PDH_STATUS ret;
254 WCHAR *pathW;
256 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
258 if (!path) return PDH_INVALID_ARGUMENT;
260 if (!(pathW = pdh_strdup_aw( path )))
261 return PDH_MEMORY_ALLOCATION_FAILURE;
263 ret = PdhAddCounterW( query, pathW, userdata, counter );
265 heap_free( pathW );
266 return ret;
269 /***********************************************************************
270 * PdhAddCounterW (PDH.@)
272 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
273 DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
275 struct query *query = hquery;
276 struct counter *counter;
277 unsigned int i;
279 TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
281 if (!path || !hcounter) return PDH_INVALID_ARGUMENT;
283 EnterCriticalSection( &pdh_handle_cs );
284 if (!query || query->magic != PDH_MAGIC_QUERY)
286 LeaveCriticalSection( &pdh_handle_cs );
287 return PDH_INVALID_HANDLE;
290 *hcounter = NULL;
291 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
293 if (pdh_match_path( counter_sources[i].path, path ))
295 if ((counter = create_counter()))
297 counter->path = pdh_strdup( counter_sources[i].path );
298 counter->collect = counter_sources[i].collect;
299 counter->type = counter_sources[i].type;
300 counter->defaultscale = counter_sources[i].scale;
301 counter->base = counter_sources[i].base;
302 counter->queryuser = query->user;
303 counter->user = userdata;
305 list_add_tail( &query->counters, &counter->entry );
306 *hcounter = counter;
308 LeaveCriticalSection( &pdh_handle_cs );
309 return ERROR_SUCCESS;
311 LeaveCriticalSection( &pdh_handle_cs );
312 return PDH_MEMORY_ALLOCATION_FAILURE;
315 LeaveCriticalSection( &pdh_handle_cs );
316 return PDH_CSTATUS_NO_COUNTER;
319 /***********************************************************************
320 * PdhAddEnglishCounterA (PDH.@)
322 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
323 DWORD_PTR userdata, PDH_HCOUNTER *counter )
325 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
327 if (!query) return PDH_INVALID_ARGUMENT;
328 return PdhAddCounterA( query, path, userdata, counter );
331 /***********************************************************************
332 * PdhAddEnglishCounterW (PDH.@)
334 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
335 DWORD_PTR userdata, PDH_HCOUNTER *counter )
337 TRACE("%p %s %lx %p\n", query, debugstr_w(path), userdata, counter);
339 if (!query) return PDH_INVALID_ARGUMENT;
340 return PdhAddCounterW( query, path, userdata, counter );
343 /* caller must hold counter lock */
344 static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
345 union value *raw2, PDH_FMT_COUNTERVALUE *value )
347 LONG factor;
349 factor = counter->scale ? counter->scale : counter->defaultscale;
350 if (format & PDH_FMT_LONG)
352 if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000;
353 else value->u.longValue = raw2->longvalue * pow( 10, factor );
355 else if (format & PDH_FMT_LARGE)
357 if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000;
358 else value->u.largeValue = raw2->largevalue * pow( 10, factor );
360 else if (format & PDH_FMT_DOUBLE)
362 if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000;
363 else value->u.doubleValue = raw2->doublevalue * pow( 10, factor );
365 else
367 WARN("unknown format %x\n", format);
368 return PDH_INVALID_ARGUMENT;
370 return ERROR_SUCCESS;
373 /***********************************************************************
374 * PdhCalculateCounterFromRawValue (PDH.@)
376 PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
377 PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
378 PPDH_FMT_COUNTERVALUE value )
380 PDH_STATUS ret;
381 struct counter *counter = handle;
383 TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value);
385 if (!value) return PDH_INVALID_ARGUMENT;
387 EnterCriticalSection( &pdh_handle_cs );
388 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
390 LeaveCriticalSection( &pdh_handle_cs );
391 return PDH_INVALID_HANDLE;
394 ret = format_value( counter, format, (union value *)&raw1->SecondValue,
395 (union value *)&raw2->SecondValue, value );
397 LeaveCriticalSection( &pdh_handle_cs );
398 return ret;
402 /***********************************************************************
403 * PdhCloseQuery (PDH.@)
405 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
407 struct query *query = handle;
408 struct list *item, *next;
410 TRACE("%p\n", handle);
412 EnterCriticalSection( &pdh_handle_cs );
413 if (!query || query->magic != PDH_MAGIC_QUERY)
415 LeaveCriticalSection( &pdh_handle_cs );
416 return PDH_INVALID_HANDLE;
419 if (query->thread)
421 HANDLE thread = query->thread;
422 SetEvent( query->stop );
423 LeaveCriticalSection( &pdh_handle_cs );
425 WaitForSingleObject( thread, INFINITE );
427 EnterCriticalSection( &pdh_handle_cs );
428 if (query->magic != PDH_MAGIC_QUERY)
430 LeaveCriticalSection( &pdh_handle_cs );
431 return ERROR_SUCCESS;
433 CloseHandle( query->stop );
434 CloseHandle( query->thread );
435 query->thread = NULL;
438 LIST_FOR_EACH_SAFE( item, next, &query->counters )
440 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
442 list_remove( &counter->entry );
443 destroy_counter( counter );
446 destroy_query( query );
448 LeaveCriticalSection( &pdh_handle_cs );
449 return ERROR_SUCCESS;
452 /* caller must hold query lock */
453 static void collect_query_data( struct query *query )
455 struct list *item;
457 LIST_FOR_EACH( item, &query->counters )
459 SYSTEMTIME time;
460 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
462 counter->collect( counter );
464 GetLocalTime( &time );
465 SystemTimeToFileTime( &time, &counter->stamp );
469 /***********************************************************************
470 * PdhCollectQueryData (PDH.@)
472 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
474 struct query *query = handle;
476 TRACE("%p\n", handle);
478 EnterCriticalSection( &pdh_handle_cs );
479 if (!query || query->magic != PDH_MAGIC_QUERY)
481 LeaveCriticalSection( &pdh_handle_cs );
482 return PDH_INVALID_HANDLE;
485 if (list_empty( &query->counters ))
487 LeaveCriticalSection( &pdh_handle_cs );
488 return PDH_NO_DATA;
491 collect_query_data( query );
493 LeaveCriticalSection( &pdh_handle_cs );
494 return ERROR_SUCCESS;
497 static DWORD CALLBACK collect_query_thread( void *arg )
499 struct query *query = arg;
500 DWORD interval = query->interval;
501 HANDLE stop = query->stop;
503 for (;;)
505 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
507 EnterCriticalSection( &pdh_handle_cs );
508 if (query->magic != PDH_MAGIC_QUERY)
510 LeaveCriticalSection( &pdh_handle_cs );
511 ExitThread( PDH_INVALID_HANDLE );
514 collect_query_data( query );
516 if (!SetEvent( query->wait ))
518 LeaveCriticalSection( &pdh_handle_cs );
519 ExitThread( 0 );
521 LeaveCriticalSection( &pdh_handle_cs );
525 /***********************************************************************
526 * PdhCollectQueryDataEx (PDH.@)
528 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
530 PDH_STATUS ret;
531 struct query *query = handle;
533 TRACE("%p %d %p\n", handle, interval, event);
535 EnterCriticalSection( &pdh_handle_cs );
536 if (!query || query->magic != PDH_MAGIC_QUERY)
538 LeaveCriticalSection( &pdh_handle_cs );
539 return PDH_INVALID_HANDLE;
541 if (list_empty( &query->counters ))
543 LeaveCriticalSection( &pdh_handle_cs );
544 return PDH_NO_DATA;
546 if (query->thread)
548 HANDLE thread = query->thread;
549 SetEvent( query->stop );
550 LeaveCriticalSection( &pdh_handle_cs );
552 WaitForSingleObject( thread, INFINITE );
554 EnterCriticalSection( &pdh_handle_cs );
555 if (query->magic != PDH_MAGIC_QUERY)
557 LeaveCriticalSection( &pdh_handle_cs );
558 return PDH_INVALID_HANDLE;
560 CloseHandle( query->thread );
561 query->thread = NULL;
563 else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
565 ret = GetLastError();
566 LeaveCriticalSection( &pdh_handle_cs );
567 return ret;
569 query->wait = event;
570 query->interval = interval * 1000;
571 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
573 ret = GetLastError();
574 CloseHandle( query->stop );
576 LeaveCriticalSection( &pdh_handle_cs );
577 return ret;
580 LeaveCriticalSection( &pdh_handle_cs );
581 return ERROR_SUCCESS;
584 /***********************************************************************
585 * PdhCollectQueryDataWithTime (PDH.@)
587 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
589 struct query *query = handle;
590 struct counter *counter;
591 struct list *item;
593 TRACE("%p %p\n", handle, timestamp);
595 if (!timestamp) return PDH_INVALID_ARGUMENT;
597 EnterCriticalSection( &pdh_handle_cs );
598 if (!query || query->magic != PDH_MAGIC_QUERY)
600 LeaveCriticalSection( &pdh_handle_cs );
601 return PDH_INVALID_HANDLE;
603 if (list_empty( &query->counters ))
605 LeaveCriticalSection( &pdh_handle_cs );
606 return PDH_NO_DATA;
609 collect_query_data( query );
611 item = list_head( &query->counters );
612 counter = LIST_ENTRY( item, struct counter, entry );
614 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
616 LeaveCriticalSection( &pdh_handle_cs );
617 return ERROR_SUCCESS;
620 /***********************************************************************
621 * PdhExpandWildCardPathA (PDH.@)
623 PDH_STATUS WINAPI PdhExpandWildCardPathA( LPCSTR szDataSource, LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
625 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
626 return PDH_NOT_IMPLEMENTED;
629 /***********************************************************************
630 * PdhExpandWildCardPathW (PDH.@)
632 PDH_STATUS WINAPI PdhExpandWildCardPathW( LPCWSTR szDataSource, LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
634 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
635 return PDH_NOT_IMPLEMENTED;
638 /***********************************************************************
639 * PdhExpandCounterPathA (PDH.@)
641 PDH_STATUS WINAPI PdhExpandCounterPathA( LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength )
643 FIXME("%s, %p, %p: stub\n", debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength);
644 return PdhExpandWildCardPathA(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
647 /***********************************************************************
648 * PdhExpandCounterPathW (PDH.@)
650 PDH_STATUS WINAPI PdhExpandCounterPathW( LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength )
652 FIXME("%s, %p, %p: stub\n", debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength);
653 return PdhExpandWildCardPathW(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
656 /***********************************************************************
657 * PdhGetCounterInfoA (PDH.@)
659 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
661 struct counter *counter = handle;
663 TRACE("%p %d %p %p\n", handle, text, size, info);
665 EnterCriticalSection( &pdh_handle_cs );
666 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
668 LeaveCriticalSection( &pdh_handle_cs );
669 return PDH_INVALID_HANDLE;
671 if (!size)
673 LeaveCriticalSection( &pdh_handle_cs );
674 return PDH_INVALID_ARGUMENT;
676 if (*size < sizeof(PDH_COUNTER_INFO_A))
678 *size = sizeof(PDH_COUNTER_INFO_A);
679 LeaveCriticalSection( &pdh_handle_cs );
680 return PDH_MORE_DATA;
683 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
685 info->dwType = counter->type;
686 info->CStatus = counter->status;
687 info->lScale = counter->scale;
688 info->lDefaultScale = counter->defaultscale;
689 info->dwUserData = counter->user;
690 info->dwQueryUserData = counter->queryuser;
692 *size = sizeof(PDH_COUNTER_INFO_A);
694 LeaveCriticalSection( &pdh_handle_cs );
695 return ERROR_SUCCESS;
698 /***********************************************************************
699 * PdhGetCounterInfoW (PDH.@)
701 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
703 struct counter *counter = handle;
705 TRACE("%p %d %p %p\n", handle, text, size, info);
707 EnterCriticalSection( &pdh_handle_cs );
708 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
710 LeaveCriticalSection( &pdh_handle_cs );
711 return PDH_INVALID_HANDLE;
713 if (!size)
715 LeaveCriticalSection( &pdh_handle_cs );
716 return PDH_INVALID_ARGUMENT;
718 if (*size < sizeof(PDH_COUNTER_INFO_W))
720 *size = sizeof(PDH_COUNTER_INFO_W);
721 LeaveCriticalSection( &pdh_handle_cs );
722 return PDH_MORE_DATA;
725 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
727 info->dwType = counter->type;
728 info->CStatus = counter->status;
729 info->lScale = counter->scale;
730 info->lDefaultScale = counter->defaultscale;
731 info->dwUserData = counter->user;
732 info->dwQueryUserData = counter->queryuser;
734 *size = sizeof(PDH_COUNTER_INFO_W);
736 LeaveCriticalSection( &pdh_handle_cs );
737 return ERROR_SUCCESS;
740 /***********************************************************************
741 * PdhGetCounterTimeBase (PDH.@)
743 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
745 struct counter *counter = handle;
747 TRACE("%p %p\n", handle, base);
749 if (!base) return PDH_INVALID_ARGUMENT;
751 EnterCriticalSection( &pdh_handle_cs );
752 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
754 LeaveCriticalSection( &pdh_handle_cs );
755 return PDH_INVALID_HANDLE;
758 *base = counter->base;
760 LeaveCriticalSection( &pdh_handle_cs );
761 return ERROR_SUCCESS;
764 /***********************************************************************
765 * PdhGetDllVersion (PDH.@)
767 PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
769 if (!version)
770 return PDH_INVALID_ARGUMENT;
772 *version = PDH_VERSION;
774 return ERROR_SUCCESS;
777 /***********************************************************************
778 * PdhGetFormattedCounterValue (PDH.@)
780 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
781 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
783 PDH_STATUS ret;
784 struct counter *counter = handle;
786 TRACE("%p %x %p %p\n", handle, format, type, value);
788 if (!value) return PDH_INVALID_ARGUMENT;
790 EnterCriticalSection( &pdh_handle_cs );
791 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
793 LeaveCriticalSection( &pdh_handle_cs );
794 return PDH_INVALID_HANDLE;
796 if (counter->status)
798 LeaveCriticalSection( &pdh_handle_cs );
799 return PDH_INVALID_DATA;
801 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
803 value->CStatus = ERROR_SUCCESS;
804 if (type) *type = counter->type;
807 LeaveCriticalSection( &pdh_handle_cs );
808 return ret;
811 /***********************************************************************
812 * PdhGetRawCounterValue (PDH.@)
814 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
815 PPDH_RAW_COUNTER value )
817 struct counter *counter = handle;
819 TRACE("%p %p %p\n", handle, type, value);
821 if (!value) return PDH_INVALID_ARGUMENT;
823 EnterCriticalSection( &pdh_handle_cs );
824 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
826 LeaveCriticalSection( &pdh_handle_cs );
827 return PDH_INVALID_HANDLE;
830 value->CStatus = counter->status;
831 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
832 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
833 value->FirstValue = counter->one.largevalue;
834 value->SecondValue = counter->two.largevalue;
835 value->MultiCount = 1; /* FIXME */
837 if (type) *type = counter->type;
839 LeaveCriticalSection( &pdh_handle_cs );
840 return ERROR_SUCCESS;
843 /***********************************************************************
844 * PdhLookupPerfIndexByNameA (PDH.@)
846 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
848 PDH_STATUS ret;
849 WCHAR *machineW = NULL;
850 WCHAR *nameW;
852 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
854 if (!name) return PDH_INVALID_ARGUMENT;
856 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
858 if (!(nameW = pdh_strdup_aw( name )))
859 return PDH_MEMORY_ALLOCATION_FAILURE;
861 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
863 heap_free( nameW );
864 heap_free( machineW );
865 return ret;
868 /***********************************************************************
869 * PdhLookupPerfIndexByNameW (PDH.@)
871 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
873 unsigned int i;
875 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
877 if (!name || !index) return PDH_INVALID_ARGUMENT;
879 if (machine)
881 FIXME("remote machine not supported\n");
882 return PDH_CSTATUS_NO_MACHINE;
884 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
886 if (pdh_match_path( counter_sources[i].path, name ))
888 *index = counter_sources[i].index;
889 return ERROR_SUCCESS;
892 return PDH_STRING_NOT_FOUND;
895 /***********************************************************************
896 * PdhLookupPerfNameByIndexA (PDH.@)
898 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
900 PDH_STATUS ret;
901 WCHAR *machineW = NULL;
902 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
903 DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR);
905 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
907 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
909 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
911 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
913 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
915 if (size && *size < required) ret = PDH_MORE_DATA;
916 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
917 if (size) *size = required;
919 heap_free( machineW );
920 return ret;
923 /***********************************************************************
924 * PdhLookupPerfNameByIndexW (PDH.@)
926 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
928 PDH_STATUS ret;
929 unsigned int i;
931 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
933 if (machine)
935 FIXME("remote machine not supported\n");
936 return PDH_CSTATUS_NO_MACHINE;
939 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
940 if (!index) return ERROR_SUCCESS;
942 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
944 if (counter_sources[i].index == index)
946 WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1;
947 unsigned int required = strlenW( p ) + 1;
949 if (*size < required) ret = PDH_MORE_DATA;
950 else
952 strcpyW( buffer, p );
953 ret = ERROR_SUCCESS;
955 *size = required;
956 return ret;
959 return PDH_INVALID_ARGUMENT;
962 /***********************************************************************
963 * PdhOpenQueryA (PDH.@)
965 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
967 PDH_STATUS ret;
968 WCHAR *sourceW = NULL;
970 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
972 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
974 ret = PdhOpenQueryW( sourceW, userdata, query );
975 heap_free( sourceW );
977 return ret;
980 /***********************************************************************
981 * PdhOpenQueryW (PDH.@)
983 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
985 struct query *query;
987 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
989 if (!handle) return PDH_INVALID_ARGUMENT;
991 if (source)
993 FIXME("log file data source not supported\n");
994 return PDH_INVALID_ARGUMENT;
996 if ((query = create_query()))
998 query->user = userdata;
999 *handle = query;
1001 return ERROR_SUCCESS;
1003 return PDH_MEMORY_ALLOCATION_FAILURE;
1006 /***********************************************************************
1007 * PdhRemoveCounter (PDH.@)
1009 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
1011 struct counter *counter = handle;
1013 TRACE("%p\n", handle);
1015 EnterCriticalSection( &pdh_handle_cs );
1016 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1018 LeaveCriticalSection( &pdh_handle_cs );
1019 return PDH_INVALID_HANDLE;
1022 list_remove( &counter->entry );
1023 destroy_counter( counter );
1025 LeaveCriticalSection( &pdh_handle_cs );
1026 return ERROR_SUCCESS;
1029 /***********************************************************************
1030 * PdhSetCounterScaleFactor (PDH.@)
1032 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
1034 struct counter *counter = handle;
1036 TRACE("%p\n", handle);
1038 EnterCriticalSection( &pdh_handle_cs );
1039 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1041 LeaveCriticalSection( &pdh_handle_cs );
1042 return PDH_INVALID_HANDLE;
1044 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
1046 LeaveCriticalSection( &pdh_handle_cs );
1047 return PDH_INVALID_ARGUMENT;
1050 counter->scale = factor;
1052 LeaveCriticalSection( &pdh_handle_cs );
1053 return ERROR_SUCCESS;
1056 /***********************************************************************
1057 * PdhValidatePathA (PDH.@)
1059 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1061 PDH_STATUS ret;
1062 WCHAR *pathW;
1064 TRACE("%s\n", debugstr_a(path));
1066 if (!path) return PDH_INVALID_ARGUMENT;
1067 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1069 ret = PdhValidatePathW( pathW );
1071 heap_free( pathW );
1072 return ret;
1075 static PDH_STATUS validate_path( LPCWSTR path )
1077 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1078 if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1079 return ERROR_SUCCESS;
1082 /***********************************************************************
1083 * PdhValidatePathW (PDH.@)
1085 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1087 PDH_STATUS ret;
1088 unsigned int i;
1090 TRACE("%s\n", debugstr_w(path));
1092 if ((ret = validate_path( path ))) return ret;
1094 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
1095 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1097 return PDH_CSTATUS_NO_COUNTER;
1100 /***********************************************************************
1101 * PdhValidatePathExA (PDH.@)
1103 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1105 TRACE("%p %s\n", source, debugstr_a(path));
1107 if (source)
1109 FIXME("log file data source not supported\n");
1110 return ERROR_SUCCESS;
1112 return PdhValidatePathA( path );
1115 /***********************************************************************
1116 * PdhValidatePathExW (PDH.@)
1118 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1120 TRACE("%p %s\n", source, debugstr_w(path));
1122 if (source)
1124 FIXME("log file data source not supported\n");
1125 return ERROR_SUCCESS;
1127 return PdhValidatePathW( path );
1130 /***********************************************************************
1131 * PdhMakeCounterPathA (PDH.@)
1133 PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1134 LPDWORD buflen, DWORD flags )
1136 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1137 PDH_COUNTER_PATH_ELEMENTS_W eW;
1138 WCHAR *bufferW;
1139 DWORD buflenW;
1141 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1143 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1145 memset( &eW, 0, sizeof(eW) );
1146 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1147 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1148 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1149 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1150 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1151 eW.dwInstanceIndex = e->dwInstanceIndex;
1153 buflenW = 0;
1154 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1155 if (ret == PDH_MORE_DATA)
1157 if ((bufferW = heap_alloc( buflenW * sizeof(WCHAR) )))
1159 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1161 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1162 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1163 else ret = PDH_MORE_DATA;
1164 *buflen = len;
1166 heap_free( bufferW );
1168 else
1169 ret = PDH_MEMORY_ALLOCATION_FAILURE;
1172 done:
1173 heap_free( eW.szMachineName );
1174 heap_free( eW.szObjectName );
1175 heap_free( eW.szInstanceName );
1176 heap_free( eW.szParentInstance );
1177 heap_free( eW.szCounterName );
1178 return ret;
1181 /***********************************************************************
1182 * PdhMakeCounterPathW (PDH.@)
1184 PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1185 LPDWORD buflen, DWORD flags )
1187 static const WCHAR bslash[] = {'\\',0};
1188 static const WCHAR fslash[] = {'/',0};
1189 static const WCHAR lparen[] = {'(',0};
1190 static const WCHAR rparen[] = {')',0};
1191 static const WCHAR fmt[] = {'#','%','u',0};
1193 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1194 PDH_STATUS ret = ERROR_SUCCESS;
1195 DWORD len;
1197 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1199 if (flags) FIXME("unimplemented flags 0x%08x\n", flags);
1201 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1202 return PDH_INVALID_ARGUMENT;
1204 path[0] = 0;
1205 if (e->szMachineName)
1207 strcatW(path, bslash);
1208 strcatW(path, bslash);
1209 strcatW(path, e->szMachineName);
1211 strcatW(path, bslash);
1212 strcatW(path, e->szObjectName);
1213 if (e->szInstanceName)
1215 strcatW(path, lparen);
1216 if (e->szParentInstance)
1218 strcatW(path, e->szParentInstance);
1219 strcatW(path, fslash);
1221 strcatW(path, e->szInstanceName);
1222 sprintfW(instance, fmt, e->dwInstanceIndex);
1223 strcatW(path, instance);
1224 strcatW(path, rparen);
1226 strcatW(path, bslash);
1227 strcatW(path, e->szCounterName);
1229 len = strlenW(path) + 1;
1230 if (*buflen >= len) strcpyW(buffer, path);
1231 else ret = PDH_MORE_DATA;
1232 *buflen = len;
1233 return ret;
1236 /***********************************************************************
1237 * PdhEnumObjectItemsA (PDH.@)
1239 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1240 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1241 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1243 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1244 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1245 pcchInstanceListLength, dwDetailLevel, dwFlags);
1247 return PDH_NOT_IMPLEMENTED;
1250 /***********************************************************************
1251 * PdhEnumObjectItemsW (PDH.@)
1253 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1254 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1255 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1257 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1258 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1259 pcchInstanceListLength, dwDetailLevel, dwFlags);
1261 return PDH_NOT_IMPLEMENTED;
1264 /***********************************************************************
1265 * PdhSetDefaultRealTimeDataSource (PDH.@)
1267 PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1269 FIXME("%u\n", source);
1270 return ERROR_SUCCESS;
1273 /***********************************************************************
1274 * PdhGetLogFileTypeA (PDH.@)
1276 PDH_STATUS WINAPI PdhGetLogFileTypeA(PDH_HLOG log, DWORD *type)
1278 FIXME("%p, %p: stub\n", log, type);
1279 return PDH_NOT_IMPLEMENTED;
1282 /***********************************************************************
1283 * PdhGetLogFileTypeW (PDH.@)
1285 PDH_STATUS WINAPI PdhGetLogFileTypeW(PDH_HLOG log, DWORD *type)
1287 FIXME("%p, %p: stub\n", log, type);
1288 return PDH_NOT_IMPLEMENTED;