Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

262 lines
9.7 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: M A I N . C P P
  7. //
  8. // Contents: Simple test harness for device host
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 19 Sep 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "pch.h"
  16. #pragma hdrstop
  17. #include "res.h"
  18. #include "ComUtility.h"
  19. #include "upnphost.h"
  20. #include "sdev.h"
  21. #include "sdev_i.c"
  22. // COM Smart Pointers
  23. typedef SmartComPtr<IUPnPRegistrar> IUPnPRegistrarPtr;
  24. typedef SmartComPtr<IUPnPReregistrar> IUPnPReregistrarPtr;
  25. typedef SmartComPtr<IUPnPDeviceControl> IUPnPDeviceControlPtr;
  26. enum TestCase { tcRegister, tcRunning, tcUnregister, tcUnregisterRunning, tcReregister, tcReregisterRunning};
  27. void Test(TestCase tc)
  28. {
  29. HRESULT hr = S_OK;
  30. IUPnPRegistrarPtr pRegistrar;
  31. hr = pRegistrar.HrCreateInstanceServer(CLSID_UPnPRegistrar);
  32. if(SUCCEEDED(hr))
  33. {
  34. HRSRC hrsrc = FindResource(GetModuleHandle(NULL), MAKEINTRESOURCE(IDX_DESC_DOC), L"XML");
  35. if(hrsrc)
  36. {
  37. HGLOBAL hGlobal = LoadResource(GetModuleHandle(NULL), hrsrc);
  38. if(hGlobal)
  39. {
  40. void * pvData = LockResource(hGlobal);
  41. BSTR bstrData = NULL;
  42. if(pvData)
  43. {
  44. long nLength = SizeofResource(GetModuleHandle(NULL), hrsrc);
  45. wchar_t * sz = new wchar_t[nLength + 1];
  46. MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, reinterpret_cast<char*>(pvData), nLength, sz, nLength);
  47. sz[nLength] = 0;
  48. bstrData = SysAllocString(sz);
  49. delete [] sz;
  50. }
  51. if(bstrData)
  52. {
  53. BSTR bstrId = NULL;
  54. BSTR bstrProgId = SysAllocString(L"Sdev.SampleDevice.1");
  55. BSTR bstrInitString = SysAllocString(L"Init");
  56. BSTR bstrContainerId = SysAllocString(L"Sample Container");
  57. BSTR bstrPath = SysAllocString(L"C:\\upnp\\");
  58. switch(tc)
  59. {
  60. case tcRegister:
  61. {
  62. hr = pRegistrar->RegisterDevice(
  63. bstrData,
  64. bstrProgId,
  65. bstrInitString,
  66. bstrContainerId,
  67. bstrPath,
  68. 100000,
  69. &bstrId);
  70. }
  71. break;
  72. case tcRunning:
  73. {
  74. IUPnPDeviceControlPtr pDevice;
  75. hr = pDevice.HrCreateInstanceInproc(CLSID_UPnPSampleDevice);
  76. if(SUCCEEDED(hr))
  77. {
  78. hr = pRegistrar->RegisterRunningDevice(
  79. bstrData,
  80. pDevice,
  81. bstrInitString,
  82. bstrPath,
  83. 100000,
  84. &bstrId);
  85. }
  86. }
  87. break;
  88. case tcUnregister:
  89. {
  90. hr = pRegistrar->RegisterDevice(
  91. bstrData,
  92. bstrProgId,
  93. bstrInitString,
  94. bstrContainerId,
  95. bstrPath,
  96. 100000,
  97. &bstrId);
  98. if(SUCCEEDED(hr))
  99. {
  100. hr = pRegistrar->UnregisterDevice(bstrId, TRUE);
  101. }
  102. }
  103. break;
  104. case tcUnregisterRunning:
  105. {
  106. IUPnPDeviceControlPtr pDevice;
  107. hr = pDevice.HrCreateInstanceInproc(CLSID_UPnPSampleDevice);
  108. if(SUCCEEDED(hr))
  109. {
  110. hr = pRegistrar->RegisterRunningDevice(
  111. bstrData,
  112. pDevice,
  113. bstrInitString,
  114. bstrPath,
  115. 100000,
  116. &bstrId);
  117. if(SUCCEEDED(hr))
  118. {
  119. hr = pRegistrar->UnregisterDevice(bstrId, TRUE);
  120. }
  121. }
  122. }
  123. break;
  124. case tcReregister:
  125. {
  126. hr = pRegistrar->RegisterDevice(
  127. bstrData,
  128. bstrProgId,
  129. bstrInitString,
  130. bstrContainerId,
  131. bstrPath,
  132. 100000,
  133. &bstrId);
  134. if(SUCCEEDED(hr))
  135. {
  136. hr = pRegistrar->UnregisterDevice(bstrId, FALSE);
  137. if(SUCCEEDED(hr))
  138. {
  139. IUPnPReregistrarPtr pReregistrar;
  140. hr = pReregistrar.HrAttach(pRegistrar);
  141. if(SUCCEEDED(hr))
  142. {
  143. hr = pReregistrar->ReregisterDevice(
  144. bstrId,
  145. bstrData,
  146. bstrProgId,
  147. bstrInitString,
  148. bstrContainerId,
  149. bstrPath,
  150. 100000);
  151. }
  152. }
  153. }
  154. }
  155. break;
  156. case tcReregisterRunning:
  157. {
  158. IUPnPDeviceControlPtr pDevice;
  159. hr = pDevice.HrCreateInstanceInproc(CLSID_UPnPSampleDevice);
  160. if(SUCCEEDED(hr))
  161. {
  162. hr = pRegistrar->RegisterRunningDevice(
  163. bstrData,
  164. pDevice,
  165. bstrInitString,
  166. bstrPath,
  167. 100000,
  168. &bstrId);
  169. if(SUCCEEDED(hr))
  170. {
  171. hr = pRegistrar->UnregisterDevice(bstrId, FALSE);
  172. if(SUCCEEDED(hr))
  173. {
  174. IUPnPReregistrarPtr pReregistrar;
  175. hr = pReregistrar.HrAttach(pRegistrar);
  176. if(SUCCEEDED(hr))
  177. {
  178. hr = pReregistrar->ReregisterRunningDevice(
  179. bstrId,
  180. bstrData,
  181. pDevice,
  182. bstrInitString,
  183. bstrPath,
  184. 100000);
  185. }
  186. }
  187. }
  188. }
  189. }
  190. break;
  191. }
  192. SysFreeString(bstrProgId);
  193. SysFreeString(bstrInitString);
  194. SysFreeString(bstrContainerId);
  195. SysFreeString(bstrPath);
  196. if(SUCCEEDED(hr))
  197. {
  198. SysFreeString(bstrId);
  199. }
  200. SysFreeString(bstrData);
  201. }
  202. FreeResource(hGlobal);
  203. }
  204. }
  205. }
  206. }
  207. extern "C" int __cdecl wmain(int argc, wchar_t * argv[])
  208. {
  209. TestCase tc = tcRegister;
  210. if(2 == argc)
  211. {
  212. if(!lstrcmpi(L"register", argv[1]))
  213. {
  214. tc = tcRegister;
  215. }
  216. else if(!lstrcmpi(L"running", argv[1]))
  217. {
  218. tc = tcRunning;
  219. }
  220. else if(!lstrcmpi(L"unregister", argv[1]))
  221. {
  222. tc = tcUnregister;
  223. }
  224. else if(!lstrcmpi(L"unregisterrunning", argv[1]))
  225. {
  226. tc = tcUnregisterRunning;
  227. }
  228. else if(!lstrcmpi(L"reregister", argv[1]))
  229. {
  230. tc = tcReregister;
  231. }
  232. else if(!lstrcmpi(L"reregisterrunning", argv[1]))
  233. {
  234. tc = tcReregisterRunning;
  235. }
  236. }
  237. CoInitializeEx(NULL, COINIT_MULTITHREADED);
  238. CoInitializeSecurity(
  239. NULL,
  240. -1,
  241. NULL,
  242. NULL,
  243. RPC_C_AUTHN_LEVEL_NONE,
  244. RPC_C_IMP_LEVEL_IMPERSONATE,
  245. NULL,
  246. 0,
  247. NULL);
  248. Test(tc);
  249. CoUninitialize();
  250. return 0;
  251. }