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.

486 lines
13 KiB

  1. //***************************************************************************
  2. // Copyright (c) Microsoft Corporation
  3. //
  4. // Module Name:
  5. // GENERAL.CPP
  6. //
  7. // Abstract:
  8. // Source file that that contains general functions implementation.
  9. //
  10. // Author:
  11. // Vasundhara .G
  12. //
  13. // Revision History:
  14. // Vasundhara .G 9-oct-2k : Created It.
  15. //***************************************************************************
  16. #include "pch.h"
  17. #include "EventConsumerProvider.h"
  18. #include "General.h"
  19. #include "resource.h"
  20. extern HMODULE g_hModule;
  21. //
  22. // internal functions ... private
  23. //
  24. //***************************************************************************
  25. // Routine Description:
  26. // Returns the variant string for a variable of type LPCTSTR .
  27. //
  28. // Arguments:
  29. // pszValue [in] - A LPCTSTR value to convert into varaint type.
  30. // pVariant [in/out] - A VARAINT type variable which hold the variant
  31. // type for the given LPCTSTR string.
  32. //
  33. // Return Value:
  34. // A Variant type string.
  35. //***************************************************************************
  36. inline VARIANT* AsVariant( LPCTSTR pszValue, VARIANT* pVariant )
  37. {
  38. // local variables
  39. WCHAR wszValue[ MAX_STRING_LENGTH + 1 ] = L"\0";
  40. // set the variant structure
  41. VariantInit( pVariant );
  42. pVariant->vt = VT_BSTR;
  43. pVariant->bstrVal = GetAsUnicodeString( pszValue, wszValue, MAX_STRING_LENGTH );
  44. // return the out parameter itself as the return value
  45. return pVariant;
  46. }
  47. //***************************************************************************
  48. // Routine Description:
  49. // Returns the variant value for a variable of type DWORD .
  50. //
  51. // Arguments:
  52. // dwValue [in] - A DWORD value to convert into varaint type.
  53. // pVariant [in/out] - A VARAINT type variable which hold the variant
  54. // type for the given DWORD.
  55. //
  56. // Return Value:
  57. // A Variant type string.
  58. //***************************************************************************
  59. inline VARIANT* AsVariant( DWORD dwValue, VARIANT* pVariant )
  60. {
  61. // set the variant structure
  62. VariantInit( pVariant );
  63. pVariant->vt = VT_UI1;
  64. pVariant->ulVal = dwValue;
  65. // return the out parameter itself as the return value
  66. return pVariant;
  67. }
  68. //***************************************************************************
  69. // Routine Description:
  70. // Get the value of a property for the given instance .
  71. //
  72. // Arguments:
  73. // pWmiObject[in] - A pointer to wmi class.
  74. // szProperty [in] - property name whose value to be returned.
  75. // dwType [in] - Data Type of the property.
  76. // pValue [in/out] - Variable to hold the data.
  77. // dwSize [in] - size of the variable.
  78. //
  79. // Return Value:
  80. // HRESULT value.
  81. //***************************************************************************
  82. HRESULT PropertyGet( IWbemClassObject* pWmiObject,
  83. LPCTSTR szProperty,
  84. DWORD dwType, LPVOID pValue, DWORD dwSize )
  85. {
  86. // local variables
  87. HRESULT hr = S_OK;
  88. VARIANT varValue;
  89. LPWSTR pwszValue = NULL;
  90. WCHAR wszProperty[ MAX_STRING_LENGTH ] = L"\0";
  91. // value should not be NULL
  92. if ( pValue == NULL )
  93. {
  94. return S_FALSE;
  95. }
  96. // initialize the values with zeros ... to be on safe side
  97. memset( pValue, 0, dwSize );
  98. memset( wszProperty, 0, MAX_STRING_LENGTH );
  99. // get the property name in UNICODE version
  100. GetAsUnicodeString( szProperty, wszProperty, MAX_STRING_LENGTH );
  101. // initialize the variant and then get the value of the specified property
  102. VariantInit( &varValue );
  103. hr = pWmiObject->Get( wszProperty, 0, &varValue, NULL, NULL );
  104. if ( FAILED( hr ) )
  105. {
  106. // clear the variant variable
  107. VariantClear( &varValue );
  108. // failed to get the value for the property
  109. return hr;
  110. }
  111. // get and put the value
  112. switch( varValue.vt )
  113. {
  114. case VT_EMPTY:
  115. case VT_NULL:
  116. break;
  117. case VT_I2:
  118. *( ( short* ) pValue ) = V_I2( &varValue );
  119. break;
  120. case VT_I4:
  121. *( ( long* ) pValue ) = V_I4( &varValue );
  122. break;
  123. case VT_R4:
  124. *( ( float* ) pValue ) = V_R4( &varValue );
  125. break;
  126. case VT_R8:
  127. *( ( double* ) pValue ) = V_R8( &varValue );
  128. break;
  129. case VT_UI1:
  130. *( ( UINT* ) pValue ) = V_UI1( &varValue );
  131. break;
  132. case VT_BSTR:
  133. {
  134. // get the unicode value
  135. pwszValue = V_BSTR( &varValue );
  136. // get the comptable string
  137. GetCompatibleStringFromUnicode( pwszValue, ( LPTSTR ) pValue, dwSize );
  138. break;
  139. }
  140. }
  141. // clear the variant variable
  142. VariantClear( &varValue );
  143. // inform success
  144. return S_OK;
  145. }
  146. //***************************************************************************
  147. // Routine Description:
  148. // putt the value of a property for the given instance .
  149. //
  150. // Arguments:
  151. // pWmiObject[in] - A pointer to wmi class.
  152. // szProperty [in] - property name whose value to be set.
  153. // SZValue [in/out] - Variable that hold the data.
  154. //
  155. // Return Value:
  156. // HRESULT value.
  157. //***************************************************************************
  158. HRESULT PropertyPut( IWbemClassObject* pWmiObject, LPCTSTR szProperty, LPCTSTR szValue )
  159. {
  160. // local variables
  161. HRESULT hr;
  162. VARIANT var;
  163. WCHAR wszProperty[ MAX_STRING_LENGTH + 1 ] = L"\0";
  164. // put the value
  165. hr = pWmiObject->Put( GetAsUnicodeString( szProperty, wszProperty, MAX_STRING_LENGTH ),
  166. 0, AsVariant( szValue, &var ), VT_BSTR );
  167. // clear the variant
  168. VariantClear( &var );
  169. // now check the result of 'put' operation
  170. if ( FAILED( hr ) )
  171. {
  172. return hr; // put has failed
  173. }
  174. // put is success ... inform the same
  175. return S_OK;
  176. }
  177. //***************************************************************************
  178. // Routine Description:
  179. // put the value of a property for the given instance .
  180. //
  181. // Arguments:
  182. // pWmiObject[in] - A pointer to wmi class.
  183. // szProperty [in] - property name whose value to be set.
  184. // dwValue [in] - Variable that hold the data.
  185. //
  186. // Return Value:
  187. // HRESULT value.
  188. //***************************************************************************
  189. HRESULT PropertyPut( IWbemClassObject* pWmiObject, LPCTSTR szProperty, DWORD dwValue )
  190. {
  191. // local variables
  192. HRESULT hr = S_OK;
  193. VARIANT var;
  194. WCHAR wszProperty[ MAX_STRING_LENGTH + 1 ] = L"\0";
  195. // put the value
  196. hr = pWmiObject->Put( GetAsUnicodeString( szProperty, wszProperty, MAX_STRING_LENGTH ),
  197. 0, AsVariant( dwValue, &var ), VT_UI1 );
  198. // clear the variant
  199. VariantClear( &var );
  200. // now check the result of 'put' operation
  201. if ( FAILED( hr ) )
  202. {
  203. return hr; // put has failed
  204. }
  205. // put is success ... inform the same
  206. return S_OK;
  207. }
  208. //***************************************************************************
  209. // Routine Description:
  210. // To write the log into log file.
  211. //
  212. // Arguments:
  213. // lpErrString [in] - text that hold the status of creating a trigger.
  214. // lpTrigName [in] - trigger name.
  215. //
  216. // Return Value:
  217. // none.
  218. //***************************************************************************
  219. VOID ErrorLog( LPCTSTR lpErrString, LPWSTR lpTrigName, DWORD dwID )
  220. {
  221. LPTSTR lpTemp = NULL;
  222. LPSTR lpFilePath = NULL;
  223. FILE *fLogFile = NULL;
  224. DWORD dwResult = 0;
  225. LPTSTR lpResStr = NULL;
  226. if( ( lpErrString == NULL ) || ( lpTrigName == NULL ) )
  227. return;
  228. lpResStr = ( LPTSTR ) __calloc( MAX_RES_STRING1 + 1, sizeof( TCHAR ) );
  229. lpTemp = ( LPTSTR )calloc( MAX_RES_STRING1, sizeof( TCHAR ) );
  230. if( ( lpTemp == NULL ) || ( lpResStr == NULL ) )
  231. {
  232. FREESTRING( lpTemp );
  233. FREESTRING( lpResStr );
  234. return;
  235. }
  236. dwResult = GetWindowsDirectory( lpTemp, MAX_RES_STRING1 );
  237. if( dwResult == 0 )
  238. {
  239. FREESTRING( lpTemp );
  240. FREESTRING( lpResStr );
  241. return;
  242. }
  243. lstrcat( lpTemp, LOG_FILE_PATH );
  244. CreateDirectory( lpTemp, NULL );
  245. lstrcat( lpTemp, LOG_FILE );
  246. lpFilePath = ( LPSTR )calloc( MAX_RES_STRING1, sizeof( TCHAR ) );
  247. if( lpFilePath == NULL )
  248. {
  249. FREESTRING( lpTemp );
  250. FREESTRING( lpResStr );
  251. return;
  252. }
  253. GetAsMultiByteString( lpTemp, lpFilePath, MAX_RES_STRING1 );
  254. memset( lpTemp, 0, MAX_RES_STRING * sizeof( TCHAR ) );
  255. if ( (fLogFile = fopen( lpFilePath, "a" )) != NULL )
  256. {
  257. LPSTR lpReason = NULL;
  258. lpReason = ( LPSTR )calloc( MAX_RES_STRING1, sizeof( TCHAR ) );
  259. if( lpReason == NULL )
  260. {
  261. FREESTRING( lpTemp );
  262. FREESTRING( lpResStr );
  263. FREESTRING( lpFilePath );
  264. fclose( fLogFile );
  265. return;
  266. }
  267. GetFormattedTime( lpTemp );
  268. if( lpTemp == NULL )
  269. {
  270. FREESTRING( lpResStr );
  271. FREESTRING( lpFilePath );
  272. return;
  273. }
  274. GetAsMultiByteString( NEW_LINE, lpReason, MAX_RES_STRING1 );
  275. fprintf( fLogFile, lpReason );
  276. GetAsMultiByteString( lpTemp, lpReason, MAX_RES_STRING1 );
  277. fprintf( fLogFile, lpReason );
  278. memset( lpTemp, 0, MAX_RES_STRING1 * sizeof( TCHAR ) );
  279. LoadStringW( g_hModule, IDS_TRIGGERNAME, lpResStr, MAX_RES_STRING1 );
  280. lstrcpy( lpTemp, lpResStr );
  281. lstrcat( lpTemp, lpTrigName );
  282. GetAsMultiByteString( NEW_LINE, lpReason, MAX_RES_STRING1 );
  283. fprintf( fLogFile, lpReason );
  284. GetAsMultiByteString( lpTemp, lpReason, MAX_RES_STRING1 );
  285. fprintf( fLogFile, lpReason );
  286. memset( lpTemp, 0, MAX_RES_STRING1 * sizeof( TCHAR ) );
  287. LoadStringW( g_hModule, IDS_TRIGGERID, lpResStr, MAX_RES_STRING1 );
  288. wsprintf( lpTemp, lpResStr, dwID );
  289. GetAsMultiByteString( NEW_LINE, lpReason, MAX_RES_STRING1 );
  290. fprintf( fLogFile, lpReason );
  291. GetAsMultiByteString( lpTemp, lpReason, MAX_RES_STRING1 );
  292. fprintf( fLogFile, lpReason );
  293. memset( lpTemp, 0, MAX_RES_STRING1 * sizeof( TCHAR ) );
  294. lstrcat( lpTemp, lpErrString );
  295. GetAsMultiByteString( NEW_LINE, lpReason, MAX_RES_STRING1 );
  296. fprintf( fLogFile, lpReason );
  297. GetAsMultiByteString( lpTemp, lpReason, MAX_RES_STRING1 );
  298. fprintf( fLogFile, lpReason );
  299. GetAsMultiByteString( NEW_LINE, lpReason, MAX_RES_STRING1 );
  300. fprintf( fLogFile, lpReason );
  301. free( lpReason );
  302. fclose( fLogFile );
  303. }
  304. FREESTRING( lpTemp );
  305. FREESTRING( lpResStr );
  306. FREESTRING( lpFilePath );
  307. }
  308. //***************************************************************************
  309. // Routine Description:
  310. // Get the system date and time in specified format .
  311. //
  312. // Arguments:
  313. // lpDate [in/out] - string that holds the current date.
  314. //
  315. // Return Value:
  316. // None.
  317. //***************************************************************************
  318. VOID GetFormattedTime( LPTSTR lpDate )
  319. {
  320. TCHAR szTime[MAX_STRING_LENGTH];
  321. INT cch = 0;
  322. if( lpDate == NULL )
  323. return;
  324. cch = GetDateFormat( LOCALE_USER_DEFAULT, 0, NULL, DATE_FORMAT, szTime, SIZE_OF_ARRAY( szTime ) );
  325. // cch includes null terminator, change it to a space to separate from time.
  326. szTime[ cch - 1 ] = ' ';
  327. // Get time and format to characters
  328. GetTimeFormat( LOCALE_USER_DEFAULT, NULL, NULL, TIME_FORMAT, szTime + cch, SIZE_OF_ARRAY( szTime ) - cch );
  329. lstrcpy( lpDate, ( LPTSTR )szTime );
  330. return;
  331. }
  332. /******************************************************************************
  333. // Routine Description:
  334. // This routine splits the input parameters into 2 substrings and returns it.
  335. //
  336. // Arguments:
  337. // szInput [in] : Input string.
  338. // szFirstString [in/out] : First Output string containing the path of the
  339. // file.
  340. // szSecondString [in/out]: The second output containing the paramters.
  341. //
  342. // Return Value :
  343. // A BOOL value indicating TRUE on success else FALSE
  344. // on failure
  345. ******************************************************************************/
  346. BOOL ProcessFilePath( LPTSTR szInput, LPTSTR szFirstString, LPTSTR szSecondString )
  347. {
  348. _TCHAR *pszTok = NULL ;
  349. _TCHAR *pszSep = NULL ;
  350. _TCHAR szTmpString[MAX_RES_STRING] = NULL_STRING;
  351. _TCHAR szTmpInStr[MAX_RES_STRING] = NULL_STRING;
  352. _TCHAR szTmpOutStr[MAX_RES_STRING] = NULL_STRING;
  353. _TCHAR szTmpString1[MAX_RES_STRING] = NULL_STRING;
  354. DWORD dwCnt = 0 ;
  355. DWORD dwLen = 0 ;
  356. #ifdef _WIN64
  357. INT64 dwPos ;
  358. #else
  359. DWORD dwPos ;
  360. #endif
  361. //checking if the input parameters are NULL and if so
  362. // return FAILURE. This condition will not come
  363. // but checking for safety sake.
  364. if( ( szInput == NULL ) || ( _tcslen( szInput ) == 0 ) )
  365. {
  366. return FALSE;
  367. }
  368. _tcscpy( szTmpString, szInput );
  369. _tcscpy( szTmpString1, szInput );
  370. _tcscpy( szTmpInStr, szInput );
  371. // check for first double quote (")
  372. if ( szTmpInStr[0] == SINGLE_QUOTE_CHAR )
  373. {
  374. // trim the first double quote
  375. StrTrim( szTmpInStr, SINGLE_QUOTE_STRING );
  376. // check for end double quote
  377. pszSep = _tcschr( szTmpInStr, SINGLE_QUOTE_CHAR ) ;
  378. // get the position
  379. dwPos = pszSep - szTmpInStr + 1;
  380. }
  381. else
  382. {
  383. // check for the space
  384. pszSep = _tcschr( szTmpInStr, CHAR_SPACE ) ;
  385. // get the position
  386. dwPos = pszSep - szTmpInStr;
  387. }
  388. if ( pszSep != NULL )
  389. {
  390. szTmpInStr[dwPos] = NULL_CHAR;
  391. }
  392. else
  393. {
  394. _tcscpy( szFirstString, szTmpString );
  395. _tcscpy( szSecondString, NULL_STRING );
  396. return TRUE;
  397. }
  398. // intialize the variable
  399. dwCnt = 0 ;
  400. // get the length of the string
  401. dwLen = _tcslen ( szTmpString );
  402. // check for end of string
  403. while ( ( dwPos <= dwLen ) && szTmpString[dwPos++] != NULL_CHAR )
  404. {
  405. szTmpOutStr[dwCnt++] = szTmpString[dwPos];
  406. }
  407. // trim the executable and arguments
  408. StrTrim( szTmpInStr, SINGLE_QUOTE_STRING );
  409. StrTrim( szTmpInStr, STRING_SPACE );
  410. _tcscpy( szFirstString, szTmpInStr );
  411. _tcscpy( szSecondString, szTmpOutStr );
  412. // return success
  413. return TRUE;
  414. }