server: Set appropriate status for a terminated thread.
[wine.git] / dlls / pdh / pdh_main.c
blobf1a34e7e46f0e2d2c0bea1b199317ee4fcf101e6
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
26 #define NONAMELESSSTRUCT
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 DeleteCriticalSection(&pdh_handle_cs);
97 break;
100 return TRUE;
103 union value
105 LONG longvalue;
106 double doublevalue;
107 LONGLONG largevalue;
110 struct counter
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;
137 return counter;
139 return NULL;
142 static void destroy_counter( struct counter *counter )
144 counter->magic = 0;
145 heap_free( counter->path );
146 heap_free( counter );
149 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
151 struct query
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 )
164 struct query *query;
166 if ((query = heap_alloc_zero( sizeof(struct query) )))
168 query->magic = PDH_MAGIC_QUERY;
169 list_init( &query->counters );
170 return query;
172 return NULL;
175 static void destroy_query( struct query *query )
177 query->magic = 0;
178 heap_free( query );
181 struct source
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 is_local_machine( const WCHAR *name, DWORD len )
225 WCHAR buf[MAX_COMPUTERNAME_LENGTH + 1];
226 DWORD buflen = sizeof(buf) / sizeof(buf[0]);
228 if (!GetComputerNameW( buf, &buflen )) return FALSE;
229 return len == buflen && !memicmpW( name, buf, buflen );
232 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
234 const WCHAR *p;
236 if (path[0] == '\\' && path[1] == '\\' && (p = strchrW( path + 2, '\\' )) &&
237 is_local_machine( path + 2, p - path - 2 ))
239 path += p - path;
241 if (strchrW( path, '\\' )) p = fullpath;
242 else p = strrchrW( fullpath, '\\' ) + 1;
243 return !strcmpW( p, path );
246 /***********************************************************************
247 * PdhAddCounterA (PDH.@)
249 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
250 DWORD_PTR userdata, PDH_HCOUNTER *counter )
252 PDH_STATUS ret;
253 WCHAR *pathW;
255 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
257 if (!path) return PDH_INVALID_ARGUMENT;
259 if (!(pathW = pdh_strdup_aw( path )))
260 return PDH_MEMORY_ALLOCATION_FAILURE;
262 ret = PdhAddCounterW( query, pathW, userdata, counter );
264 heap_free( pathW );
265 return ret;
268 /***********************************************************************
269 * PdhAddCounterW (PDH.@)
271 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
272 DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
274 struct query *query = hquery;
275 struct counter *counter;
276 unsigned int i;
278 TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
280 if (!path || !hcounter) return PDH_INVALID_ARGUMENT;
282 EnterCriticalSection( &pdh_handle_cs );
283 if (!query || query->magic != PDH_MAGIC_QUERY)
285 LeaveCriticalSection( &pdh_handle_cs );
286 return PDH_INVALID_HANDLE;
289 *hcounter = NULL;
290 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
292 if (pdh_match_path( counter_sources[i].path, path ))
294 if ((counter = create_counter()))
296 counter->path = pdh_strdup( counter_sources[i].path );
297 counter->collect = counter_sources[i].collect;
298 counter->type = counter_sources[i].type;
299 counter->defaultscale = counter_sources[i].scale;
300 counter->base = counter_sources[i].base;
301 counter->queryuser = query->user;
302 counter->user = userdata;
304 list_add_tail( &query->counters, &counter->entry );
305 *hcounter = counter;
307 LeaveCriticalSection( &pdh_handle_cs );
308 return ERROR_SUCCESS;
310 LeaveCriticalSection( &pdh_handle_cs );
311 return PDH_MEMORY_ALLOCATION_FAILURE;
314 LeaveCriticalSection( &pdh_handle_cs );
315 return PDH_CSTATUS_NO_COUNTER;
318 /***********************************************************************
319 * PdhAddEnglishCounterA (PDH.@)
321 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
322 DWORD_PTR userdata, PDH_HCOUNTER *counter )
324 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
326 if (!query) return PDH_INVALID_ARGUMENT;
327 return PdhAddCounterA( query, path, userdata, counter );
330 /***********************************************************************
331 * PdhAddEnglishCounterW (PDH.@)
333 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
334 DWORD_PTR userdata, PDH_HCOUNTER *counter )
336 TRACE("%p %s %lx %p\n", query, debugstr_w(path), userdata, counter);
338 if (!query) return PDH_INVALID_ARGUMENT;
339 return PdhAddCounterW( query, path, userdata, counter );
342 /* caller must hold counter lock */
343 static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
344 union value *raw2, PDH_FMT_COUNTERVALUE *value )
346 LONG factor;
348 factor = counter->scale ? counter->scale : counter->defaultscale;
349 if (format & PDH_FMT_LONG)
351 if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000;
352 else value->u.longValue = raw2->longvalue * pow( 10, factor );
354 else if (format & PDH_FMT_LARGE)
356 if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000;
357 else value->u.largeValue = raw2->largevalue * pow( 10, factor );
359 else if (format & PDH_FMT_DOUBLE)
361 if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000;
362 else value->u.doubleValue = raw2->doublevalue * pow( 10, factor );
364 else
366 WARN("unknown format %x\n", format);
367 return PDH_INVALID_ARGUMENT;
369 return ERROR_SUCCESS;
372 /***********************************************************************
373 * PdhCalculateCounterFromRawValue (PDH.@)
375 PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
376 PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
377 PPDH_FMT_COUNTERVALUE value )
379 PDH_STATUS ret;
380 struct counter *counter = handle;
382 TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value);
384 if (!value) return PDH_INVALID_ARGUMENT;
386 EnterCriticalSection( &pdh_handle_cs );
387 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
389 LeaveCriticalSection( &pdh_handle_cs );
390 return PDH_INVALID_HANDLE;
393 ret = format_value( counter, format, (union value *)&raw1->SecondValue,
394 (union value *)&raw2->SecondValue, value );
396 LeaveCriticalSection( &pdh_handle_cs );
397 return ret;
401 /***********************************************************************
402 * PdhCloseQuery (PDH.@)
404 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
406 struct query *query = handle;
407 struct list *item, *next;
409 TRACE("%p\n", handle);
411 EnterCriticalSection( &pdh_handle_cs );
412 if (!query || query->magic != PDH_MAGIC_QUERY)
414 LeaveCriticalSection( &pdh_handle_cs );
415 return PDH_INVALID_HANDLE;
418 if (query->thread)
420 HANDLE thread = query->thread;
421 SetEvent( query->stop );
422 LeaveCriticalSection( &pdh_handle_cs );
424 WaitForSingleObject( thread, INFINITE );
426 EnterCriticalSection( &pdh_handle_cs );
427 if (query->magic != PDH_MAGIC_QUERY)
429 LeaveCriticalSection( &pdh_handle_cs );
430 return ERROR_SUCCESS;
432 CloseHandle( query->stop );
433 CloseHandle( query->thread );
434 query->thread = NULL;
437 LIST_FOR_EACH_SAFE( item, next, &query->counters )
439 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
441 list_remove( &counter->entry );
442 destroy_counter( counter );
445 destroy_query( query );
447 LeaveCriticalSection( &pdh_handle_cs );
448 return ERROR_SUCCESS;
451 /* caller must hold query lock */
452 static void collect_query_data( struct query *query )
454 struct list *item;
456 LIST_FOR_EACH( item, &query->counters )
458 SYSTEMTIME time;
459 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
461 counter->collect( counter );
463 GetLocalTime( &time );
464 SystemTimeToFileTime( &time, &counter->stamp );
468 /***********************************************************************
469 * PdhCollectQueryData (PDH.@)
471 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
473 struct query *query = handle;
475 TRACE("%p\n", handle);
477 EnterCriticalSection( &pdh_handle_cs );
478 if (!query || query->magic != PDH_MAGIC_QUERY)
480 LeaveCriticalSection( &pdh_handle_cs );
481 return PDH_INVALID_HANDLE;
484 if (list_empty( &query->counters ))
486 LeaveCriticalSection( &pdh_handle_cs );
487 return PDH_NO_DATA;
490 collect_query_data( query );
492 LeaveCriticalSection( &pdh_handle_cs );
493 return ERROR_SUCCESS;
496 static DWORD CALLBACK collect_query_thread( void *arg )
498 struct query *query = arg;
499 DWORD interval = query->interval;
500 HANDLE stop = query->stop;
502 for (;;)
504 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
506 EnterCriticalSection( &pdh_handle_cs );
507 if (query->magic != PDH_MAGIC_QUERY)
509 LeaveCriticalSection( &pdh_handle_cs );
510 ExitThread( PDH_INVALID_HANDLE );
513 collect_query_data( query );
515 if (!SetEvent( query->wait ))
517 LeaveCriticalSection( &pdh_handle_cs );
518 ExitThread( 0 );
520 LeaveCriticalSection( &pdh_handle_cs );
524 /***********************************************************************
525 * PdhCollectQueryDataEx (PDH.@)
527 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
529 PDH_STATUS ret;
530 struct query *query = handle;
532 TRACE("%p %d %p\n", handle, interval, event);
534 EnterCriticalSection( &pdh_handle_cs );
535 if (!query || query->magic != PDH_MAGIC_QUERY)
537 LeaveCriticalSection( &pdh_handle_cs );
538 return PDH_INVALID_HANDLE;
540 if (list_empty( &query->counters ))
542 LeaveCriticalSection( &pdh_handle_cs );
543 return PDH_NO_DATA;
545 if (query->thread)
547 HANDLE thread = query->thread;
548 SetEvent( query->stop );
549 LeaveCriticalSection( &pdh_handle_cs );
551 WaitForSingleObject( thread, INFINITE );
553 EnterCriticalSection( &pdh_handle_cs );
554 if (query->magic != PDH_MAGIC_QUERY)
556 LeaveCriticalSection( &pdh_handle_cs );
557 return PDH_INVALID_HANDLE;
559 CloseHandle( query->thread );
560 query->thread = NULL;
562 else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
564 ret = GetLastError();
565 LeaveCriticalSection( &pdh_handle_cs );
566 return ret;
568 query->wait = event;
569 query->interval = interval * 1000;
570 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
572 ret = GetLastError();
573 CloseHandle( query->stop );
575 LeaveCriticalSection( &pdh_handle_cs );
576 return ret;
579 LeaveCriticalSection( &pdh_handle_cs );
580 return ERROR_SUCCESS;
583 /***********************************************************************
584 * PdhCollectQueryDataWithTime (PDH.@)
586 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
588 struct query *query = handle;
589 struct counter *counter;
590 struct list *item;
592 TRACE("%p %p\n", handle, timestamp);
594 if (!timestamp) return PDH_INVALID_ARGUMENT;
596 EnterCriticalSection( &pdh_handle_cs );
597 if (!query || query->magic != PDH_MAGIC_QUERY)
599 LeaveCriticalSection( &pdh_handle_cs );
600 return PDH_INVALID_HANDLE;
602 if (list_empty( &query->counters ))
604 LeaveCriticalSection( &pdh_handle_cs );
605 return PDH_NO_DATA;
608 collect_query_data( query );
610 item = list_head( &query->counters );
611 counter = LIST_ENTRY( item, struct counter, entry );
613 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
615 LeaveCriticalSection( &pdh_handle_cs );
616 return ERROR_SUCCESS;
619 /***********************************************************************
620 * PdhExpandWildCardPathA (PDH.@)
622 PDH_STATUS WINAPI PdhExpandWildCardPathA( LPCSTR szDataSource, LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
624 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
625 return PDH_NOT_IMPLEMENTED;
628 /***********************************************************************
629 * PdhExpandWildCardPathW (PDH.@)
631 PDH_STATUS WINAPI PdhExpandWildCardPathW( LPCWSTR szDataSource, LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
633 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
634 return PDH_NOT_IMPLEMENTED;
637 /***********************************************************************
638 * PdhGetCounterInfoA (PDH.@)
640 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
642 struct counter *counter = handle;
644 TRACE("%p %d %p %p\n", handle, text, size, info);
646 EnterCriticalSection( &pdh_handle_cs );
647 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
649 LeaveCriticalSection( &pdh_handle_cs );
650 return PDH_INVALID_HANDLE;
652 if (!size)
654 LeaveCriticalSection( &pdh_handle_cs );
655 return PDH_INVALID_ARGUMENT;
657 if (*size < sizeof(PDH_COUNTER_INFO_A))
659 *size = sizeof(PDH_COUNTER_INFO_A);
660 LeaveCriticalSection( &pdh_handle_cs );
661 return PDH_MORE_DATA;
664 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
666 info->dwType = counter->type;
667 info->CStatus = counter->status;
668 info->lScale = counter->scale;
669 info->lDefaultScale = counter->defaultscale;
670 info->dwUserData = counter->user;
671 info->dwQueryUserData = counter->queryuser;
673 *size = sizeof(PDH_COUNTER_INFO_A);
675 LeaveCriticalSection( &pdh_handle_cs );
676 return ERROR_SUCCESS;
679 /***********************************************************************
680 * PdhGetCounterInfoW (PDH.@)
682 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
684 struct counter *counter = handle;
686 TRACE("%p %d %p %p\n", handle, text, size, info);
688 EnterCriticalSection( &pdh_handle_cs );
689 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
691 LeaveCriticalSection( &pdh_handle_cs );
692 return PDH_INVALID_HANDLE;
694 if (!size)
696 LeaveCriticalSection( &pdh_handle_cs );
697 return PDH_INVALID_ARGUMENT;
699 if (*size < sizeof(PDH_COUNTER_INFO_W))
701 *size = sizeof(PDH_COUNTER_INFO_W);
702 LeaveCriticalSection( &pdh_handle_cs );
703 return PDH_MORE_DATA;
706 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
708 info->dwType = counter->type;
709 info->CStatus = counter->status;
710 info->lScale = counter->scale;
711 info->lDefaultScale = counter->defaultscale;
712 info->dwUserData = counter->user;
713 info->dwQueryUserData = counter->queryuser;
715 *size = sizeof(PDH_COUNTER_INFO_W);
717 LeaveCriticalSection( &pdh_handle_cs );
718 return ERROR_SUCCESS;
721 /***********************************************************************
722 * PdhGetCounterTimeBase (PDH.@)
724 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
726 struct counter *counter = handle;
728 TRACE("%p %p\n", handle, base);
730 if (!base) return PDH_INVALID_ARGUMENT;
732 EnterCriticalSection( &pdh_handle_cs );
733 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
735 LeaveCriticalSection( &pdh_handle_cs );
736 return PDH_INVALID_HANDLE;
739 *base = counter->base;
741 LeaveCriticalSection( &pdh_handle_cs );
742 return ERROR_SUCCESS;
745 /***********************************************************************
746 * PdhGetDllVersion (PDH.@)
748 PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
750 if (!version)
751 return PDH_INVALID_ARGUMENT;
753 *version = PDH_VERSION;
755 return ERROR_SUCCESS;
758 /***********************************************************************
759 * PdhGetFormattedCounterValue (PDH.@)
761 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
762 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
764 PDH_STATUS ret;
765 struct counter *counter = handle;
767 TRACE("%p %x %p %p\n", handle, format, type, value);
769 if (!value) return PDH_INVALID_ARGUMENT;
771 EnterCriticalSection( &pdh_handle_cs );
772 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
774 LeaveCriticalSection( &pdh_handle_cs );
775 return PDH_INVALID_HANDLE;
777 if (counter->status)
779 LeaveCriticalSection( &pdh_handle_cs );
780 return PDH_INVALID_DATA;
782 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
784 value->CStatus = ERROR_SUCCESS;
785 if (type) *type = counter->type;
788 LeaveCriticalSection( &pdh_handle_cs );
789 return ret;
792 /***********************************************************************
793 * PdhGetRawCounterValue (PDH.@)
795 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
796 PPDH_RAW_COUNTER value )
798 struct counter *counter = handle;
800 TRACE("%p %p %p\n", handle, type, value);
802 if (!value) return PDH_INVALID_ARGUMENT;
804 EnterCriticalSection( &pdh_handle_cs );
805 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
807 LeaveCriticalSection( &pdh_handle_cs );
808 return PDH_INVALID_HANDLE;
811 value->CStatus = counter->status;
812 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
813 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
814 value->FirstValue = counter->one.largevalue;
815 value->SecondValue = counter->two.largevalue;
816 value->MultiCount = 1; /* FIXME */
818 if (type) *type = counter->type;
820 LeaveCriticalSection( &pdh_handle_cs );
821 return ERROR_SUCCESS;
824 /***********************************************************************
825 * PdhLookupPerfIndexByNameA (PDH.@)
827 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
829 PDH_STATUS ret;
830 WCHAR *machineW = NULL;
831 WCHAR *nameW;
833 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
835 if (!name) return PDH_INVALID_ARGUMENT;
837 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
839 if (!(nameW = pdh_strdup_aw( name )))
840 return PDH_MEMORY_ALLOCATION_FAILURE;
842 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
844 heap_free( nameW );
845 heap_free( machineW );
846 return ret;
849 /***********************************************************************
850 * PdhLookupPerfIndexByNameW (PDH.@)
852 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
854 unsigned int i;
856 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
858 if (!name || !index) return PDH_INVALID_ARGUMENT;
860 if (machine)
862 FIXME("remote machine not supported\n");
863 return PDH_CSTATUS_NO_MACHINE;
865 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
867 if (pdh_match_path( counter_sources[i].path, name ))
869 *index = counter_sources[i].index;
870 return ERROR_SUCCESS;
873 return PDH_STRING_NOT_FOUND;
876 /***********************************************************************
877 * PdhLookupPerfNameByIndexA (PDH.@)
879 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
881 PDH_STATUS ret;
882 WCHAR *machineW = NULL;
883 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
884 DWORD sizeW = sizeof(bufferW) / sizeof(WCHAR);
886 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
888 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
890 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
892 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
894 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
896 if (size && *size < required) ret = PDH_MORE_DATA;
897 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
898 if (size) *size = required;
900 heap_free( machineW );
901 return ret;
904 /***********************************************************************
905 * PdhLookupPerfNameByIndexW (PDH.@)
907 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
909 PDH_STATUS ret;
910 unsigned int i;
912 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
914 if (machine)
916 FIXME("remote machine not supported\n");
917 return PDH_CSTATUS_NO_MACHINE;
920 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
921 if (!index) return ERROR_SUCCESS;
923 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
925 if (counter_sources[i].index == index)
927 WCHAR *p = strrchrW( counter_sources[i].path, '\\' ) + 1;
928 unsigned int required = strlenW( p ) + 1;
930 if (*size < required) ret = PDH_MORE_DATA;
931 else
933 strcpyW( buffer, p );
934 ret = ERROR_SUCCESS;
936 *size = required;
937 return ret;
940 return PDH_INVALID_ARGUMENT;
943 /***********************************************************************
944 * PdhOpenQueryA (PDH.@)
946 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
948 PDH_STATUS ret;
949 WCHAR *sourceW = NULL;
951 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
953 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
955 ret = PdhOpenQueryW( sourceW, userdata, query );
956 heap_free( sourceW );
958 return ret;
961 /***********************************************************************
962 * PdhOpenQueryW (PDH.@)
964 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
966 struct query *query;
968 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
970 if (!handle) return PDH_INVALID_ARGUMENT;
972 if (source)
974 FIXME("log file data source not supported\n");
975 return PDH_INVALID_ARGUMENT;
977 if ((query = create_query()))
979 query->user = userdata;
980 *handle = query;
982 return ERROR_SUCCESS;
984 return PDH_MEMORY_ALLOCATION_FAILURE;
987 /***********************************************************************
988 * PdhRemoveCounter (PDH.@)
990 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
992 struct counter *counter = handle;
994 TRACE("%p\n", handle);
996 EnterCriticalSection( &pdh_handle_cs );
997 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
999 LeaveCriticalSection( &pdh_handle_cs );
1000 return PDH_INVALID_HANDLE;
1003 list_remove( &counter->entry );
1004 destroy_counter( counter );
1006 LeaveCriticalSection( &pdh_handle_cs );
1007 return ERROR_SUCCESS;
1010 /***********************************************************************
1011 * PdhSetCounterScaleFactor (PDH.@)
1013 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
1015 struct counter *counter = handle;
1017 TRACE("%p\n", handle);
1019 EnterCriticalSection( &pdh_handle_cs );
1020 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1022 LeaveCriticalSection( &pdh_handle_cs );
1023 return PDH_INVALID_HANDLE;
1025 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
1027 LeaveCriticalSection( &pdh_handle_cs );
1028 return PDH_INVALID_ARGUMENT;
1031 counter->scale = factor;
1033 LeaveCriticalSection( &pdh_handle_cs );
1034 return ERROR_SUCCESS;
1037 /***********************************************************************
1038 * PdhValidatePathA (PDH.@)
1040 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1042 PDH_STATUS ret;
1043 WCHAR *pathW;
1045 TRACE("%s\n", debugstr_a(path));
1047 if (!path) return PDH_INVALID_ARGUMENT;
1048 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1050 ret = PdhValidatePathW( pathW );
1052 heap_free( pathW );
1053 return ret;
1056 static PDH_STATUS validate_path( LPCWSTR path )
1058 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1059 if (*path++ != '\\' || !strchrW( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1060 return ERROR_SUCCESS;
1063 /***********************************************************************
1064 * PdhValidatePathW (PDH.@)
1066 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1068 PDH_STATUS ret;
1069 unsigned int i;
1071 TRACE("%s\n", debugstr_w(path));
1073 if ((ret = validate_path( path ))) return ret;
1075 for (i = 0; i < sizeof(counter_sources) / sizeof(counter_sources[0]); i++)
1076 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1078 return PDH_CSTATUS_NO_COUNTER;
1081 /***********************************************************************
1082 * PdhValidatePathExA (PDH.@)
1084 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1086 TRACE("%p %s\n", source, debugstr_a(path));
1088 if (source)
1090 FIXME("log file data source not supported\n");
1091 return ERROR_SUCCESS;
1093 return PdhValidatePathA( path );
1096 /***********************************************************************
1097 * PdhValidatePathExW (PDH.@)
1099 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1101 TRACE("%p %s\n", source, debugstr_w(path));
1103 if (source)
1105 FIXME("log file data source not supported\n");
1106 return ERROR_SUCCESS;
1108 return PdhValidatePathW( path );
1111 /***********************************************************************
1112 * PdhMakeCounterPathA (PDH.@)
1114 PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1115 LPDWORD buflen, DWORD flags )
1117 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1118 PDH_COUNTER_PATH_ELEMENTS_W eW;
1119 WCHAR *bufferW;
1120 DWORD buflenW;
1122 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1124 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1126 memset( &eW, 0, sizeof(eW) );
1127 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1128 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1129 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1130 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1131 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1132 eW.dwInstanceIndex = e->dwInstanceIndex;
1134 buflenW = 0;
1135 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1136 if (ret == PDH_MORE_DATA)
1138 if ((bufferW = heap_alloc( buflenW * sizeof(WCHAR) )))
1140 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1142 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1143 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1144 else ret = PDH_MORE_DATA;
1145 *buflen = len;
1147 heap_free( bufferW );
1149 else
1150 ret = PDH_MEMORY_ALLOCATION_FAILURE;
1153 done:
1154 heap_free( eW.szMachineName );
1155 heap_free( eW.szObjectName );
1156 heap_free( eW.szInstanceName );
1157 heap_free( eW.szParentInstance );
1158 heap_free( eW.szCounterName );
1159 return ret;
1162 /***********************************************************************
1163 * PdhMakeCounterPathW (PDH.@)
1165 PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1166 LPDWORD buflen, DWORD flags )
1168 static const WCHAR bslash[] = {'\\',0};
1169 static const WCHAR fslash[] = {'/',0};
1170 static const WCHAR lparen[] = {'(',0};
1171 static const WCHAR rparen[] = {')',0};
1172 static const WCHAR fmt[] = {'#','%','u',0};
1174 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1175 PDH_STATUS ret = ERROR_SUCCESS;
1176 DWORD len;
1178 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1180 if (flags) FIXME("unimplemented flags 0x%08x\n", flags);
1182 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1183 return PDH_INVALID_ARGUMENT;
1185 path[0] = 0;
1186 if (e->szMachineName)
1188 strcatW(path, bslash);
1189 strcatW(path, bslash);
1190 strcatW(path, e->szMachineName);
1192 strcatW(path, bslash);
1193 strcatW(path, e->szObjectName);
1194 if (e->szInstanceName)
1196 strcatW(path, lparen);
1197 if (e->szParentInstance)
1199 strcatW(path, e->szParentInstance);
1200 strcatW(path, fslash);
1202 strcatW(path, e->szInstanceName);
1203 sprintfW(instance, fmt, e->dwInstanceIndex);
1204 strcatW(path, instance);
1205 strcatW(path, rparen);
1207 strcatW(path, bslash);
1208 strcatW(path, e->szCounterName);
1210 len = strlenW(path) + 1;
1211 if (*buflen >= len) strcpyW(buffer, path);
1212 else ret = PDH_MORE_DATA;
1213 *buflen = len;
1214 return ret;
1217 /***********************************************************************
1218 * PdhEnumObjectItemsA (PDH.@)
1220 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1221 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1222 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1224 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1225 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1226 pcchInstanceListLength, dwDetailLevel, dwFlags);
1228 return PDH_NOT_IMPLEMENTED;
1231 /***********************************************************************
1232 * PdhEnumObjectItemsW (PDH.@)
1234 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1235 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1236 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1238 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1239 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1240 pcchInstanceListLength, dwDetailLevel, dwFlags);
1242 return PDH_NOT_IMPLEMENTED;
1245 /***********************************************************************
1246 * PdhSetDefaultRealTimeDataSource (PDH.@)
1248 PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1250 FIXME("%u\n", source);
1251 return ERROR_SUCCESS;