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.

384 lines
9.7 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation
  3. Module Name:
  4. GENERAL.CPP
  5. Abstract:
  6. Source file that that contains general functions implementation.
  7. Author:
  8. Vasundhara .G
  9. Revision History:
  10. Vasundhara .G 9-oct-2k : Created It.
  11. --*/
  12. #include "pch.h"
  13. #include "EventConsumerProvider.h"
  14. #include "General.h"
  15. #include "resource.h"
  16. extern HMODULE g_hModule;
  17. HRESULT
  18. PropertyGet(
  19. IN IWbemClassObject* pWmiObject,
  20. IN LPCTSTR szProperty,
  21. IN DWORD dwType,
  22. OUT LPVOID pValue,
  23. IN DWORD dwSize
  24. )
  25. /*++
  26. Routine Description:
  27. Get the value of a property for the given instance .
  28. Arguments:
  29. [IN] pWmiObject - A pointer to wmi class.
  30. [IN] szProperty - property name whose value to be returned.
  31. [IN] dwType - Data Type of the property.
  32. [OUT] pValue - Variable to hold the data.
  33. [IN] dwSize - size of the variable.
  34. Return Value:
  35. HRESULT value.
  36. --*/
  37. {
  38. // local variables
  39. HRESULT hr = S_OK;
  40. VARIANT varValue;
  41. LPWSTR pwszValue = NULL;
  42. WCHAR wszProperty[ MAX_STRING_LENGTH ] = L"\0";
  43. // value should not be NULL
  44. if ( NULL == pValue || NULL == szProperty || NULL == pWmiObject )
  45. {
  46. return E_FAIL;
  47. }
  48. // initialize the values with zeros ... to be on safe side
  49. SecureZeroMemory( pValue, dwSize );
  50. SecureZeroMemory( wszProperty, MAX_STRING_LENGTH );
  51. // get the property name in UNICODE version
  52. StringCopy( wszProperty, szProperty, MAX_STRING_LENGTH );
  53. // initialize the variant and then get the value of the specified property
  54. VariantInit( &varValue );
  55. hr = pWmiObject->Get( wszProperty, 0, &varValue, NULL, NULL );
  56. if ( FAILED( hr ) )
  57. {
  58. // clear the variant variable
  59. VariantClear( &varValue );
  60. // failed to get the value for the property
  61. return hr;
  62. }
  63. // get and put the value
  64. switch( varValue.vt )
  65. {
  66. case VT_EMPTY:
  67. case VT_NULL:
  68. break;
  69. case VT_I2:
  70. *( ( short* ) pValue ) = V_I2( &varValue );
  71. break;
  72. case VT_I4:
  73. *( ( long* ) pValue ) = V_I4( &varValue );
  74. break;
  75. case VT_R4:
  76. *( ( float* ) pValue ) = V_R4( &varValue );
  77. break;
  78. case VT_R8:
  79. *( ( double* ) pValue ) = V_R8( &varValue );
  80. break;
  81. case VT_UI1:
  82. *( ( UINT* ) pValue ) = V_UI1( &varValue );
  83. break;
  84. case VT_BSTR:
  85. {
  86. // get the unicode value
  87. pwszValue = V_BSTR( &varValue );
  88. // get the comptable string
  89. StringCopy( ( LPTSTR ) pValue, pwszValue, dwSize );
  90. break;
  91. }
  92. default:
  93. break;
  94. }
  95. // clear the variant variable
  96. VariantClear( &varValue );
  97. // inform success
  98. return S_OK;
  99. }
  100. VOID
  101. ErrorLog(
  102. IN LPCTSTR lpErrString,
  103. IN LPWSTR lpTrigName,
  104. IN DWORD dwID
  105. )
  106. /*++
  107. Routine Description:
  108. To write the log into log file.
  109. Arguments:
  110. [IN] lpErrString - text that hold the status of creating a trigger.
  111. [IN] lpTrigName - trigger name.
  112. [IN] dwID - TriggerID.
  113. Return Value:
  114. none.
  115. --*/
  116. {
  117. LPTSTR lpTemp = NULL;
  118. LPSTR lpFilePath = NULL;
  119. FILE *fLogFile = NULL;
  120. DWORD dwResult = 0;
  121. LPTSTR lpResStr = NULL;
  122. if( ( NULL == lpErrString ) || ( NULL == lpTrigName ) )
  123. {
  124. return;
  125. }
  126. lpResStr = ( LPTSTR ) AllocateMemory( ( MAX_RES_STRING1 + 1 ) * sizeof( TCHAR ) );
  127. lpTemp = ( LPTSTR )AllocateMemory( ( MAX_RES_STRING1 ) * sizeof( TCHAR ) );
  128. if( ( NULL == lpTemp ) || ( NULL == lpResStr ) )
  129. {
  130. FREESTRING( lpTemp );
  131. FREESTRING( lpResStr );
  132. return;
  133. }
  134. dwResult = GetWindowsDirectory( lpTemp, MAX_RES_STRING1 );
  135. if( 0 == dwResult )
  136. {
  137. FREESTRING( lpTemp );
  138. FREESTRING( lpResStr );
  139. return;
  140. }
  141. StringConcatEx( lpTemp, LOG_FILE_PATH );
  142. CreateDirectory( lpTemp, NULL );
  143. StringConcatEx( lpTemp, LOG_FILE );
  144. lpFilePath = ( LPSTR )AllocateMemory( ( MAX_RES_STRING1 ) * sizeof( TCHAR ) );
  145. if( NULL == lpFilePath )
  146. {
  147. FREESTRING( lpTemp );
  148. FREESTRING( lpResStr );
  149. return;
  150. }
  151. dwResult = MAX_RES_STRING1;
  152. GetAsMultiByteString2( lpTemp, lpFilePath, &dwResult );
  153. SecureZeroMemory( lpTemp, MAX_RES_STRING * sizeof( TCHAR ) );
  154. if ( (fLogFile = fopen( lpFilePath, "a" )) != NULL )
  155. {
  156. LPSTR lpReason = NULL;
  157. lpReason = ( LPSTR )AllocateMemory( ( MAX_RES_STRING1 ) * sizeof( TCHAR ) );
  158. if( NULL == lpReason )
  159. {
  160. FREESTRING( lpTemp );
  161. FREESTRING( lpResStr );
  162. FREESTRING( lpFilePath );
  163. fclose( fLogFile );
  164. return;
  165. }
  166. BOOL bFlag = GetFormattedTime( lpTemp );
  167. if( FALSE == bFlag )
  168. {
  169. FREESTRING( lpResStr );
  170. FREESTRING( lpFilePath );
  171. return;
  172. }
  173. ShowMessage( fLogFile, NEW_LINE );
  174. ShowMessage( fLogFile, lpTemp );
  175. SecureZeroMemory( lpTemp, MAX_RES_STRING1 * sizeof( TCHAR ) );
  176. LoadStringW( g_hModule, IDS_TRIGGERNAME, lpResStr, MAX_RES_STRING1 );
  177. StringCopyEx( lpTemp, lpResStr );
  178. StringConcatEx( lpTemp, lpTrigName );
  179. ShowMessage( fLogFile, NEW_LINE );
  180. ShowMessage( fLogFile, lpTemp );
  181. SecureZeroMemory( lpTemp, MAX_RES_STRING1 * sizeof( TCHAR ) );
  182. LoadStringW( g_hModule, IDS_TRIGGERID, lpResStr, MAX_RES_STRING1 );
  183. StringCchPrintf( lpTemp, MAX_RES_STRING1, lpResStr, dwID );
  184. ShowMessage( fLogFile, NEW_LINE );
  185. ShowMessage( fLogFile, lpTemp );
  186. SecureZeroMemory( lpTemp, MAX_RES_STRING1 * sizeof( TCHAR ) );
  187. StringConcatEx( lpTemp, lpErrString );
  188. ShowMessage( fLogFile, NEW_LINE );
  189. ShowMessage( fLogFile, lpTemp );
  190. FREESTRING( lpReason );
  191. fclose( fLogFile );
  192. }
  193. FREESTRING( lpTemp );
  194. FREESTRING( lpResStr );
  195. FREESTRING( lpFilePath );
  196. }
  197. BOOL
  198. GetFormattedTime(
  199. OUT LPTSTR lpDate
  200. )
  201. /*++
  202. Routine Description:
  203. Get the system date and time in specified format .
  204. Arguments:
  205. [OUT] lpDate - string that holds the current date.
  206. Return Value:
  207. None.
  208. --*/
  209. {
  210. TCHAR szTime[MAX_STRING_LENGTH];
  211. INT cch = 0;
  212. if( NULL == lpDate )
  213. {
  214. return FALSE;
  215. }
  216. cch = GetDateFormat( LOCALE_USER_DEFAULT, 0, NULL, DATE_FORMAT, szTime, SIZE_OF_ARRAY( szTime ) );
  217. if( 0 == cch )
  218. {
  219. return FALSE;
  220. }
  221. // cch includes null terminator, change it to a space to separate from time.
  222. szTime[ cch - 1 ] = ' ';
  223. // Get time and format to characters
  224. cch = GetTimeFormat( LOCALE_USER_DEFAULT, NULL, NULL, TIME_FORMAT, szTime + cch, SIZE_OF_ARRAY( szTime ) - cch );
  225. if( 0 == cch )
  226. {
  227. return FALSE;
  228. }
  229. StringCopyEx( lpDate, ( LPTSTR )szTime );
  230. return TRUE;
  231. }
  232. BOOL
  233. ProcessFilePath(
  234. IN LPTSTR szInput,
  235. OUT LPTSTR szFirstString,
  236. OUT LPTSTR szSecondString
  237. )
  238. /*++
  239. Routine Description:
  240. This routine splits the input parameters into 2 substrings and returns it.
  241. Arguments:
  242. [IN] szInput : Input string.
  243. [OUT] szFirstString : First Output string containing the path of the
  244. file.
  245. [OUT] szSecondString : The second output containing the paramters.
  246. Return Value :
  247. A BOOL value indicating TRUE on success else FALSE
  248. on failure
  249. --*/
  250. {
  251. WCHAR *pszSep = NULL ;
  252. WCHAR szTmpString[MAX_RES_STRING] = L"\0";
  253. WCHAR szTmpInStr[MAX_RES_STRING] = L"\0";
  254. WCHAR szTmpOutStr[MAX_RES_STRING] = L"\0";
  255. WCHAR szTmpString1[MAX_RES_STRING] = L"\0";
  256. DWORD dwCnt = 0 ;
  257. DWORD dwLen = 0 ;
  258. #ifdef _WIN64
  259. INT64 dwPos ;
  260. #else
  261. DWORD dwPos ;
  262. #endif
  263. //checking if the input parameters are NULL and if so
  264. // return FAILURE. This condition will not come
  265. // but checking for safety sake.
  266. if( (szInput == NULL) || (StringLength(szInput, 0)==0))
  267. {
  268. return FALSE ;
  269. }
  270. StringCopy(szTmpString, szInput, SIZE_OF_ARRAY(szTmpString));
  271. StringCopy(szTmpString1, szInput, SIZE_OF_ARRAY(szTmpString1));
  272. StringCopy(szTmpInStr, szInput, SIZE_OF_ARRAY(szTmpInStr));
  273. // check for first double quote (")
  274. if ( szTmpInStr[0] == _T('\"') )
  275. {
  276. // trim the first double quote
  277. StrTrim( szTmpInStr, _T("\""));
  278. // check for end double quote
  279. pszSep = (LPWSTR)FindChar(szTmpInStr,_T('\"'), 0) ;
  280. // get the position
  281. dwPos = pszSep - szTmpInStr + 1;
  282. }
  283. else
  284. {
  285. // check for the space
  286. pszSep = (LPWSTR)FindChar(szTmpInStr, _T(' '), 0) ;
  287. // get the position
  288. dwPos = pszSep - szTmpInStr;
  289. }
  290. if ( pszSep != NULL )
  291. {
  292. szTmpInStr[dwPos] = _T('\0');
  293. }
  294. else
  295. {
  296. StringCopy(szFirstString, szTmpString, MAX_RES_STRING);
  297. StringCopy(szSecondString, L"\0", MAX_RES_STRING);
  298. return TRUE;
  299. }
  300. // intialize the variable
  301. dwCnt = 0 ;
  302. // get the length of the string
  303. dwLen = StringLength ( szTmpString, 0 );
  304. // check for end of string
  305. while ( ( dwPos <= dwLen ) && szTmpString[dwPos++] != _T('\0') )
  306. {
  307. szTmpOutStr[dwCnt++] = szTmpString[dwPos];
  308. }
  309. // trim the executable and arguments
  310. StrTrim( szTmpInStr, _T("\""));
  311. StrTrim( szTmpInStr, _T(" "));
  312. StringCopy(szFirstString, szTmpInStr, MAX_RES_STRING);
  313. StringCopy(szSecondString, szTmpOutStr, MAX_RES_STRING);
  314. // return success
  315. return TRUE;
  316. }