实现 OpenPerformanceData
OpenPerformanceData 函数使提供程序有机会初始化其性能数据结构。 当使用者首次调用 RegQueryValueEx 时,或者使用者使用 RegOpenKey 或 RegConnectRegistry 函数打开HKEY_PERFORMANCE_DATA,系统会 调用 open 函数。
以下示例演示 OpenPerformanceData 函数的实现。 包含此函数中使用的计数器定义的头文件遵循此示例。 如果使用 C++ 实现此函数,请确保在声明函数时使用外部“C”。 此示例中使用的计数器偏移常量定义 CounterOffsets.h 文件,如 向注册表添加计数器名称和说明中所示。
#include "provider.h"
// Callback that the performance service calls once for each consumer that wants
// to consume one or more of your counters. You can use this notification to
// prepare the counter data structures and setup communications with the
// application that is providing the actual performance data.
extern "C" DWORD APIENTRY OpenPerfData(LPWSTR pContext)
{
DWORD FirstCounterIndex = 0;
DWORD FirstHelpIndex = 0;
DWORD DataSize = sizeof(DWORD); // Used in the RegQueryValueEx call
HKEY hkey;
LONG rc = ERROR_SUCCESS;
// Use g_OpenCount to prevent initializing the data more than once.
if (g_OpenCount > 1)
{
goto cleanup;
}
// Retrieve the application specific context data
// if you define an Export value under the
// HKLM\SYSTEM\CurrentControlSet\Services\MyProvider\Linkage key.
// The context string is a REG_MULTI_SZ string.
if (pContext)
{
for (; *pContext; pContext += (wcslen(pContext)+1))
{
// Do something with the string.
}
}
// Retrieve the starting index values for your counters and help strings. You add
// your offset values (see Adding Counter Names and Descriptions to the Registry)
// to these index values to determine your actual object and counter index values.
rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, PERFORMANCE_REGISTRY_PATH, 0, KEY_READ, &hkey);
if (ERROR_SUCCESS == rc)
{
rc = RegQueryValueEx(hkey, L"First Counter", NULL, NULL, (LPBYTE)&FirstCounterIndex, &DataSize);
if (ERROR_SUCCESS == rc)
{
rc = RegQueryValueEx(hkey, L"First Help", NULL, NULL, (LPBYTE)&FirstHelpIndex, &DataSize);
}
RegCloseKey(hkey);
}
// If you could not open the key or retrieve the value, exit.
if (ERROR_SUCCESS != rc)
{
// If you return a non-success code, the service adds a Disable Performance
// Counters value to your Performance key to disable your provider.
// Consider logging an event and returning success instead of failing.
rc = ERROR_SUCCESS;
goto cleanup;
}
// Initialize the Transfer object. This is a single instance object, so you can
// initialize each member of the object and its counters.
ZeroMemory(&g_Transfer, TransferSize);
g_Transfer.Object.TotalByteLength = TransferSize;
g_Transfer.Object.DefinitionLength = sizeof(PERF_OBJECT_TYPE) +
sizeof(PERF_COUNTER_DEFINITION) * NumberOfTransferCounters;
g_Transfer.Object.HeaderLength = sizeof(PERF_OBJECT_TYPE);
g_TransferIndex = FirstCounterIndex;
g_Transfer.Object.ObjectNameTitleIndex = g_TransferIndex;
g_Transfer.Object.ObjectHelpTitleIndex = FirstHelpIndex;
g_Transfer.Object.DetailLevel = PERF_DETAIL_ADVANCED;
g_Transfer.Object.NumCounters = NumberOfTransferCounters;
g_Transfer.Object.DefaultCounter = -1; // No default counter
g_Transfer.Object.NumInstances = PERF_NO_INSTANCES;
// Initialize the bytes sent counter.
g_Transfer.BytesSentCounter.ByteLength = sizeof(PERF_COUNTER_DEFINITION);
g_Transfer.BytesSentCounter.CounterNameTitleIndex = FirstCounterIndex + BYTES_SENT;
g_Transfer.BytesSentCounter.CounterHelpTitleIndex = FirstHelpIndex + BYTES_SENT;
g_Transfer.BytesSentCounter.DetailLevel = PERF_DETAIL_ADVANCED;
g_Transfer.BytesSentCounter.CounterType = PERF_COUNTER_RAWCOUNT;
g_Transfer.BytesSentCounter.CounterSize = sizeof(DWORD);
g_Transfer.BytesSentCounter.CounterOffset = BytesSentOffset;
// Initialize the available bandwidth counter.
g_Transfer.AvailableBandwidthCounter.ByteLength = sizeof(PERF_COUNTER_DEFINITION);
g_Transfer.AvailableBandwidthCounter.CounterNameTitleIndex = FirstCounterIndex + AVAILABLE_BANDWIDTH;
g_Transfer.AvailableBandwidthCounter.CounterHelpTitleIndex = FirstHelpIndex + AVAILABLE_BANDWIDTH;
g_Transfer.AvailableBandwidthCounter.DetailLevel = PERF_DETAIL_ADVANCED;
g_Transfer.AvailableBandwidthCounter.CounterType = PERF_RAW_FRACTION;
g_Transfer.AvailableBandwidthCounter.CounterSize = sizeof(DWORD);
g_Transfer.AvailableBandwidthCounter.CounterOffset = AvailableBandwidthOffset;
// Initialize the total bandwidth counter. This is a base counter for the
// available bandwidth counter. You do not specify index values for base counters.
g_Transfer.TotalBandwidthBaseCounter.ByteLength = sizeof(PERF_COUNTER_DEFINITION);
g_Transfer.TotalBandwidthBaseCounter.CounterType = PERF_RAW_BASE;
g_Transfer.TotalBandwidthBaseCounter.CounterSize = sizeof(DWORD);
g_Transfer.TotalBandwidthBaseCounter.CounterOffset = TotalBandwidthBaseOffset;
// Specify the size of the counter block which contains the raw counter values.
g_Transfer.CounterBlock.ByteLength = EndOfTransferData;
// Initialize the Peer object. This is a multiple instance object. You cannot
// initialize all members of the Peer object because you do not know how
// many instances there will be until the object data is queried. You set
// the TotalByteLength and NumInstances members in the CollectPerfData function.
ZeroMemory(&g_Peer, sizeof(PEER));
g_Peer.Object.DefinitionLength = sizeof(PERF_OBJECT_TYPE) +
sizeof(PERF_COUNTER_DEFINITION) * NumberOfPeerCounters;
g_Peer.Object.HeaderLength = sizeof(PERF_OBJECT_TYPE);
g_PeerIndex = FirstCounterIndex + PEER_OBJECT;
g_Peer.Object.ObjectNameTitleIndex = g_PeerIndex;
g_Peer.Object.ObjectHelpTitleIndex = FirstHelpIndex + PEER_OBJECT;
g_Peer.Object.DetailLevel = PERF_DETAIL_ADVANCED;
g_Peer.Object.NumCounters = NumberOfPeerCounters;
g_Peer.Object.DefaultCounter = -1; // No default counter
// Initialize the bytes served counter.
g_Peer.BytesServedCounter.ByteLength = sizeof(PERF_COUNTER_DEFINITION);
g_Peer.BytesServedCounter.CounterNameTitleIndex = FirstCounterIndex + BYTES_SERVED;
g_Peer.BytesServedCounter.CounterHelpTitleIndex = FirstHelpIndex + BYTES_SERVED;
g_Peer.BytesServedCounter.DetailLevel = PERF_DETAIL_ADVANCED;
g_Peer.BytesServedCounter.CounterType = PERF_COUNTER_RAWCOUNT;
g_Peer.BytesServedCounter.CounterSize = sizeof(DWORD);
g_Peer.BytesServedCounter.CounterOffset = BytesServedOffset;
InterlockedIncrement(&g_OpenCount); // Decremented in ClosePerfData
cleanup:
return rc;
}
下面是此示例中使用的头文件。
#ifndef _PROVIDER_H
#define _PROVIDER_H
#include <windows.h>
#include <strsafe.h>
#include "CounterOffsets.h"
extern "C" DWORD APIENTRY OpenPerfData(LPWSTR pContext);
extern "C" DWORD APIENTRY CollectPerfData(LPWSTR pQuery, PVOID* ppData, LPDWORD pcbData, LPDWORD pObjectsReturned);
extern "C" DWORD APIENTRY ClosePerfData();
BOOL IsQuerySupported(LPWSTR pQuery, DWORD* pQueriedObjects);
DWORD GetQuerySize(DWORD QueriedObjects);
// Define the Transfer counter object. This object is a single
// instance counter object, so you can define it completely.
typedef struct _transfer
{
PERF_OBJECT_TYPE Object;
PERF_COUNTER_DEFINITION BytesSentCounter;
PERF_COUNTER_DEFINITION AvailableBandwidthCounter;
PERF_COUNTER_DEFINITION TotalBandwidthBaseCounter;
PERF_COUNTER_BLOCK CounterBlock;
DWORD BytesSentData;
DWORD AvailableBandwidthData;
DWORD TotalBandwidthData;
} TRANSFER, *PTRANSFER;
// Define offsets to counter data
#define BytesSentOffset sizeof(PERF_COUNTER_BLOCK)
#define AvailableBandwidthOffset BytesSentOffset + sizeof(DWORD)
#define TotalBandwidthBaseOffset AvailableBandwidthOffset + sizeof(DWORD)
#define EndOfTransferData TotalBandwidthBaseOffset + sizeof(DWORD)
#define TransferSize sizeof(TRANSFER)
#define NumberOfTransferCounters 3
#define MAX_INSTANCE_NAME_LEN 15
// Define the Peer counter object. This object is a multiple
// instance object, so you must define the object and instances
// separately.
typedef struct _peer {
PERF_OBJECT_TYPE Object;
PERF_COUNTER_DEFINITION BytesServedCounter;
} PEER, *PPEER;
typedef struct _peerinstance {
PERF_INSTANCE_DEFINITION Instance;
WCHAR InstanceName[MAX_INSTANCE_NAME_LEN+1];
PERF_COUNTER_BLOCK CounterBlock;
DWORD BytesServedData;
} PEER_INSTANCE, *PPEER_INSTANCE;
#define BytesServedOffset sizeof(PERF_COUNTER_BLOCK)
#define EndOfPeerData BytesServedOffset + sizeof(DWORD)
#define NumberOfPeerCounters 1
// For this example, the instances are static, so predefine the names.
#define INSTANCE_NAME_1 L"Peer 1"
#define INSTANCE_NAME_2 L"Peer 2"
#define PERFORMANCE_REGISTRY_PATH L"SYSTEM\\CurrentControlSet\\Services\\MyApplication\\Performance"
typedef enum _queriedflags {
QUERIED_TRANSFER_OBJECT = 1,
QUERIED_PEER_OBJECT = 2,
QUERIED_ALL_OBJECTS = 0xFFFF
} QUERIED_FLAGS;
//Globals
TRANSFER g_Transfer; // Transfer object
PEER g_Peer; // Peer object
DWORD g_TransferIndex = 0; // Index value for the Transfer object
DWORD g_PeerIndex = 0; // Index value for the Peer object
DWORD g_QueriedObjects = 0; // Objects that were queried
UNALIGNED LONG g_OpenCount = 0; // Reference count for the number of times
// that OpenPerfData is called.
#endif
以下示例演示用于导出打开、收集和关闭函数的模块定义 (.def) 文件。
LIBRARY "PerfProvider"
EXPORTS
OpenPerfData @1
CollectPerfData @2
ClosePerfData @3
以下示例演示 ClosePerformanceData 函数的实现。 当使用者调用 RegCloseKey 以关闭HKEY_PERFORMANCE_DATA时,系统会调用 close 函数。 提供程序使用此调用释放已分配的任何资源。
// Callback that the performance service calls once for each consumer that has
// finished consuming your performance counters. Cleanup any resources that
// you allocated for your performance counters.
extern "C" DWORD APIENTRY ClosePerfData(void)
{
if (g_OpenCount > 0)
{
InterlockedDecrement(&g_OpenCount);
}
return ERROR_SUCCESS;
}