comctl32/listview: Handle CCM_[G,S]ETVERSION in listview.
[wine] / dlls / pdh / pdh_main.c
1 /*
2  * Performance Data Helper (pdh.dll)
3  *
4  * Copyright 2007 Andrey Turkin
5  * Copyright 2007 Hans Leidekker
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include <stdarg.h>
23 #include <math.h>
24
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29
30 #include "pdh.h"
31 #include "pdhmsg.h"
32 #include "winperf.h"
33
34 #include "wine/debug.h"
35 #include "wine/list.h"
36 #include "wine/unicode.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(pdh);
39
40 static CRITICAL_SECTION pdh_handle_cs;
41 static CRITICAL_SECTION_DEBUG pdh_handle_cs_debug =
42 {
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") }
47 };
48 static CRITICAL_SECTION pdh_handle_cs = { &pdh_handle_cs_debug, -1, 0, 0, 0, 0 };
49
50 static inline void *heap_alloc( SIZE_T size )
51 {
52     return HeapAlloc( GetProcessHeap(), 0, size );
53 }
54
55 static inline void *heap_alloc_zero( SIZE_T size )
56 {
57     return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size );
58 }
59
60 static inline void heap_free( LPVOID mem )
61 {
62     HeapFree( GetProcessHeap(), 0, mem );
63 }
64
65 static inline WCHAR *pdh_strdup( const WCHAR *src )
66 {
67     WCHAR *dst;
68
69     if (!src) return NULL;
70     if ((dst = heap_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) ))) strcpyW( dst, src );
71     return dst;
72 }
73
74 static inline WCHAR *pdh_strdup_aw( const char *src )
75 {
76     int len;
77     WCHAR *dst;
78
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;
83 }
84
85 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
86 {
87     TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
88
89     if (fdwReason == DLL_WINE_PREATTACH) return FALSE;    /* prefer native version */
90
91     if (fdwReason == DLL_PROCESS_ATTACH)
92     {
93         DisableThreadLibraryCalls( hinstDLL );
94     }
95
96     return TRUE;
97 }
98
99 union value
100 {
101     LONG     longvalue;
102     double   doublevalue;
103     LONGLONG largevalue;
104 };
105
106 struct counter
107 {
108     DWORD           magic;                          /* signature */
109     struct list     entry;                          /* list entry */
110     WCHAR          *path;                           /* identifier */
111     DWORD           type;                           /* counter type */
112     DWORD           status;                         /* update status */
113     LONG            scale;                          /* scale factor */
114     LONG            defaultscale;                   /* default scale factor */
115     DWORD_PTR       user;                           /* user data */
116     DWORD_PTR       queryuser;                      /* query user data */
117     LONGLONG        base;                           /* samples per second */
118     FILETIME        stamp;                          /* time stamp */
119     void (CALLBACK *collect)( struct counter * );   /* collect callback */
120     union value     one;                            /* first value */
121     union value     two;                            /* second value */
122 };
123
124 #define PDH_MAGIC_COUNTER   0x50444831 /* 'PDH1' */
125
126 static struct counter *create_counter( void )
127 {
128     struct counter *counter;
129
130     if ((counter = heap_alloc_zero( sizeof(struct counter) )))
131     {
132         counter->magic = PDH_MAGIC_COUNTER;
133         return counter;
134     }
135     return NULL;
136 }
137
138 static void destroy_counter( struct counter *counter )
139 {
140     counter->magic = 0;
141     heap_free( counter->path );
142     heap_free( counter );
143 }
144
145 #define PDH_MAGIC_QUERY     0x50444830 /* 'PDH0' */
146
147 struct query
148 {
149     DWORD       magic;      /* signature */
150     DWORD_PTR   user;       /* user data */
151     HANDLE      thread;     /* collect thread */
152     DWORD       interval;   /* collect interval */
153     HANDLE      wait;       /* wait event */
154     HANDLE      stop;       /* stop event */
155     struct list counters;   /* counter list */
156 };
157
158 static struct query *create_query( void )
159 {
160     struct query *query;
161
162     if ((query = heap_alloc_zero( sizeof(struct query) )))
163     {
164         query->magic = PDH_MAGIC_QUERY;
165         list_init( &query->counters );
166         return query;
167     }
168     return NULL;
169 }
170
171 static void destroy_query( struct query *query )
172 {
173     query->magic = 0;
174     heap_free( query );
175 }
176
177 struct source
178 {
179     DWORD           index;                          /* name index */
180     const WCHAR    *path;                           /* identifier */
181     void (CALLBACK *collect)( struct counter * );   /* collect callback */
182     DWORD           type;                           /* counter type */
183     LONG            scale;                          /* default scale factor */
184     LONGLONG        base;                           /* samples per second */
185 };
186
187 static const WCHAR path_processor_time[] =
188     {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
189      '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
190 static const WCHAR path_uptime[] =
191     {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
192
193 static void CALLBACK collect_processor_time( struct counter *counter )
194 {
195     counter->two.largevalue = 500000; /* FIXME */
196     counter->status = PDH_CSTATUS_VALID_DATA;
197 }
198
199 static void CALLBACK collect_uptime( struct counter *counter )
200 {
201     counter->two.largevalue = GetTickCount64();
202     counter->status = PDH_CSTATUS_VALID_DATA;
203 }
204
205 #define TYPE_PROCESSOR_TIME \
206     (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
207      PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
208
209 #define TYPE_UPTIME \
210     (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
211
212 /* counter source registry */
213 static const struct source counter_sources[] =
214 {
215     { 6,    path_processor_time,    collect_processor_time,     TYPE_PROCESSOR_TIME,    -5,     10000000 },
216     { 674,  path_uptime,            collect_uptime,             TYPE_UPTIME,            -3,     1000 }
217 };
218
219 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
220 {
221     const WCHAR *p;
222
223     if (strchrW( path, '\\')) p = fullpath;
224     else p = strrchrW( fullpath, '\\' ) + 1;
225     if (strcmpW( p, path )) return FALSE;
226     return TRUE;
227 }
228
229 /***********************************************************************
230  *              PdhAddCounterA   (PDH.@)
231  */
232 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
233                                   DWORD_PTR userdata, PDH_HCOUNTER *counter )
234 {
235     PDH_STATUS ret;
236     WCHAR *pathW;
237
238     TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
239
240     if (!path) return PDH_INVALID_ARGUMENT;
241
242     if (!(pathW = pdh_strdup_aw( path )))
243         return PDH_MEMORY_ALLOCATION_FAILURE;
244
245     ret = PdhAddCounterW( query, pathW, userdata, counter );
246
247     heap_free( pathW );
248     return ret;
249 }
250
251 /***********************************************************************
252  *              PdhAddCounterW   (PDH.@)
253  */
254 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
255                                   DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
256 {
257     struct query *query = hquery;
258     struct counter *counter;
259     unsigned int i;
260
261     TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
262
263     if (!path  || !hcounter) return PDH_INVALID_ARGUMENT;
264
265     EnterCriticalSection( &pdh_handle_cs );
266     if (!query || query->magic != PDH_MAGIC_QUERY)
267     {
268         LeaveCriticalSection( &pdh_handle_cs );
269         return PDH_INVALID_HANDLE;
270     }
271
272     *hcounter = NULL;
273     for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
274     {
275         if (pdh_match_path( counter_sources[i].path, path ))
276         {
277             if ((counter = create_counter()))
278             {
279                 counter->path         = pdh_strdup( counter_sources[i].path );
280                 counter->collect      = counter_sources[i].collect;
281                 counter->type         = counter_sources[i].type;
282                 counter->defaultscale = counter_sources[i].scale;
283                 counter->base         = counter_sources[i].base;
284                 counter->queryuser    = query->user;
285                 counter->user         = userdata;
286
287                 list_add_tail( &query->counters, &counter->entry );
288                 *hcounter = counter;
289
290                 LeaveCriticalSection( &pdh_handle_cs );
291                 return ERROR_SUCCESS;
292             }
293             LeaveCriticalSection( &pdh_handle_cs );
294             return PDH_MEMORY_ALLOCATION_FAILURE;
295         }
296     }
297     LeaveCriticalSection( &pdh_handle_cs );
298     return PDH_CSTATUS_NO_COUNTER;
299 }
300
301 /***********************************************************************
302  *              PdhAddEnglishCounterA   (PDH.@)
303  */
304 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
305                                          DWORD_PTR userdata, PDH_HCOUNTER *counter )
306 {
307     TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
308
309     if (!query) return PDH_INVALID_ARGUMENT;
310     return PdhAddCounterA( query, path, userdata, counter );
311 }
312
313 /***********************************************************************
314  *              PdhAddEnglishCounterW   (PDH.@)
315  */
316 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
317                                          DWORD_PTR userdata, PDH_HCOUNTER *counter )
318 {
319     TRACE("%p %s %lx %p\n", query, debugstr_w(path), userdata, counter);
320
321     if (!query) return PDH_INVALID_ARGUMENT;
322     return PdhAddCounterW( query, path, userdata, counter );
323 }
324
325 /* caller must hold counter lock */
326 static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
327                                 union value *raw2, PDH_FMT_COUNTERVALUE *value )
328 {
329     LONG factor;
330
331     factor = counter->scale ? counter->scale : counter->defaultscale;
332     if (format & PDH_FMT_LONG)
333     {
334         if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000;
335         else value->u.longValue = raw2->longvalue * pow( 10, factor );
336     }
337     else if (format & PDH_FMT_LARGE)
338     {
339         if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000;
340         else value->u.largeValue = raw2->largevalue * pow( 10, factor );
341     }
342     else if (format & PDH_FMT_DOUBLE)
343     {
344         if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000;
345         else value->u.doubleValue = raw2->doublevalue * pow( 10, factor );
346     }
347     else
348     {
349         WARN("unknown format %x\n", format);
350         return PDH_INVALID_ARGUMENT;
351     }
352     return ERROR_SUCCESS;
353 }
354
355 /***********************************************************************
356  *              PdhCalculateCounterFromRawValue   (PDH.@)
357  */
358 PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
359                                                    PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
360                                                    PPDH_FMT_COUNTERVALUE value )
361 {
362     PDH_STATUS ret;
363     struct counter *counter = handle;
364
365     TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value);
366
367     if (!value) return PDH_INVALID_ARGUMENT;
368
369     EnterCriticalSection( &pdh_handle_cs );
370     if (!counter || counter->magic != PDH_MAGIC_COUNTER)
371     {
372         LeaveCriticalSection( &pdh_handle_cs );
373         return PDH_INVALID_HANDLE;
374     }
375
376     ret = format_value( counter, format, (union value *)&raw1->SecondValue,
377                                          (union value *)&raw2->SecondValue, value );
378
379     LeaveCriticalSection( &pdh_handle_cs );
380     return ret;
381 }
382
383
384 /***********************************************************************
385  *              PdhCloseQuery   (PDH.@)
386  */
387 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
388 {
389     struct query *query = handle;
390     struct list *item, *next;
391
392     TRACE("%p\n", handle);
393
394     EnterCriticalSection( &pdh_handle_cs );
395     if (!query || query->magic != PDH_MAGIC_QUERY)
396     {
397         LeaveCriticalSection( &pdh_handle_cs );
398         return PDH_INVALID_HANDLE;
399     }
400
401     if (query->thread)
402     {
403         HANDLE thread = query->thread;
404         SetEvent( query->stop );
405         LeaveCriticalSection( &pdh_handle_cs );
406
407         WaitForSingleObject( thread, INFINITE );
408
409         EnterCriticalSection( &pdh_handle_cs );
410         if (query->magic != PDH_MAGIC_QUERY)
411         {
412             LeaveCriticalSection( &pdh_handle_cs );
413             return ERROR_SUCCESS;
414         }
415         CloseHandle( query->stop );
416         CloseHandle( query->thread );
417         query->thread = NULL;
418     }
419
420     LIST_FOR_EACH_SAFE( item, next, &query->counters )
421     {
422         struct counter *counter = LIST_ENTRY( item, struct counter, entry );
423
424         list_remove( &counter->entry );
425         destroy_counter( counter );
426     }
427
428     destroy_query( query );
429
430     LeaveCriticalSection( &pdh_handle_cs );
431     return ERROR_SUCCESS;
432 }
433
434 /* caller must hold query lock */
435 static void collect_query_data( struct query *query )
436 {
437     struct list *item;
438
439     LIST_FOR_EACH( item, &query->counters )
440     {
441         SYSTEMTIME time;
442         struct counter *counter = LIST_ENTRY( item, struct counter, entry );
443
444         counter->collect( counter );
445
446         GetLocalTime( &time );
447         SystemTimeToFileTime( &time, &counter->stamp );
448     }
449 }
450
451 /***********************************************************************
452  *              PdhCollectQueryData   (PDH.@)
453  */
454 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
455 {
456     struct query *query = handle;
457
458     TRACE("%p\n", handle);
459
460     EnterCriticalSection( &pdh_handle_cs );
461     if (!query || query->magic != PDH_MAGIC_QUERY)
462     {
463         LeaveCriticalSection( &pdh_handle_cs );
464         return PDH_INVALID_HANDLE;
465     }
466
467     if (list_empty( &query->counters ))
468     {
469         LeaveCriticalSection( &pdh_handle_cs );
470         return PDH_NO_DATA;
471     }
472
473     collect_query_data( query );
474
475     LeaveCriticalSection( &pdh_handle_cs );
476     return ERROR_SUCCESS;
477 }
478
479 static DWORD CALLBACK collect_query_thread( void *arg )
480 {
481     struct query *query = arg;
482     DWORD interval = query->interval;
483     HANDLE stop = query->stop;
484
485     for (;;)
486     {
487         if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
488
489         EnterCriticalSection( &pdh_handle_cs );
490         if (query->magic != PDH_MAGIC_QUERY)
491         {
492             LeaveCriticalSection( &pdh_handle_cs );
493             ExitThread( PDH_INVALID_HANDLE );
494         }
495
496         collect_query_data( query );
497
498         if (!SetEvent( query->wait ))
499         {
500             LeaveCriticalSection( &pdh_handle_cs );
501             ExitThread( 0 );
502         }
503         LeaveCriticalSection( &pdh_handle_cs );
504     }
505 }
506
507 /***********************************************************************
508  *              PdhCollectQueryDataEx   (PDH.@)
509  */
510 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
511 {
512     PDH_STATUS ret;
513     struct query *query = handle;
514
515     TRACE("%p %d %p\n", handle, interval, event);
516
517     EnterCriticalSection( &pdh_handle_cs );
518     if (!query || query->magic != PDH_MAGIC_QUERY)
519     {
520         LeaveCriticalSection( &pdh_handle_cs );
521         return PDH_INVALID_HANDLE;
522     }
523     if (list_empty( &query->counters ))
524     {
525         LeaveCriticalSection( &pdh_handle_cs );
526         return PDH_NO_DATA;
527     }
528     if (query->thread)
529     {
530         HANDLE thread = query->thread;
531         SetEvent( query->stop );
532         LeaveCriticalSection( &pdh_handle_cs );
533
534         WaitForSingleObject( thread, INFINITE );
535
536         EnterCriticalSection( &pdh_handle_cs );
537         if (query->magic != PDH_MAGIC_QUERY)
538         {
539             LeaveCriticalSection( &pdh_handle_cs );
540             return PDH_INVALID_HANDLE;
541         }
542         CloseHandle( query->thread );
543         query->thread = NULL;
544     }
545     else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
546     {
547         ret = GetLastError();
548         LeaveCriticalSection( &pdh_handle_cs );
549         return ret;
550     }
551     query->wait = event;
552     query->interval = interval * 1000;
553     if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
554     {
555         ret = GetLastError();
556         CloseHandle( query->stop );
557
558         LeaveCriticalSection( &pdh_handle_cs );
559         return ret;
560     }
561
562     LeaveCriticalSection( &pdh_handle_cs );
563     return ERROR_SUCCESS;
564 }
565
566 /***********************************************************************
567  *              PdhCollectQueryDataWithTime   (PDH.@)
568  */
569 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
570 {
571     struct query *query = handle;
572     struct counter *counter;
573     struct list *item;
574
575     TRACE("%p %p\n", handle, timestamp);
576
577     if (!timestamp) return PDH_INVALID_ARGUMENT;
578
579     EnterCriticalSection( &pdh_handle_cs );
580     if (!query || query->magic != PDH_MAGIC_QUERY)
581     {
582         LeaveCriticalSection( &pdh_handle_cs );
583         return PDH_INVALID_HANDLE;
584     }
585     if (list_empty( &query->counters ))
586     {
587         LeaveCriticalSection( &pdh_handle_cs );
588         return PDH_NO_DATA;
589     }
590
591     collect_query_data( query );
592
593     item = list_head( &query->counters );
594     counter = LIST_ENTRY( item, struct counter, entry );
595
596     *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
597
598     LeaveCriticalSection( &pdh_handle_cs );
599     return ERROR_SUCCESS;
600 }
601
602 /***********************************************************************
603  *              PdhGetCounterInfoA   (PDH.@)
604  */
605 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
606 {
607     struct counter *counter = handle;
608
609     TRACE("%p %d %p %p\n", handle, text, size, info);
610
611     EnterCriticalSection( &pdh_handle_cs );
612     if (!counter || counter->magic != PDH_MAGIC_COUNTER)
613     {
614         LeaveCriticalSection( &pdh_handle_cs );
615         return PDH_INVALID_HANDLE;
616     }
617     if (!size)
618     {
619         LeaveCriticalSection( &pdh_handle_cs );
620         return PDH_INVALID_ARGUMENT;
621     }
622     if (*size < sizeof(PDH_COUNTER_INFO_A))
623     {
624         *size = sizeof(PDH_COUNTER_INFO_A);
625         LeaveCriticalSection( &pdh_handle_cs );
626         return PDH_MORE_DATA;
627     }
628
629     memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
630
631     info->dwType          = counter->type;
632     info->CStatus         = counter->status;
633     info->lScale          = counter->scale;
634     info->lDefaultScale   = counter->defaultscale;
635     info->dwUserData      = counter->user;
636     info->dwQueryUserData = counter->queryuser;
637
638     *size = sizeof(PDH_COUNTER_INFO_A);
639
640     LeaveCriticalSection( &pdh_handle_cs );
641     return ERROR_SUCCESS;
642 }
643
644 /***********************************************************************
645  *              PdhGetCounterInfoW   (PDH.@)
646  */
647 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
648 {
649     struct counter *counter = handle;
650
651     TRACE("%p %d %p %p\n", handle, text, size, info);
652
653     EnterCriticalSection( &pdh_handle_cs );
654     if (!counter || counter->magic != PDH_MAGIC_COUNTER)
655     {
656         LeaveCriticalSection( &pdh_handle_cs );
657         return PDH_INVALID_HANDLE;
658     }
659     if (!size)
660     {
661         LeaveCriticalSection( &pdh_handle_cs );
662         return PDH_INVALID_ARGUMENT;
663     }
664     if (*size < sizeof(PDH_COUNTER_INFO_W))
665     {
666         *size = sizeof(PDH_COUNTER_INFO_W);
667         LeaveCriticalSection( &pdh_handle_cs );
668         return PDH_MORE_DATA;
669     }
670
671     memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
672
673     info->dwType          = counter->type;
674     info->CStatus         = counter->status;
675     info->lScale          = counter->scale;
676     info->lDefaultScale   = counter->defaultscale;
677     info->dwUserData      = counter->user;
678     info->dwQueryUserData = counter->queryuser;
679
680     *size = sizeof(PDH_COUNTER_INFO_W);
681
682     LeaveCriticalSection( &pdh_handle_cs );
683     return ERROR_SUCCESS;
684 }
685
686 /***********************************************************************
687  *              PdhGetCounterTimeBase   (PDH.@)
688  */
689 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
690 {
691     struct counter *counter = handle;
692
693     TRACE("%p %p\n", handle, base);
694
695     if (!base) return PDH_INVALID_ARGUMENT;
696
697     EnterCriticalSection( &pdh_handle_cs );
698     if (!counter || counter->magic != PDH_MAGIC_COUNTER)
699     {
700         LeaveCriticalSection( &pdh_handle_cs );
701         return PDH_INVALID_HANDLE;
702     }
703
704     *base = counter->base;
705
706     LeaveCriticalSection( &pdh_handle_cs );
707     return ERROR_SUCCESS;
708 }
709
710 /***********************************************************************
711  *              PdhGetFormattedCounterValue   (PDH.@)
712  */
713 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
714                                                LPDWORD type, PPDH_FMT_COUNTERVALUE value )
715 {
716     PDH_STATUS ret;
717     struct counter *counter = handle;
718
719     TRACE("%p %x %p %p\n", handle, format, type, value);
720
721     if (!value) return PDH_INVALID_ARGUMENT;
722
723     EnterCriticalSection( &pdh_handle_cs );
724     if (!counter || counter->magic != PDH_MAGIC_COUNTER)
725     {
726         LeaveCriticalSection( &pdh_handle_cs );
727         return PDH_INVALID_HANDLE;
728     }
729     if (counter->status)
730     {
731         LeaveCriticalSection( &pdh_handle_cs );
732         return PDH_INVALID_DATA;
733     }
734     if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
735     {
736         value->CStatus = ERROR_SUCCESS;
737         if (type) *type = counter->type;
738     }
739
740     LeaveCriticalSection( &pdh_handle_cs );
741     return ret;
742 }
743
744 /***********************************************************************
745  *              PdhGetRawCounterValue   (PDH.@)
746  */
747 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
748                                          PPDH_RAW_COUNTER value )
749 {
750     struct counter *counter = handle;
751
752     TRACE("%p %p %p\n", handle, type, value);
753
754     if (!value) return PDH_INVALID_ARGUMENT;
755
756     EnterCriticalSection( &pdh_handle_cs );
757     if (!counter || counter->magic != PDH_MAGIC_COUNTER)
758     {
759         LeaveCriticalSection( &pdh_handle_cs );
760         return PDH_INVALID_HANDLE;
761     }
762
763     value->CStatus                  = counter->status;
764     value->TimeStamp.dwLowDateTime  = counter->stamp.dwLowDateTime;
765     value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
766     value->FirstValue               = counter->one.largevalue;
767     value->SecondValue              = counter->two.largevalue;
768     value->MultiCount               = 1; /* FIXME */
769
770     if (type) *type = counter->type;
771
772     LeaveCriticalSection( &pdh_handle_cs );
773     return ERROR_SUCCESS;
774 }
775
776 /***********************************************************************
777  *              PdhLookupPerfIndexByNameA   (PDH.@)
778  */
779 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
780 {
781     PDH_STATUS ret;
782     WCHAR *machineW = NULL;
783     WCHAR *nameW;
784
785     TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
786
787     if (!name) return PDH_INVALID_ARGUMENT;
788
789     if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
790
791     if (!(nameW = pdh_strdup_aw( name )))
792         return PDH_MEMORY_ALLOCATION_FAILURE;
793
794     ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
795
796     heap_free( nameW );
797     heap_free( machineW );
798     return ret;
799 }
800
801 /***********************************************************************
802  *              PdhLookupPerfIndexByNameW   (PDH.@)
803  */
804 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
805 {
806     unsigned int i;
807
808     TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
809
810     if (!name || !index) return PDH_INVALID_ARGUMENT;
811
812     if (machine)
813     {
814         FIXME("remote machine not supported\n");
815         return PDH_CSTATUS_NO_MACHINE;
816     }
817     for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
818     {
819         if (pdh_match_path( counter_sources[i].path, name ))
820         {
821             *index = counter_sources[i].index;
822             return ERROR_SUCCESS;
823         }
824     }
825     return PDH_STRING_NOT_FOUND;
826 }
827
828 /***********************************************************************
829  *              PdhLookupPerfNameByIndexA   (PDH.@)
830  */
831 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
832 {
833     PDH_STATUS ret;
834     WCHAR *machineW = NULL;
835     WCHAR bufferW[PDH_MAX_COUNTER_NAME];
836     DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR);
837
838     TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
839
840     if (!buffer || !size) return PDH_INVALID_ARGUMENT;
841
842     if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
843
844     if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
845     {
846         int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
847
848         if (size && *size < required) ret = PDH_MORE_DATA;
849         else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
850         if (size) *size = required;
851     }
852     heap_free( machineW );
853     return ret;
854 }
855
856 /***********************************************************************
857  *              PdhLookupPerfNameByIndexW   (PDH.@)
858  */
859 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
860 {
861     PDH_STATUS ret;
862     unsigned int i;
863
864     TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
865
866     if (machine)
867     {
868         FIXME("remote machine not supported\n");
869         return PDH_CSTATUS_NO_MACHINE;
870     }
871
872     if (!buffer || !size) return PDH_INVALID_ARGUMENT;
873     if (!index) return ERROR_SUCCESS;
874
875     for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
876     {
877         if (counter_sources[i].index == index)
878         {
879             WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1;
880             unsigned int required = strlenW( p ) + 1;
881
882             if (*size < required) ret = PDH_MORE_DATA;
883             else
884             {
885                 strcpyW( buffer, p );
886                 ret = ERROR_SUCCESS;
887             }
888             *size = required;
889             return ret;
890         }
891     }
892     return PDH_INVALID_ARGUMENT;
893 }
894
895 /***********************************************************************
896  *              PdhOpenQueryA   (PDH.@)
897  */
898 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
899 {
900     PDH_STATUS ret;
901     WCHAR *sourceW = NULL;
902
903     TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
904
905     if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
906
907     ret = PdhOpenQueryW( sourceW, userdata, query );
908     heap_free( sourceW );
909
910     return ret;
911 }
912
913 /***********************************************************************
914  *              PdhOpenQueryW   (PDH.@)
915  */
916 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
917 {
918     struct query *query;
919
920     TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
921
922     if (!handle) return PDH_INVALID_ARGUMENT;
923
924     if (source)
925     {
926         FIXME("log file data source not supported\n");
927         return PDH_INVALID_ARGUMENT;
928     }
929     if ((query = create_query()))
930     {
931         query->user = userdata;
932         *handle = query;
933
934         return ERROR_SUCCESS;
935     }
936     return PDH_MEMORY_ALLOCATION_FAILURE;
937 }
938
939 /***********************************************************************
940  *              PdhRemoveCounter   (PDH.@)
941  */
942 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
943 {
944     struct counter *counter = handle;
945
946     TRACE("%p\n", handle);
947
948     EnterCriticalSection( &pdh_handle_cs );
949     if (!counter || counter->magic != PDH_MAGIC_COUNTER)
950     {
951         LeaveCriticalSection( &pdh_handle_cs );
952         return PDH_INVALID_HANDLE;
953     }
954
955     list_remove( &counter->entry );
956     destroy_counter( counter );
957
958     LeaveCriticalSection( &pdh_handle_cs );
959     return ERROR_SUCCESS;
960 }
961
962 /***********************************************************************
963  *              PdhSetCounterScaleFactor   (PDH.@)
964  */
965 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
966 {
967     struct counter *counter = handle;
968
969     TRACE("%p\n", handle);
970
971     EnterCriticalSection( &pdh_handle_cs );
972     if (!counter || counter->magic != PDH_MAGIC_COUNTER)
973     {
974         LeaveCriticalSection( &pdh_handle_cs );
975         return PDH_INVALID_HANDLE;
976     }
977     if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
978     {
979         LeaveCriticalSection( &pdh_handle_cs );
980         return PDH_INVALID_ARGUMENT;
981     }
982
983     counter->scale = factor;
984
985     LeaveCriticalSection( &pdh_handle_cs );
986     return ERROR_SUCCESS;
987 }
988
989 /***********************************************************************
990  *              PdhValidatePathA   (PDH.@)
991  */
992 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
993 {
994     PDH_STATUS ret;
995     WCHAR *pathW;
996
997     TRACE("%s\n", debugstr_a(path));
998
999     if (!path) return PDH_INVALID_ARGUMENT;
1000     if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1001
1002     ret = PdhValidatePathW( pathW );
1003
1004     heap_free( pathW );
1005     return ret;
1006 }
1007
1008 static PDH_STATUS validate_path( LPCWSTR path )
1009 {
1010     if (!path || !*path) return PDH_INVALID_ARGUMENT;
1011     if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1012     return ERROR_SUCCESS;
1013  }
1014
1015 /***********************************************************************
1016  *              PdhValidatePathW   (PDH.@)
1017  */
1018 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1019 {
1020     PDH_STATUS ret;
1021     unsigned int i;
1022
1023     TRACE("%s\n", debugstr_w(path));
1024
1025     if ((ret = validate_path( path ))) return ret;
1026
1027     for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
1028         if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1029
1030     return PDH_CSTATUS_NO_COUNTER;
1031 }
1032
1033 /***********************************************************************
1034  *              PdhValidatePathExA   (PDH.@)
1035  */
1036 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1037 {
1038     TRACE("%p %s\n", source, debugstr_a(path));
1039
1040     if (source)
1041     {
1042         FIXME("log file data source not supported\n");
1043         return ERROR_SUCCESS;
1044     }
1045     return PdhValidatePathA( path );
1046 }
1047
1048 /***********************************************************************
1049  *              PdhValidatePathExW   (PDH.@)
1050  */
1051 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1052 {
1053     TRACE("%p %s\n", source, debugstr_w(path));
1054
1055     if (source)
1056     {
1057         FIXME("log file data source not supported\n");
1058         return ERROR_SUCCESS;
1059     }
1060     return PdhValidatePathW( path );
1061 }
1062
1063 /***********************************************************************
1064  *              PdhEnumObjectItemsA   (PDH.@)
1065  */
1066 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1067                                       LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1068                                       LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1069 {
1070     FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1071          debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1072          pcchInstanceListLength, dwDetailLevel, dwFlags);
1073
1074     return PDH_NOT_IMPLEMENTED;
1075 }
1076
1077 /***********************************************************************
1078  *              PdhEnumObjectItemsW   (PDH.@)
1079  */
1080 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1081                                       LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1082                                       LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1083 {
1084     FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1085          debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1086          pcchInstanceListLength, dwDetailLevel, dwFlags);
1087
1088     return PDH_NOT_IMPLEMENTED;
1089 }