makefiles: Don't use standard libs for programs that specify -nodefaultlibs.
[wine/zf.git] / dlls / pdh / pdh_main.c
blobdaa0e2d33953013bd62a307d753f9090976cc909
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/heap.h"
36 #include "wine/list.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 WCHAR *pdh_strdup( const WCHAR *src )
52 WCHAR *dst;
54 if (!src) return NULL;
55 if ((dst = heap_alloc( (lstrlenW( src ) + 1) * sizeof(WCHAR) ))) lstrcpyW( dst, src );
56 return dst;
59 static inline WCHAR *pdh_strdup_aw( const char *src )
61 int len;
62 WCHAR *dst;
64 if (!src) return NULL;
65 len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
66 if ((dst = heap_alloc( len * sizeof(WCHAR) ))) MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
67 return dst;
70 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
72 TRACE("(0x%p, %d, %p)\n",hinstDLL,fdwReason,lpvReserved);
73 switch (fdwReason)
75 case DLL_WINE_PREATTACH:
76 return FALSE; /* prefer native version */
77 case DLL_PROCESS_ATTACH:
78 DisableThreadLibraryCalls(hinstDLL);
79 break;
80 case DLL_PROCESS_DETACH:
81 if (lpvReserved) break;
82 DeleteCriticalSection(&pdh_handle_cs);
83 break;
86 return TRUE;
89 union value
91 LONG longvalue;
92 double doublevalue;
93 LONGLONG largevalue;
96 struct counter
98 DWORD magic; /* signature */
99 struct list entry; /* list entry */
100 WCHAR *path; /* identifier */
101 DWORD type; /* counter type */
102 DWORD status; /* update status */
103 LONG scale; /* scale factor */
104 LONG defaultscale; /* default scale factor */
105 DWORD_PTR user; /* user data */
106 DWORD_PTR queryuser; /* query user data */
107 LONGLONG base; /* samples per second */
108 FILETIME stamp; /* time stamp */
109 void (CALLBACK *collect)( struct counter * ); /* collect callback */
110 union value one; /* first value */
111 union value two; /* second value */
114 #define PDH_MAGIC_COUNTER 0x50444831 /* 'PDH1' */
116 static struct counter *create_counter( void )
118 struct counter *counter;
120 if ((counter = heap_alloc_zero( sizeof(struct counter) )))
122 counter->magic = PDH_MAGIC_COUNTER;
123 return counter;
125 return NULL;
128 static void destroy_counter( struct counter *counter )
130 counter->magic = 0;
131 heap_free( counter->path );
132 heap_free( counter );
135 #define PDH_MAGIC_QUERY 0x50444830 /* 'PDH0' */
137 struct query
139 DWORD magic; /* signature */
140 DWORD_PTR user; /* user data */
141 HANDLE thread; /* collect thread */
142 DWORD interval; /* collect interval */
143 HANDLE wait; /* wait event */
144 HANDLE stop; /* stop event */
145 struct list counters; /* counter list */
148 static struct query *create_query( void )
150 struct query *query;
152 if ((query = heap_alloc_zero( sizeof(struct query) )))
154 query->magic = PDH_MAGIC_QUERY;
155 list_init( &query->counters );
156 return query;
158 return NULL;
161 static void destroy_query( struct query *query )
163 query->magic = 0;
164 heap_free( query );
167 struct source
169 DWORD index; /* name index */
170 const WCHAR *path; /* identifier */
171 void (CALLBACK *collect)( struct counter * ); /* collect callback */
172 DWORD type; /* counter type */
173 LONG scale; /* default scale factor */
174 LONGLONG base; /* samples per second */
177 static const WCHAR path_processor_time[] =
178 {'\\','P','r','o','c','e','s','s','o','r','(','_','T','o','t','a','l',')',
179 '\\','%',' ','P','r','o','c','e','s','s','o','r',' ','T','i','m','e',0};
180 static const WCHAR path_uptime[] =
181 {'\\','S','y','s','t','e','m', '\\', 'S','y','s','t','e','m',' ','U','p',' ','T','i','m','e',0};
183 static void CALLBACK collect_processor_time( struct counter *counter )
185 counter->two.largevalue = 500000; /* FIXME */
186 counter->status = PDH_CSTATUS_VALID_DATA;
189 static void CALLBACK collect_uptime( struct counter *counter )
191 counter->two.largevalue = GetTickCount64();
192 counter->status = PDH_CSTATUS_VALID_DATA;
195 #define TYPE_PROCESSOR_TIME \
196 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_RATE | PERF_TIMER_100NS | PERF_DELTA_COUNTER | \
197 PERF_INVERSE_COUNTER | PERF_DISPLAY_PERCENT)
199 #define TYPE_UPTIME \
200 (PERF_SIZE_LARGE | PERF_TYPE_COUNTER | PERF_COUNTER_ELAPSED | PERF_OBJECT_TIMER | PERF_DISPLAY_SECONDS)
202 /* counter source registry */
203 static const struct source counter_sources[] =
205 { 6, path_processor_time, collect_processor_time, TYPE_PROCESSOR_TIME, -5, 10000000 },
206 { 674, path_uptime, collect_uptime, TYPE_UPTIME, -3, 1000 }
209 static BOOL is_local_machine( const WCHAR *name, DWORD len )
211 WCHAR buf[MAX_COMPUTERNAME_LENGTH + 1];
212 DWORD buflen = ARRAY_SIZE(buf);
214 if (!GetComputerNameW( buf, &buflen )) return FALSE;
215 return len == buflen && !wcsnicmp( name, buf, buflen );
218 static BOOL pdh_match_path( LPCWSTR fullpath, LPCWSTR path )
220 const WCHAR *p;
222 if (path[0] == '\\' && path[1] == '\\' && (p = wcschr( path + 2, '\\' )) &&
223 is_local_machine( path + 2, p - path - 2 ))
225 path += p - path;
227 if (wcschr( path, '\\' )) p = fullpath;
228 else p = wcsrchr( fullpath, '\\' ) + 1;
229 return !wcscmp( p, path );
232 /***********************************************************************
233 * PdhAddCounterA (PDH.@)
235 PDH_STATUS WINAPI PdhAddCounterA( PDH_HQUERY query, LPCSTR path,
236 DWORD_PTR userdata, PDH_HCOUNTER *counter )
238 PDH_STATUS ret;
239 WCHAR *pathW;
241 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
243 if (!path) return PDH_INVALID_ARGUMENT;
245 if (!(pathW = pdh_strdup_aw( path )))
246 return PDH_MEMORY_ALLOCATION_FAILURE;
248 ret = PdhAddCounterW( query, pathW, userdata, counter );
250 heap_free( pathW );
251 return ret;
254 /***********************************************************************
255 * PdhAddCounterW (PDH.@)
257 PDH_STATUS WINAPI PdhAddCounterW( PDH_HQUERY hquery, LPCWSTR path,
258 DWORD_PTR userdata, PDH_HCOUNTER *hcounter )
260 struct query *query = hquery;
261 struct counter *counter;
262 unsigned int i;
264 TRACE("%p %s %lx %p\n", hquery, debugstr_w(path), userdata, hcounter);
266 if (!path || !hcounter) return PDH_INVALID_ARGUMENT;
268 EnterCriticalSection( &pdh_handle_cs );
269 if (!query || query->magic != PDH_MAGIC_QUERY)
271 LeaveCriticalSection( &pdh_handle_cs );
272 return PDH_INVALID_HANDLE;
275 *hcounter = NULL;
276 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
278 if (pdh_match_path( counter_sources[i].path, path ))
280 if ((counter = create_counter()))
282 counter->path = pdh_strdup( counter_sources[i].path );
283 counter->collect = counter_sources[i].collect;
284 counter->type = counter_sources[i].type;
285 counter->defaultscale = counter_sources[i].scale;
286 counter->base = counter_sources[i].base;
287 counter->queryuser = query->user;
288 counter->user = userdata;
290 list_add_tail( &query->counters, &counter->entry );
291 *hcounter = counter;
293 LeaveCriticalSection( &pdh_handle_cs );
294 return ERROR_SUCCESS;
296 LeaveCriticalSection( &pdh_handle_cs );
297 return PDH_MEMORY_ALLOCATION_FAILURE;
300 LeaveCriticalSection( &pdh_handle_cs );
301 return PDH_CSTATUS_NO_COUNTER;
304 /***********************************************************************
305 * PdhAddEnglishCounterA (PDH.@)
307 PDH_STATUS WINAPI PdhAddEnglishCounterA( PDH_HQUERY query, LPCSTR path,
308 DWORD_PTR userdata, PDH_HCOUNTER *counter )
310 TRACE("%p %s %lx %p\n", query, debugstr_a(path), userdata, counter);
312 if (!query) return PDH_INVALID_ARGUMENT;
313 return PdhAddCounterA( query, path, userdata, counter );
316 /***********************************************************************
317 * PdhAddEnglishCounterW (PDH.@)
319 PDH_STATUS WINAPI PdhAddEnglishCounterW( PDH_HQUERY query, LPCWSTR path,
320 DWORD_PTR userdata, PDH_HCOUNTER *counter )
322 TRACE("%p %s %lx %p\n", query, debugstr_w(path), userdata, counter);
324 if (!query) return PDH_INVALID_ARGUMENT;
325 return PdhAddCounterW( query, path, userdata, counter );
328 /* caller must hold counter lock */
329 static PDH_STATUS format_value( struct counter *counter, DWORD format, union value *raw1,
330 union value *raw2, PDH_FMT_COUNTERVALUE *value )
332 LONG factor;
334 factor = counter->scale ? counter->scale : counter->defaultscale;
335 if (format & PDH_FMT_LONG)
337 if (format & PDH_FMT_1000) value->u.longValue = raw2->longvalue * 1000;
338 else value->u.longValue = raw2->longvalue * pow( 10, factor );
340 else if (format & PDH_FMT_LARGE)
342 if (format & PDH_FMT_1000) value->u.largeValue = raw2->largevalue * 1000;
343 else value->u.largeValue = raw2->largevalue * pow( 10, factor );
345 else if (format & PDH_FMT_DOUBLE)
347 if (format & PDH_FMT_1000) value->u.doubleValue = raw2->doublevalue * 1000;
348 else value->u.doubleValue = raw2->doublevalue * pow( 10, factor );
350 else
352 WARN("unknown format %x\n", format);
353 return PDH_INVALID_ARGUMENT;
355 return ERROR_SUCCESS;
358 /***********************************************************************
359 * PdhCalculateCounterFromRawValue (PDH.@)
361 PDH_STATUS WINAPI PdhCalculateCounterFromRawValue( PDH_HCOUNTER handle, DWORD format,
362 PPDH_RAW_COUNTER raw1, PPDH_RAW_COUNTER raw2,
363 PPDH_FMT_COUNTERVALUE value )
365 PDH_STATUS ret;
366 struct counter *counter = handle;
368 TRACE("%p 0x%08x %p %p %p\n", handle, format, raw1, raw2, value);
370 if (!value) return PDH_INVALID_ARGUMENT;
372 EnterCriticalSection( &pdh_handle_cs );
373 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
375 LeaveCriticalSection( &pdh_handle_cs );
376 return PDH_INVALID_HANDLE;
379 ret = format_value( counter, format, (union value *)&raw1->SecondValue,
380 (union value *)&raw2->SecondValue, value );
382 LeaveCriticalSection( &pdh_handle_cs );
383 return ret;
387 /***********************************************************************
388 * PdhCloseQuery (PDH.@)
390 PDH_STATUS WINAPI PdhCloseQuery( PDH_HQUERY handle )
392 struct query *query = handle;
393 struct list *item, *next;
395 TRACE("%p\n", handle);
397 EnterCriticalSection( &pdh_handle_cs );
398 if (!query || query->magic != PDH_MAGIC_QUERY)
400 LeaveCriticalSection( &pdh_handle_cs );
401 return PDH_INVALID_HANDLE;
404 if (query->thread)
406 HANDLE thread = query->thread;
407 SetEvent( query->stop );
408 LeaveCriticalSection( &pdh_handle_cs );
410 WaitForSingleObject( thread, INFINITE );
412 EnterCriticalSection( &pdh_handle_cs );
413 if (query->magic != PDH_MAGIC_QUERY)
415 LeaveCriticalSection( &pdh_handle_cs );
416 return ERROR_SUCCESS;
418 CloseHandle( query->stop );
419 CloseHandle( query->thread );
420 query->thread = NULL;
423 LIST_FOR_EACH_SAFE( item, next, &query->counters )
425 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
427 list_remove( &counter->entry );
428 destroy_counter( counter );
431 destroy_query( query );
433 LeaveCriticalSection( &pdh_handle_cs );
434 return ERROR_SUCCESS;
437 /* caller must hold query lock */
438 static void collect_query_data( struct query *query )
440 struct list *item;
442 LIST_FOR_EACH( item, &query->counters )
444 SYSTEMTIME time;
445 struct counter *counter = LIST_ENTRY( item, struct counter, entry );
447 counter->collect( counter );
449 GetLocalTime( &time );
450 SystemTimeToFileTime( &time, &counter->stamp );
454 /***********************************************************************
455 * PdhCollectQueryData (PDH.@)
457 PDH_STATUS WINAPI PdhCollectQueryData( PDH_HQUERY handle )
459 struct query *query = handle;
461 TRACE("%p\n", handle);
463 EnterCriticalSection( &pdh_handle_cs );
464 if (!query || query->magic != PDH_MAGIC_QUERY)
466 LeaveCriticalSection( &pdh_handle_cs );
467 return PDH_INVALID_HANDLE;
470 if (list_empty( &query->counters ))
472 LeaveCriticalSection( &pdh_handle_cs );
473 return PDH_NO_DATA;
476 collect_query_data( query );
478 LeaveCriticalSection( &pdh_handle_cs );
479 return ERROR_SUCCESS;
482 static DWORD CALLBACK collect_query_thread( void *arg )
484 struct query *query = arg;
485 DWORD interval = query->interval;
486 HANDLE stop = query->stop;
488 for (;;)
490 if (WaitForSingleObject( stop, interval ) != WAIT_TIMEOUT) ExitThread( 0 );
492 EnterCriticalSection( &pdh_handle_cs );
493 if (query->magic != PDH_MAGIC_QUERY)
495 LeaveCriticalSection( &pdh_handle_cs );
496 ExitThread( PDH_INVALID_HANDLE );
499 collect_query_data( query );
501 if (!SetEvent( query->wait ))
503 LeaveCriticalSection( &pdh_handle_cs );
504 ExitThread( 0 );
506 LeaveCriticalSection( &pdh_handle_cs );
510 /***********************************************************************
511 * PdhCollectQueryDataEx (PDH.@)
513 PDH_STATUS WINAPI PdhCollectQueryDataEx( PDH_HQUERY handle, DWORD interval, HANDLE event )
515 PDH_STATUS ret;
516 struct query *query = handle;
518 TRACE("%p %d %p\n", handle, interval, event);
520 EnterCriticalSection( &pdh_handle_cs );
521 if (!query || query->magic != PDH_MAGIC_QUERY)
523 LeaveCriticalSection( &pdh_handle_cs );
524 return PDH_INVALID_HANDLE;
526 if (list_empty( &query->counters ))
528 LeaveCriticalSection( &pdh_handle_cs );
529 return PDH_NO_DATA;
531 if (query->thread)
533 HANDLE thread = query->thread;
534 SetEvent( query->stop );
535 LeaveCriticalSection( &pdh_handle_cs );
537 WaitForSingleObject( thread, INFINITE );
539 EnterCriticalSection( &pdh_handle_cs );
540 if (query->magic != PDH_MAGIC_QUERY)
542 LeaveCriticalSection( &pdh_handle_cs );
543 return PDH_INVALID_HANDLE;
545 CloseHandle( query->thread );
546 query->thread = NULL;
548 else if (!(query->stop = CreateEventW( NULL, FALSE, FALSE, NULL )))
550 ret = GetLastError();
551 LeaveCriticalSection( &pdh_handle_cs );
552 return ret;
554 query->wait = event;
555 query->interval = interval * 1000;
556 if (!(query->thread = CreateThread( NULL, 0, collect_query_thread, query, 0, NULL )))
558 ret = GetLastError();
559 CloseHandle( query->stop );
561 LeaveCriticalSection( &pdh_handle_cs );
562 return ret;
565 LeaveCriticalSection( &pdh_handle_cs );
566 return ERROR_SUCCESS;
569 /***********************************************************************
570 * PdhCollectQueryDataWithTime (PDH.@)
572 PDH_STATUS WINAPI PdhCollectQueryDataWithTime( PDH_HQUERY handle, LONGLONG *timestamp )
574 struct query *query = handle;
575 struct counter *counter;
576 struct list *item;
578 TRACE("%p %p\n", handle, timestamp);
580 if (!timestamp) return PDH_INVALID_ARGUMENT;
582 EnterCriticalSection( &pdh_handle_cs );
583 if (!query || query->magic != PDH_MAGIC_QUERY)
585 LeaveCriticalSection( &pdh_handle_cs );
586 return PDH_INVALID_HANDLE;
588 if (list_empty( &query->counters ))
590 LeaveCriticalSection( &pdh_handle_cs );
591 return PDH_NO_DATA;
594 collect_query_data( query );
596 item = list_head( &query->counters );
597 counter = LIST_ENTRY( item, struct counter, entry );
599 *timestamp = ((LONGLONG)counter->stamp.dwHighDateTime << 32) | counter->stamp.dwLowDateTime;
601 LeaveCriticalSection( &pdh_handle_cs );
602 return ERROR_SUCCESS;
605 /***********************************************************************
606 * PdhExpandWildCardPathA (PDH.@)
608 PDH_STATUS WINAPI PdhExpandWildCardPathA( LPCSTR szDataSource, LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
610 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
611 return PDH_NOT_IMPLEMENTED;
614 /***********************************************************************
615 * PdhExpandWildCardPathW (PDH.@)
617 PDH_STATUS WINAPI PdhExpandWildCardPathW( LPCWSTR szDataSource, LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength, DWORD dwFlags )
619 FIXME("%s, %s, %p, %p, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength, dwFlags);
620 return PDH_NOT_IMPLEMENTED;
623 /***********************************************************************
624 * PdhExpandCounterPathA (PDH.@)
626 PDH_STATUS WINAPI PdhExpandCounterPathA( LPCSTR szWildCardPath, LPSTR mszExpandedPathList, LPDWORD pcchPathListLength )
628 FIXME("%s, %p, %p: stub\n", debugstr_a(szWildCardPath), mszExpandedPathList, pcchPathListLength);
629 return PdhExpandWildCardPathA(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
632 /***********************************************************************
633 * PdhExpandCounterPathW (PDH.@)
635 PDH_STATUS WINAPI PdhExpandCounterPathW( LPCWSTR szWildCardPath, LPWSTR mszExpandedPathList, LPDWORD pcchPathListLength )
637 FIXME("%s, %p, %p: stub\n", debugstr_w(szWildCardPath), mszExpandedPathList, pcchPathListLength);
638 return PdhExpandWildCardPathW(NULL, szWildCardPath, mszExpandedPathList, pcchPathListLength, 0);
641 /***********************************************************************
642 * PdhGetCounterInfoA (PDH.@)
644 PDH_STATUS WINAPI PdhGetCounterInfoA( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_A info )
646 struct counter *counter = handle;
648 TRACE("%p %d %p %p\n", handle, text, size, info);
650 EnterCriticalSection( &pdh_handle_cs );
651 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
653 LeaveCriticalSection( &pdh_handle_cs );
654 return PDH_INVALID_HANDLE;
656 if (!size)
658 LeaveCriticalSection( &pdh_handle_cs );
659 return PDH_INVALID_ARGUMENT;
661 if (*size < sizeof(PDH_COUNTER_INFO_A))
663 *size = sizeof(PDH_COUNTER_INFO_A);
664 LeaveCriticalSection( &pdh_handle_cs );
665 return PDH_MORE_DATA;
668 memset( info, 0, sizeof(PDH_COUNTER_INFO_A) );
670 info->dwType = counter->type;
671 info->CStatus = counter->status;
672 info->lScale = counter->scale;
673 info->lDefaultScale = counter->defaultscale;
674 info->dwUserData = counter->user;
675 info->dwQueryUserData = counter->queryuser;
677 *size = sizeof(PDH_COUNTER_INFO_A);
679 LeaveCriticalSection( &pdh_handle_cs );
680 return ERROR_SUCCESS;
683 /***********************************************************************
684 * PdhGetCounterInfoW (PDH.@)
686 PDH_STATUS WINAPI PdhGetCounterInfoW( PDH_HCOUNTER handle, BOOLEAN text, LPDWORD size, PPDH_COUNTER_INFO_W info )
688 struct counter *counter = handle;
690 TRACE("%p %d %p %p\n", handle, text, size, info);
692 EnterCriticalSection( &pdh_handle_cs );
693 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
695 LeaveCriticalSection( &pdh_handle_cs );
696 return PDH_INVALID_HANDLE;
698 if (!size)
700 LeaveCriticalSection( &pdh_handle_cs );
701 return PDH_INVALID_ARGUMENT;
703 if (*size < sizeof(PDH_COUNTER_INFO_W))
705 *size = sizeof(PDH_COUNTER_INFO_W);
706 LeaveCriticalSection( &pdh_handle_cs );
707 return PDH_MORE_DATA;
710 memset( info, 0, sizeof(PDH_COUNTER_INFO_W) );
712 info->dwType = counter->type;
713 info->CStatus = counter->status;
714 info->lScale = counter->scale;
715 info->lDefaultScale = counter->defaultscale;
716 info->dwUserData = counter->user;
717 info->dwQueryUserData = counter->queryuser;
719 *size = sizeof(PDH_COUNTER_INFO_W);
721 LeaveCriticalSection( &pdh_handle_cs );
722 return ERROR_SUCCESS;
725 /***********************************************************************
726 * PdhGetCounterTimeBase (PDH.@)
728 PDH_STATUS WINAPI PdhGetCounterTimeBase( PDH_HCOUNTER handle, LONGLONG *base )
730 struct counter *counter = handle;
732 TRACE("%p %p\n", handle, base);
734 if (!base) return PDH_INVALID_ARGUMENT;
736 EnterCriticalSection( &pdh_handle_cs );
737 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
739 LeaveCriticalSection( &pdh_handle_cs );
740 return PDH_INVALID_HANDLE;
743 *base = counter->base;
745 LeaveCriticalSection( &pdh_handle_cs );
746 return ERROR_SUCCESS;
749 /***********************************************************************
750 * PdhGetDllVersion (PDH.@)
752 PDH_STATUS WINAPI PdhGetDllVersion( LPDWORD version )
754 if (!version)
755 return PDH_INVALID_ARGUMENT;
757 *version = PDH_VERSION;
759 return ERROR_SUCCESS;
762 /***********************************************************************
763 * PdhGetFormattedCounterValue (PDH.@)
765 PDH_STATUS WINAPI PdhGetFormattedCounterValue( PDH_HCOUNTER handle, DWORD format,
766 LPDWORD type, PPDH_FMT_COUNTERVALUE value )
768 PDH_STATUS ret;
769 struct counter *counter = handle;
771 TRACE("%p %x %p %p\n", handle, format, type, value);
773 if (!value) return PDH_INVALID_ARGUMENT;
775 EnterCriticalSection( &pdh_handle_cs );
776 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
778 LeaveCriticalSection( &pdh_handle_cs );
779 return PDH_INVALID_HANDLE;
781 if (counter->status)
783 LeaveCriticalSection( &pdh_handle_cs );
784 return PDH_INVALID_DATA;
786 if (!(ret = format_value( counter, format, &counter->one, &counter->two, value )))
788 value->CStatus = ERROR_SUCCESS;
789 if (type) *type = counter->type;
792 LeaveCriticalSection( &pdh_handle_cs );
793 return ret;
796 /***********************************************************************
797 * PdhGetRawCounterValue (PDH.@)
799 PDH_STATUS WINAPI PdhGetRawCounterValue( PDH_HCOUNTER handle, LPDWORD type,
800 PPDH_RAW_COUNTER value )
802 struct counter *counter = handle;
804 TRACE("%p %p %p\n", handle, type, value);
806 if (!value) return PDH_INVALID_ARGUMENT;
808 EnterCriticalSection( &pdh_handle_cs );
809 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
811 LeaveCriticalSection( &pdh_handle_cs );
812 return PDH_INVALID_HANDLE;
815 value->CStatus = counter->status;
816 value->TimeStamp.dwLowDateTime = counter->stamp.dwLowDateTime;
817 value->TimeStamp.dwHighDateTime = counter->stamp.dwHighDateTime;
818 value->FirstValue = counter->one.largevalue;
819 value->SecondValue = counter->two.largevalue;
820 value->MultiCount = 1; /* FIXME */
822 if (type) *type = counter->type;
824 LeaveCriticalSection( &pdh_handle_cs );
825 return ERROR_SUCCESS;
828 /***********************************************************************
829 * PdhLookupPerfIndexByNameA (PDH.@)
831 PDH_STATUS WINAPI PdhLookupPerfIndexByNameA( LPCSTR machine, LPCSTR name, LPDWORD index )
833 PDH_STATUS ret;
834 WCHAR *machineW = NULL;
835 WCHAR *nameW;
837 TRACE("%s %s %p\n", debugstr_a(machine), debugstr_a(name), index);
839 if (!name) return PDH_INVALID_ARGUMENT;
841 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
843 if (!(nameW = pdh_strdup_aw( name )))
844 return PDH_MEMORY_ALLOCATION_FAILURE;
846 ret = PdhLookupPerfIndexByNameW( machineW, nameW, index );
848 heap_free( nameW );
849 heap_free( machineW );
850 return ret;
853 /***********************************************************************
854 * PdhLookupPerfIndexByNameW (PDH.@)
856 PDH_STATUS WINAPI PdhLookupPerfIndexByNameW( LPCWSTR machine, LPCWSTR name, LPDWORD index )
858 unsigned int i;
860 TRACE("%s %s %p\n", debugstr_w(machine), debugstr_w(name), index);
862 if (!name || !index) return PDH_INVALID_ARGUMENT;
864 if (machine)
866 FIXME("remote machine not supported\n");
867 return PDH_CSTATUS_NO_MACHINE;
869 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
871 if (pdh_match_path( counter_sources[i].path, name ))
873 *index = counter_sources[i].index;
874 return ERROR_SUCCESS;
877 return PDH_STRING_NOT_FOUND;
880 /***********************************************************************
881 * PdhLookupPerfNameByIndexA (PDH.@)
883 PDH_STATUS WINAPI PdhLookupPerfNameByIndexA( LPCSTR machine, DWORD index, LPSTR buffer, LPDWORD size )
885 PDH_STATUS ret;
886 WCHAR *machineW = NULL;
887 WCHAR bufferW[PDH_MAX_COUNTER_NAME];
888 DWORD sizeW = ARRAY_SIZE(bufferW);
890 TRACE("%s %d %p %p\n", debugstr_a(machine), index, buffer, size);
892 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
894 if (machine && !(machineW = pdh_strdup_aw( machine ))) return PDH_MEMORY_ALLOCATION_FAILURE;
896 if (!(ret = PdhLookupPerfNameByIndexW( machineW, index, bufferW, &sizeW )))
898 int required = WideCharToMultiByte( CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL );
900 if (*size < required) ret = PDH_MORE_DATA;
901 else WideCharToMultiByte( CP_ACP, 0, bufferW, -1, buffer, required, NULL, NULL );
902 *size = required;
904 heap_free( machineW );
905 return ret;
908 /***********************************************************************
909 * PdhLookupPerfNameByIndexW (PDH.@)
911 PDH_STATUS WINAPI PdhLookupPerfNameByIndexW( LPCWSTR machine, DWORD index, LPWSTR buffer, LPDWORD size )
913 PDH_STATUS ret;
914 unsigned int i;
916 TRACE("%s %d %p %p\n", debugstr_w(machine), index, buffer, size);
918 if (machine)
920 FIXME("remote machine not supported\n");
921 return PDH_CSTATUS_NO_MACHINE;
924 if (!buffer || !size) return PDH_INVALID_ARGUMENT;
925 if (!index) return ERROR_SUCCESS;
927 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
929 if (counter_sources[i].index == index)
931 WCHAR *p = wcsrchr( counter_sources[i].path, '\\' ) + 1;
932 unsigned int required = lstrlenW( p ) + 1;
934 if (*size < required) ret = PDH_MORE_DATA;
935 else
937 lstrcpyW( buffer, p );
938 ret = ERROR_SUCCESS;
940 *size = required;
941 return ret;
944 return PDH_INVALID_ARGUMENT;
947 /***********************************************************************
948 * PdhOpenQueryA (PDH.@)
950 PDH_STATUS WINAPI PdhOpenQueryA( LPCSTR source, DWORD_PTR userdata, PDH_HQUERY *query )
952 PDH_STATUS ret;
953 WCHAR *sourceW = NULL;
955 TRACE("%s %lx %p\n", debugstr_a(source), userdata, query);
957 if (source && !(sourceW = pdh_strdup_aw( source ))) return PDH_MEMORY_ALLOCATION_FAILURE;
959 ret = PdhOpenQueryW( sourceW, userdata, query );
960 heap_free( sourceW );
962 return ret;
965 /***********************************************************************
966 * PdhOpenQueryW (PDH.@)
968 PDH_STATUS WINAPI PdhOpenQueryW( LPCWSTR source, DWORD_PTR userdata, PDH_HQUERY *handle )
970 struct query *query;
972 TRACE("%s %lx %p\n", debugstr_w(source), userdata, handle);
974 if (!handle) return PDH_INVALID_ARGUMENT;
976 if (source)
978 FIXME("log file data source not supported\n");
979 return PDH_INVALID_ARGUMENT;
981 if ((query = create_query()))
983 query->user = userdata;
984 *handle = query;
986 return ERROR_SUCCESS;
988 return PDH_MEMORY_ALLOCATION_FAILURE;
991 /***********************************************************************
992 * PdhRemoveCounter (PDH.@)
994 PDH_STATUS WINAPI PdhRemoveCounter( PDH_HCOUNTER handle )
996 struct counter *counter = handle;
998 TRACE("%p\n", handle);
1000 EnterCriticalSection( &pdh_handle_cs );
1001 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1003 LeaveCriticalSection( &pdh_handle_cs );
1004 return PDH_INVALID_HANDLE;
1007 list_remove( &counter->entry );
1008 destroy_counter( counter );
1010 LeaveCriticalSection( &pdh_handle_cs );
1011 return ERROR_SUCCESS;
1014 /***********************************************************************
1015 * PdhSetCounterScaleFactor (PDH.@)
1017 PDH_STATUS WINAPI PdhSetCounterScaleFactor( PDH_HCOUNTER handle, LONG factor )
1019 struct counter *counter = handle;
1021 TRACE("%p\n", handle);
1023 EnterCriticalSection( &pdh_handle_cs );
1024 if (!counter || counter->magic != PDH_MAGIC_COUNTER)
1026 LeaveCriticalSection( &pdh_handle_cs );
1027 return PDH_INVALID_HANDLE;
1029 if (factor < PDH_MIN_SCALE || factor > PDH_MAX_SCALE)
1031 LeaveCriticalSection( &pdh_handle_cs );
1032 return PDH_INVALID_ARGUMENT;
1035 counter->scale = factor;
1037 LeaveCriticalSection( &pdh_handle_cs );
1038 return ERROR_SUCCESS;
1041 /***********************************************************************
1042 * PdhValidatePathA (PDH.@)
1044 PDH_STATUS WINAPI PdhValidatePathA( LPCSTR path )
1046 PDH_STATUS ret;
1047 WCHAR *pathW;
1049 TRACE("%s\n", debugstr_a(path));
1051 if (!path) return PDH_INVALID_ARGUMENT;
1052 if (!(pathW = pdh_strdup_aw( path ))) return PDH_MEMORY_ALLOCATION_FAILURE;
1054 ret = PdhValidatePathW( pathW );
1056 heap_free( pathW );
1057 return ret;
1060 static PDH_STATUS validate_path( LPCWSTR path )
1062 if (!path || !*path) return PDH_INVALID_ARGUMENT;
1063 if (*path++ != '\\' || !wcschr( path, '\\' )) return PDH_CSTATUS_BAD_COUNTERNAME;
1064 return ERROR_SUCCESS;
1067 /***********************************************************************
1068 * PdhValidatePathW (PDH.@)
1070 PDH_STATUS WINAPI PdhValidatePathW( LPCWSTR path )
1072 PDH_STATUS ret;
1073 unsigned int i;
1075 TRACE("%s\n", debugstr_w(path));
1077 if ((ret = validate_path( path ))) return ret;
1079 for (i = 0; i < ARRAY_SIZE(counter_sources); i++)
1080 if (pdh_match_path( counter_sources[i].path, path )) return ERROR_SUCCESS;
1082 return PDH_CSTATUS_NO_COUNTER;
1085 /***********************************************************************
1086 * PdhVbAddCounter (PDH.@)
1088 PDH_STATUS WINAPI PdhVbAddCounter( PDH_HQUERY query, LPCSTR path, PDH_HCOUNTER *counter )
1090 FIXME("%p, %s, %p: stub!\n", query, debugstr_a(path), counter);
1092 if (!path) return PDH_INVALID_ARGUMENT;
1094 return PDH_NOT_IMPLEMENTED;
1097 /***********************************************************************
1098 * PdhValidatePathExA (PDH.@)
1100 PDH_STATUS WINAPI PdhValidatePathExA( PDH_HLOG source, LPCSTR path )
1102 TRACE("%p %s\n", source, debugstr_a(path));
1104 if (source)
1106 FIXME("log file data source not supported\n");
1107 return ERROR_SUCCESS;
1109 return PdhValidatePathA( path );
1112 /***********************************************************************
1113 * PdhValidatePathExW (PDH.@)
1115 PDH_STATUS WINAPI PdhValidatePathExW( PDH_HLOG source, LPCWSTR path )
1117 TRACE("%p %s\n", source, debugstr_w(path));
1119 if (source)
1121 FIXME("log file data source not supported\n");
1122 return ERROR_SUCCESS;
1124 return PdhValidatePathW( path );
1127 /***********************************************************************
1128 * PdhMakeCounterPathA (PDH.@)
1130 PDH_STATUS WINAPI PdhMakeCounterPathA( PDH_COUNTER_PATH_ELEMENTS_A *e, LPSTR buffer,
1131 LPDWORD buflen, DWORD flags )
1133 PDH_STATUS ret = PDH_MEMORY_ALLOCATION_FAILURE;
1134 PDH_COUNTER_PATH_ELEMENTS_W eW;
1135 WCHAR *bufferW;
1136 DWORD buflenW;
1138 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1140 if (!e || !buflen) return PDH_INVALID_ARGUMENT;
1142 memset( &eW, 0, sizeof(eW) );
1143 if (e->szMachineName && !(eW.szMachineName = pdh_strdup_aw( e->szMachineName ))) goto done;
1144 if (e->szObjectName && !(eW.szObjectName = pdh_strdup_aw( e->szObjectName ))) goto done;
1145 if (e->szInstanceName && !(eW.szInstanceName = pdh_strdup_aw( e->szInstanceName ))) goto done;
1146 if (e->szParentInstance && !(eW.szParentInstance = pdh_strdup_aw( e->szParentInstance ))) goto done;
1147 if (e->szCounterName && !(eW.szCounterName = pdh_strdup_aw( e->szCounterName ))) goto done;
1148 eW.dwInstanceIndex = e->dwInstanceIndex;
1150 buflenW = 0;
1151 ret = PdhMakeCounterPathW( &eW, NULL, &buflenW, flags );
1152 if (ret == PDH_MORE_DATA)
1154 if ((bufferW = heap_alloc( buflenW * sizeof(WCHAR) )))
1156 if (!(ret = PdhMakeCounterPathW( &eW, bufferW, &buflenW, flags )))
1158 int len = WideCharToMultiByte(CP_ACP, 0, bufferW, -1, NULL, 0, NULL, NULL);
1159 if (*buflen >= len) WideCharToMultiByte(CP_ACP, 0, bufferW, -1, buffer, *buflen, NULL, NULL);
1160 else ret = PDH_MORE_DATA;
1161 *buflen = len;
1163 heap_free( bufferW );
1165 else
1166 ret = PDH_MEMORY_ALLOCATION_FAILURE;
1169 done:
1170 heap_free( eW.szMachineName );
1171 heap_free( eW.szObjectName );
1172 heap_free( eW.szInstanceName );
1173 heap_free( eW.szParentInstance );
1174 heap_free( eW.szCounterName );
1175 return ret;
1178 /***********************************************************************
1179 * PdhMakeCounterPathW (PDH.@)
1181 PDH_STATUS WINAPI PdhMakeCounterPathW( PDH_COUNTER_PATH_ELEMENTS_W *e, LPWSTR buffer,
1182 LPDWORD buflen, DWORD flags )
1184 static const WCHAR bslash[] = {'\\',0};
1185 static const WCHAR fslash[] = {'/',0};
1186 static const WCHAR lparen[] = {'(',0};
1187 static const WCHAR rparen[] = {')',0};
1188 static const WCHAR fmt[] = {'#','%','u',0};
1190 WCHAR path[PDH_MAX_COUNTER_NAME], instance[12];
1191 PDH_STATUS ret = ERROR_SUCCESS;
1192 DWORD len;
1194 TRACE("%p %p %p 0x%08x\n", e, buffer, buflen, flags);
1196 if (flags) FIXME("unimplemented flags 0x%08x\n", flags);
1198 if (!e || !e->szCounterName || !e->szObjectName || !buflen)
1199 return PDH_INVALID_ARGUMENT;
1201 path[0] = 0;
1202 if (e->szMachineName)
1204 lstrcatW(path, bslash);
1205 lstrcatW(path, bslash);
1206 lstrcatW(path, e->szMachineName);
1208 lstrcatW(path, bslash);
1209 lstrcatW(path, e->szObjectName);
1210 if (e->szInstanceName)
1212 lstrcatW(path, lparen);
1213 if (e->szParentInstance)
1215 lstrcatW(path, e->szParentInstance);
1216 lstrcatW(path, fslash);
1218 lstrcatW(path, e->szInstanceName);
1219 swprintf(instance, ARRAY_SIZE(instance), fmt, e->dwInstanceIndex);
1220 lstrcatW(path, instance);
1221 lstrcatW(path, rparen);
1223 lstrcatW(path, bslash);
1224 lstrcatW(path, e->szCounterName);
1226 len = lstrlenW(path) + 1;
1227 if (*buflen >= len) lstrcpyW(buffer, path);
1228 else ret = PDH_MORE_DATA;
1229 *buflen = len;
1230 return ret;
1233 /***********************************************************************
1234 * PdhEnumObjectItemsA (PDH.@)
1236 PDH_STATUS WINAPI PdhEnumObjectItemsA(LPCSTR szDataSource, LPCSTR szMachineName, LPCSTR szObjectName,
1237 LPSTR mszCounterList, LPDWORD pcchCounterListLength, LPSTR mszInstanceList,
1238 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1240 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_a(szDataSource), debugstr_a(szMachineName),
1241 debugstr_a(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1242 pcchInstanceListLength, dwDetailLevel, dwFlags);
1244 return PDH_NOT_IMPLEMENTED;
1247 /***********************************************************************
1248 * PdhEnumObjectItemsW (PDH.@)
1250 PDH_STATUS WINAPI PdhEnumObjectItemsW(LPCWSTR szDataSource, LPCWSTR szMachineName, LPCWSTR szObjectName,
1251 LPWSTR mszCounterList, LPDWORD pcchCounterListLength, LPWSTR mszInstanceList,
1252 LPDWORD pcchInstanceListLength, DWORD dwDetailLevel, DWORD dwFlags)
1254 FIXME("%s, %s, %s, %p, %p, %p, %p, %d, 0x%x: stub\n", debugstr_w(szDataSource), debugstr_w(szMachineName),
1255 debugstr_w(szObjectName), mszCounterList, pcchCounterListLength, mszInstanceList,
1256 pcchInstanceListLength, dwDetailLevel, dwFlags);
1258 return PDH_NOT_IMPLEMENTED;
1261 /***********************************************************************
1262 * PdhSetDefaultRealTimeDataSource (PDH.@)
1264 PDH_STATUS WINAPI PdhSetDefaultRealTimeDataSource( DWORD source )
1266 FIXME("%u\n", source);
1267 return ERROR_SUCCESS;
1270 /***********************************************************************
1271 * PdhGetLogFileTypeA (PDH.@)
1273 PDH_STATUS WINAPI PdhGetLogFileTypeA(const char *log, DWORD *type)
1275 FIXME("%s, %p: stub\n", debugstr_a(log), type);
1276 return PDH_NOT_IMPLEMENTED;
1279 /***********************************************************************
1280 * PdhGetLogFileTypeW (PDH.@)
1282 PDH_STATUS WINAPI PdhGetLogFileTypeW(const WCHAR *log, DWORD *type)
1284 FIXME("%s, %p: stub\n", debugstr_w(log), type);
1285 return PDH_NOT_IMPLEMENTED;
1288 /***********************************************************************
1289 * PdhBindInputDataSourceA (PDH.@)
1291 PDH_STATUS WINAPI PdhBindInputDataSourceA(PDH_HLOG *source, const char *filenamelist)
1293 FIXME("%p %s: stub\n", source, debugstr_a(filenamelist));
1294 return PDH_NOT_IMPLEMENTED;
1297 /***********************************************************************
1298 * PdhBindInputDataSourceW (PDH.@)
1300 PDH_STATUS WINAPI PdhBindInputDataSourceW(PDH_HLOG *source, const WCHAR *filenamelist)
1302 FIXME("%p %s: stub\n", source, debugstr_w(filenamelist));
1303 return PDH_NOT_IMPLEMENTED;