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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#include "main.h"
#include "./deviceConnection.h"
#include <strsafe.h>
DeviceConnection::DeviceConnection()
: ref(1), name(NULL), displayName(NULL)
{
if (FAILED(DeviceIconStore::CreateInstance(&iconStore)))
iconStore = NULL;
InitializeCriticalSection(&lock);
}
DeviceConnection::~DeviceConnection()
{
AnsiString_Free(name);
String_Free(displayName);
if (NULL != iconStore)
iconStore->Release();
DeleteCriticalSection(&lock);
}
HRESULT DeviceConnection::CreateInstance(const char *name, DeviceConnection **instance)
{
char *nameCopy;
if (NULL == instance)
return E_POINTER;
if (FALSE != IS_STRING_EMPTY(name))
return E_INVALIDARG;
*instance = new DeviceConnection();
if (NULL == *instance)
return E_OUTOFMEMORY;
nameCopy = AnsiString_Duplicate(name);
if (NULL == nameCopy)
{
(*instance)->Release();
return E_OUTOFMEMORY;
}
(*instance)->name = nameCopy;
return S_OK;
}
size_t DeviceConnection::AddRef()
{
return InterlockedIncrement((LONG*)&ref);
}
size_t DeviceConnection::Release()
{
if (0 == ref)
return ref;
LONG r = InterlockedDecrement((LONG*)&ref);
if (0 == r)
delete(this);
return r;
}
int DeviceConnection::QueryInterface(GUID interface_guid, void **object)
{
if (NULL == object) return E_POINTER;
if (IsEqualIID(interface_guid, IFC_DeviceConnection))
*object = static_cast<ifc_deviceconnection*>(this);
else if (IsEqualIID(interface_guid, IFC_DeviceConnectionEditor))
*object = static_cast<ifc_deviceconnectioneditor*>(this);
else if (IsEqualIID(interface_guid, IFC_DeviceObject))
*object = static_cast<ifc_deviceobject*>(this);
else
{
*object = NULL;
return E_NOINTERFACE;
}
if (NULL == *object)
return E_UNEXPECTED;
AddRef();
return S_OK;
}
void DeviceConnection::Lock()
{
EnterCriticalSection(&lock);
}
void DeviceConnection::Unlock()
{
LeaveCriticalSection(&lock);
}
const char *DeviceConnection::GetName()
{
return name;
}
HRESULT DeviceConnection::GetIcon(wchar_t *buffer, size_t bufferSize, int width, int height)
{
HRESULT hr;
if (NULL == buffer)
return E_POINTER;
Lock();
if (NULL == iconStore)
hr = E_UNEXPECTED;
else
hr = iconStore->Get(buffer, bufferSize, width, height);
Unlock();
return hr;
}
HRESULT DeviceConnection::GetDisplayName(wchar_t *buffer, size_t bufferSize)
{
HRESULT hr;
if (NULL == buffer)
return E_POINTER;
Lock();
hr = StringCchCopyExW(buffer, bufferSize, displayName, NULL, NULL, STRSAFE_IGNORE_NULLS);
Unlock();
return hr;
}
HRESULT DeviceConnection::GetIconStore(ifc_deviceiconstore **store)
{
HRESULT hr;
if (NULL == store)
return E_POINTER;
Lock();
if (NULL == iconStore)
hr = E_UNEXPECTED;
else
{
iconStore->AddRef();
*store = iconStore;
hr = S_OK;
}
Unlock();
return hr;
}
HRESULT DeviceConnection::SetDisplayName(const wchar_t *displayName)
{
HRESULT hr;
Lock();
String_Free(this->displayName);
this->displayName = String_Duplicate(displayName);
if (NULL == this->displayName && NULL != displayName)
hr = E_OUTOFMEMORY;
else
hr = S_OK;
Unlock();
return hr;
}
#define CBCLASS DeviceConnection
START_MULTIPATCH;
START_PATCH(MPIID_DEVICECONNECTION)
M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, ADDREF, AddRef);
M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, RELEASE, Release);
M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, QUERYINTERFACE, QueryInterface);
M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, API_GETNAME, GetName);
M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, API_GETICON, GetIcon);
M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, API_GETDISPLAYNAME, GetDisplayName);
NEXT_PATCH(MPIID_DEVICECONNECTIONEDITOR)
M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, ADDREF, AddRef);
M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, RELEASE, Release);
M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, QUERYINTERFACE, QueryInterface);
M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, API_SETDISPLAYNAME, SetDisplayName);
M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, API_GETICONSTORE, GetIconStore);
END_PATCH
END_MULTIPATCH;
|