deviceConnection.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include "main.h"
  2. #include "./deviceConnection.h"
  3. #include <strsafe.h>
  4. DeviceConnection::DeviceConnection()
  5. : ref(1), name(NULL), displayName(NULL)
  6. {
  7. if (FAILED(DeviceIconStore::CreateInstance(&iconStore)))
  8. iconStore = NULL;
  9. InitializeCriticalSection(&lock);
  10. }
  11. DeviceConnection::~DeviceConnection()
  12. {
  13. AnsiString_Free(name);
  14. String_Free(displayName);
  15. if (NULL != iconStore)
  16. iconStore->Release();
  17. DeleteCriticalSection(&lock);
  18. }
  19. HRESULT DeviceConnection::CreateInstance(const char *name, DeviceConnection **instance)
  20. {
  21. char *nameCopy;
  22. if (NULL == instance)
  23. return E_POINTER;
  24. if (FALSE != IS_STRING_EMPTY(name))
  25. return E_INVALIDARG;
  26. *instance = new DeviceConnection();
  27. if (NULL == *instance)
  28. return E_OUTOFMEMORY;
  29. nameCopy = AnsiString_Duplicate(name);
  30. if (NULL == nameCopy)
  31. {
  32. (*instance)->Release();
  33. return E_OUTOFMEMORY;
  34. }
  35. (*instance)->name = nameCopy;
  36. return S_OK;
  37. }
  38. size_t DeviceConnection::AddRef()
  39. {
  40. return InterlockedIncrement((LONG*)&ref);
  41. }
  42. size_t DeviceConnection::Release()
  43. {
  44. if (0 == ref)
  45. return ref;
  46. LONG r = InterlockedDecrement((LONG*)&ref);
  47. if (0 == r)
  48. delete(this);
  49. return r;
  50. }
  51. int DeviceConnection::QueryInterface(GUID interface_guid, void **object)
  52. {
  53. if (NULL == object) return E_POINTER;
  54. if (IsEqualIID(interface_guid, IFC_DeviceConnection))
  55. *object = static_cast<ifc_deviceconnection*>(this);
  56. else if (IsEqualIID(interface_guid, IFC_DeviceConnectionEditor))
  57. *object = static_cast<ifc_deviceconnectioneditor*>(this);
  58. else if (IsEqualIID(interface_guid, IFC_DeviceObject))
  59. *object = static_cast<ifc_deviceobject*>(this);
  60. else
  61. {
  62. *object = NULL;
  63. return E_NOINTERFACE;
  64. }
  65. if (NULL == *object)
  66. return E_UNEXPECTED;
  67. AddRef();
  68. return S_OK;
  69. }
  70. void DeviceConnection::Lock()
  71. {
  72. EnterCriticalSection(&lock);
  73. }
  74. void DeviceConnection::Unlock()
  75. {
  76. LeaveCriticalSection(&lock);
  77. }
  78. const char *DeviceConnection::GetName()
  79. {
  80. return name;
  81. }
  82. HRESULT DeviceConnection::GetIcon(wchar_t *buffer, size_t bufferSize, int width, int height)
  83. {
  84. HRESULT hr;
  85. if (NULL == buffer)
  86. return E_POINTER;
  87. Lock();
  88. if (NULL == iconStore)
  89. hr = E_UNEXPECTED;
  90. else
  91. hr = iconStore->Get(buffer, bufferSize, width, height);
  92. Unlock();
  93. return hr;
  94. }
  95. HRESULT DeviceConnection::GetDisplayName(wchar_t *buffer, size_t bufferSize)
  96. {
  97. HRESULT hr;
  98. if (NULL == buffer)
  99. return E_POINTER;
  100. Lock();
  101. hr = StringCchCopyExW(buffer, bufferSize, displayName, NULL, NULL, STRSAFE_IGNORE_NULLS);
  102. Unlock();
  103. return hr;
  104. }
  105. HRESULT DeviceConnection::GetIconStore(ifc_deviceiconstore **store)
  106. {
  107. HRESULT hr;
  108. if (NULL == store)
  109. return E_POINTER;
  110. Lock();
  111. if (NULL == iconStore)
  112. hr = E_UNEXPECTED;
  113. else
  114. {
  115. iconStore->AddRef();
  116. *store = iconStore;
  117. hr = S_OK;
  118. }
  119. Unlock();
  120. return hr;
  121. }
  122. HRESULT DeviceConnection::SetDisplayName(const wchar_t *displayName)
  123. {
  124. HRESULT hr;
  125. Lock();
  126. String_Free(this->displayName);
  127. this->displayName = String_Duplicate(displayName);
  128. if (NULL == this->displayName && NULL != displayName)
  129. hr = E_OUTOFMEMORY;
  130. else
  131. hr = S_OK;
  132. Unlock();
  133. return hr;
  134. }
  135. #define CBCLASS DeviceConnection
  136. START_MULTIPATCH;
  137. START_PATCH(MPIID_DEVICECONNECTION)
  138. M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, ADDREF, AddRef);
  139. M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, RELEASE, Release);
  140. M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, QUERYINTERFACE, QueryInterface);
  141. M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, API_GETNAME, GetName);
  142. M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, API_GETICON, GetIcon);
  143. M_CB(MPIID_DEVICECONNECTION, ifc_deviceconnection, API_GETDISPLAYNAME, GetDisplayName);
  144. NEXT_PATCH(MPIID_DEVICECONNECTIONEDITOR)
  145. M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, ADDREF, AddRef);
  146. M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, RELEASE, Release);
  147. M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, QUERYINTERFACE, QueryInterface);
  148. M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, API_SETDISPLAYNAME, SetDisplayName);
  149. M_CB(MPIID_DEVICECONNECTIONEDITOR, ifc_deviceconnectioneditor, API_GETICONSTORE, GetIconStore);
  150. END_PATCH
  151. END_MULTIPATCH;