Leaked source code of windows server 2003
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.

295 lines
11 KiB

  1. /*****************************************************************************
  2. * (C) COPYRIGHT MICROSOFT CORPORATION, 2002
  3. *
  4. * AUTHOR: ByronC
  5. *
  6. * DATE: 4/13/2002
  7. *
  8. * @doc INTERNAL
  9. *
  10. * @module StiEventHandlerInfo.cpp - Implementation for <c StiEventHandlerInfo> |
  11. *
  12. * This file contains the implementations for the <c StiEventHandlerInfo> class.
  13. *
  14. *****************************************************************************/
  15. #include "precomp.h"
  16. /*****************************************************************************
  17. * @doc INTERNAL
  18. *
  19. * @mfunc | StiEventHandlerInfo | StiEventHandlerInfo |
  20. *
  21. * We initialize all member variables. In general, this sets the values to 0,
  22. * except:
  23. * <nl><md StiEventHandlerInfo::m_ulSig> is set to be StiEventHandlerInfo_INIT_SIG.
  24. * <nl><md StiEventHandlerInfo::m_cRef> is set to be 1.
  25. *
  26. *****************************************************************************/
  27. StiEventHandlerInfo::StiEventHandlerInfo(
  28. const CSimpleStringWide &cswAppName,
  29. const CSimpleStringWide &cswCommandline) :
  30. m_ulSig(StiEventHandlerInfo_INIT_SIG),
  31. m_cRef(1),
  32. m_cswAppName(cswAppName),
  33. m_cswCommandline(cswCommandline)
  34. {
  35. }
  36. /*****************************************************************************
  37. * @doc INTERNAL
  38. *
  39. * @mfunc | StiEventHandlerInfo | ~StiEventHandlerInfo |
  40. *
  41. * Do any cleanup that is not already done.
  42. *
  43. * Also:
  44. * <nl><md StiEventHandlerInfo::m_ulSig> is set to be StiEventHandlerInfo_DEL_SIG.
  45. *
  46. *****************************************************************************/
  47. StiEventHandlerInfo::~StiEventHandlerInfo()
  48. {
  49. m_ulSig = StiEventHandlerInfo_DEL_SIG;
  50. m_cRef = 0;
  51. }
  52. /*****************************************************************************
  53. * @doc INTERNAL
  54. *
  55. * @mfunc ULONG | StiEventHandlerInfo | AddRef |
  56. *
  57. * Increments this object's ref count. We should always AddRef when handing
  58. * out a pointer to this object.
  59. *
  60. * @rvalue Count |
  61. * The reference count after the count has been incremented.
  62. *****************************************************************************/
  63. ULONG __stdcall StiEventHandlerInfo::AddRef()
  64. {
  65. InterlockedIncrement((long*) &m_cRef);
  66. return m_cRef;
  67. }
  68. /*****************************************************************************
  69. * @doc INTERNAL
  70. *
  71. * @mfunc ULONG | StiEventHandlerInfo | Release |
  72. *
  73. * Decrement this object's ref count. We should always Release when finished
  74. * with a pointer to this object.
  75. *
  76. * @rvalue Count |
  77. * The reference count after the count has been decremented.
  78. *****************************************************************************/
  79. ULONG __stdcall StiEventHandlerInfo::Release()
  80. {
  81. ULONG ulRefCount = m_cRef - 1;
  82. if (InterlockedDecrement((long*) &m_cRef) == 0)
  83. {
  84. delete this;
  85. return 0;
  86. }
  87. return ulRefCount;
  88. }
  89. /*****************************************************************************
  90. * @doc INTERNAL
  91. *
  92. * @mfunc CSimpleStringWide | StiEventHandlerInfo | getAppName |
  93. *
  94. * Accessor method for the application name. The return is done as a copy by value.
  95. *
  96. * @rvalue CSimpleStringWide |
  97. * The name of the registered STI handler.
  98. *****************************************************************************/
  99. CSimpleStringWide StiEventHandlerInfo::getAppName()
  100. {
  101. return m_cswAppName;
  102. }
  103. /*****************************************************************************
  104. * @doc INTERNAL
  105. *
  106. * @mfunc CSimpleStringWide | StiEventHandlerInfo | getCommandline |
  107. *
  108. * Accessor method for the commandline the application registered.
  109. * The return is done as a copy by value.
  110. *
  111. * @rvalue CSimpleStringWide |
  112. * The commandline for the registered STI handler.
  113. *****************************************************************************/
  114. CSimpleStringWide StiEventHandlerInfo::getCommandline()
  115. {
  116. return m_cswCommandline;
  117. }
  118. /*****************************************************************************
  119. * @doc INTERNAL
  120. *
  121. * @mfunc CSimpleStringWide | StiEventHandlerInfo | getPreparedCommandline |
  122. *
  123. * The commandline used to start the application, after substituting
  124. * DeviceID and event parameters e.g.
  125. * <nl> The registered commandline may look like: "MyApp.exe /StiDevice:%1 /StiEvent:%2"
  126. * <nl> The prepared commandline returned from this would like something like:
  127. * <nl> "MyApp.exe /StiDevice:{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0001 /StiEvent:{61127f40-e1a5-11d0-b454-00a02438ad48}"
  128. *
  129. * The return is done as a copy by value.
  130. *
  131. * @parm const CSimpleStringWide & | cswDeviceID |
  132. * The device id to put into the commandline.
  133. * @parm const CSimpleStringWide & | cswEventGuid |
  134. * The event guid string to put into the commandline.
  135. *
  136. * @rvalue CSimpleStringWide |
  137. * The commandline for the registered STI handler.
  138. *****************************************************************************/
  139. CSimpleStringWide StiEventHandlerInfo::getPreparedCommandline(
  140. const CSimpleStringWide &cswDeviceID,
  141. const CSimpleStringWide &cswEventGuid)
  142. {
  143. CSimpleStringWide cswCommandline;
  144. //
  145. // Substitute the DeviceID for the sti device token (/StiDevice:%1)
  146. //
  147. cswCommandline = ExpandTokenIntoString(m_cswCommandline,
  148. STI_DEVICE_TOKEN,
  149. cswDeviceID);
  150. //
  151. // Substitute the Event for the sti event token (/StiEvent:%2).
  152. // Note we use cswCommandline as the source because it already
  153. // has the device ID expanded into it.
  154. //
  155. cswCommandline = ExpandTokenIntoString(cswCommandline,
  156. STI_EVENT_TOKEN,
  157. cswEventGuid);
  158. return cswCommandline;
  159. }
  160. /*****************************************************************************
  161. * @doc INTERNAL
  162. *
  163. * @mfunc CSimpleStringWide | StiEventHandlerInfo | ExpandTokenIntoString |
  164. *
  165. * This method inserts a value string in place of a token, similar to how
  166. * printf expands:
  167. * <nl>CHAR *szMyString = "TokenValue";
  168. * <nl>printf("left %s right", szMyString);
  169. * <nl>into the string "left TokenValue right".
  170. *
  171. * This method will only substitute the first matching token.
  172. *
  173. * @parm const CSimpleStringWide & | cswInput |
  174. * The input string containing the tokens to substitute
  175. * @parm const CSimpleStringWide & | cswToken |
  176. * The token we're looking for
  177. * @parm const CSimpleStringWide & | cswTokenValue |
  178. * The value we want to substitute for the token. It does not have to
  179. * be the same size as the token.
  180. *
  181. * @rvalue CSimpleStringWide |
  182. * The resulting string after the substitution.
  183. *****************************************************************************/
  184. CSimpleStringWide StiEventHandlerInfo::ExpandTokenIntoString(
  185. const CSimpleStringWide &cswInput,
  186. const CSimpleStringWide &cswToken,
  187. const CSimpleStringWide &cswTokenValue
  188. )
  189. {
  190. CSimpleString cswExpandedString;
  191. //
  192. // Look for the token start
  193. //
  194. int iTokenStart = cswInput.Find(cswToken, 0);
  195. if (iTokenStart != -1)
  196. {
  197. //
  198. // We found the token, so let's make the substitution.
  199. // The original string looks like this:
  200. // lllllllTokenrrrrrrr
  201. // |
  202. // |
  203. // iTokenStart
  204. // We want the string to look like this:
  205. // lllllllTokenValuerrrrrrr
  206. // Therefore, take everything before the Token, add the token value, then
  207. // everything following the token i.e.
  208. // lllllll + TokenValue + rrrrrrr
  209. // | |
  210. // iTokenStart -1 |
  211. // iTokenStart + Token.length()
  212. //
  213. cswExpandedString = cswInput.SubStr(0, iTokenStart);
  214. cswExpandedString += cswTokenValue;
  215. cswExpandedString += cswInput.SubStr(iTokenStart + cswToken.Length(), -1);
  216. }
  217. else
  218. {
  219. cswExpandedString = cswInput;
  220. }
  221. return cswExpandedString;
  222. }
  223. /*****************************************************************************
  224. * @doc INTERNAL
  225. *
  226. * @mfunc CSimpleStringWide | StiEventHandlerInfo | getPreparedCommandline |
  227. *
  228. * This method is a thin wrapper for the other <mf StiEventHandlerInfo::getPreparedCommandline>.
  229. *
  230. * The commandline used to start the application, after substituting
  231. * DeviceID and event parameters e.g.
  232. * <nl> The registered commandline may look like: "MyApp.exe /StiDevice:%1 /StiEvent:%2"
  233. * <nl> The prepared commandline returned from this would like something like:
  234. * <nl> "MyApp.exe /StiDevice:{6BDD1FC6-810F-11D0-BEC7-08002BE2092F}\0001 /StiEvent:{61127f40-e1a5-11d0-b454-00a02438ad48}"
  235. *
  236. * The return is done as a copy by value.
  237. *
  238. * @parm const CSimpleStringWide & | cswDeviceID |
  239. * The device id to put into the commandline.
  240. * @parm const CSimpleStringWide & | cswEventGuid |
  241. * The event guid string to put into the commandline.
  242. *
  243. * @rvalue CSimpleStringWide |
  244. * The commandline for the registered STI handler.
  245. *****************************************************************************/
  246. CSimpleStringWide StiEventHandlerInfo::getPreparedCommandline(
  247. const CSimpleStringWide &cswDeviceID,
  248. const GUID &guidEvent)
  249. {
  250. CSimpleStringWide cswEventGuidString;
  251. WCHAR wszGuid[40];
  252. if (StringFromGUID2(guidEvent, wszGuid, sizeof(wszGuid)/sizeof(wszGuid[0])))
  253. {
  254. wszGuid[(sizeof(wszGuid)/sizeof(wszGuid[0])) - 1] = L'\0';
  255. cswEventGuidString = wszGuid;
  256. }
  257. return getPreparedCommandline(cswDeviceID, cswEventGuidString);
  258. }
  259. /*****************************************************************************
  260. * @doc INTERNAL
  261. *
  262. * @mfunc VOID | StiEventHandlerInfo | Dump |
  263. *
  264. * Description goes here
  265. *
  266. * @parm LONG | lFlags |
  267. * Operational flags
  268. * @flag 0 | No switches
  269. *
  270. * @rvalue S_OK |
  271. * The method succeeded.
  272. *****************************************************************************/
  273. VOID StiEventHandlerInfo::Dump(
  274. )
  275. {
  276. DBG_TRC(("Sti registration for"));
  277. DBG_TRC((" Name: %ws", getAppName().String()));
  278. DBG_TRC((" Commandline: %ws\n", getCommandline().String()));
  279. }