1
0

deviceCommand.cpp 4.7 KB

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