1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
// There is an error in the system header files that incorrectly
// places RpcStringBindingCompose in the app partition.
// Work around it by changing the WINAPI_FAMILY to desktop temporarily.
#pragma push_macro("WINAPI_FAMILY")
#undef WINAPI_FAMILY
#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
#include "RpcClient.h"
#pragma pop_macro("WINAPI_FAMILY")
using namespace SDKTemplate;
__int64 RpcClient::Initialize()
{
RPC_STATUS status;
RPC_WSTR pszStringBinding = nullptr;
status = RpcStringBindingCompose(
NULL,
reinterpret_cast<RPC_WSTR>(L"ncalrpc"),
NULL,
reinterpret_cast<RPC_WSTR>(RPC_STATIC_ENDPOINT),
NULL,
&pszStringBinding);
if (status)
{
goto error_status;
}
status = RpcBindingFromStringBinding(
pszStringBinding,
&hRpcBinding);
if (status)
{
goto error_status;
}
status = RpcStringFree(&pszStringBinding);
if (status)
{
goto error_status;
}
RpcTryExcept
{
::RemoteOpen(hRpcBinding, &phContext);
}
RpcExcept(1)
{
status = RpcExceptionCode();
}
RpcEndExcept
error_status:
return status;
}
//
// Make RPC call to start metering. This is a blocking call and
// will return only after StopMetering is called.
//
__int64 RpcClient::StartMeteringAndWaitForStop(__int64 samplePeriod)
{
__int64 ulCode = 0;
CallbackCount = 0;
MeteringData = 0;
RpcTryExcept
{
::StartMetering(phContext, samplePeriod, (__int64)this);
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
}
RpcEndExcept
return ulCode;
}
//
// Make rpc call SetSampleRate
//
__int64 RpcClient::SetSampleRate(int rate)
{
__int64 ulCode = 0;
RpcTryExcept
{
::SetSamplePeriod(phContext, rate);
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
}
RpcEndExcept
return ulCode;
}
//
// Make rpc call StopMetering
//
__int64 RpcClient::StopMetering()
{
__int64 ulCode = 0;
RpcTryExcept
{
::StopMetering(phContext);
}
RpcExcept(1)
{
ulCode = RpcExceptionCode();
}
RpcEndExcept
return ulCode;
}
RpcClient::~RpcClient()
{
RPC_STATUS status;
if (hRpcBinding != NULL)
{
RpcTryExcept
{
::RemoteClose(&phContext);
}
RpcExcept(1)
{
// Ignoring the result of RemoteClose as nothing can be
// done on the client side with this return code
status = RpcExceptionCode();
}
RpcEndExcept
status = RpcBindingFree(&hRpcBinding);
hRpcBinding = NULL;
}
}
//
// Metering rpc callback
//
void MeteringDataEvent(__int64 data, __int64 context)
{
RpcClient* client = static_cast<RpcClient*>((PVOID)context);
client->MeteringData = data;
++client->CallbackCount;
}
///******************************************************/
///* MIDL allocate and free */
///******************************************************/
void __RPC_FAR * __RPC_USER midl_user_allocate(size_t len)
{
return(malloc(len));
}
void __RPC_USER midl_user_free(void __RPC_FAR * ptr)
{
free(ptr);
}
|