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.

259 lines
6.3 KiB

  1. #define INITGUID
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <ole2.h>
  5. #include "mdsync.h"
  6. //
  7. // HRESULTTOWIN32() maps an HRESULT to a Win32 error. If the facility code
  8. // of the HRESULT is FACILITY_WIN32, then the code portion (i.e. the
  9. // original Win32 error) is returned. Otherwise, the original HRESULT is
  10. // returned unchagned.
  11. //
  12. #define HRESULTTOWIN32(hres) \
  13. ((HRESULT_FACILITY(hres) == FACILITY_WIN32) \
  14. ? HRESULT_CODE(hres) \
  15. : (hres))
  16. BOOL g_fDone = FALSE;
  17. DWORD g_dwTarget = 0;
  18. SYNC_STAT* g_pStat = NULL;
  19. CHAR g_achMsg1[128];
  20. CHAR g_achMsg2[128];
  21. CHAR g_achMsg3[128];
  22. CHAR g_achMsg4[128];
  23. CHAR g_achMsg5[128];
  24. CHAR g_achMsg6[128];
  25. CHAR g_achMsg7[128];
  26. CHAR g_achMsg8[128];
  27. CHAR g_achMsg9[128];
  28. DWORD
  29. WINAPI
  30. Monitor(
  31. LPVOID pV
  32. )
  33. {
  34. for ( ;; )
  35. {
  36. printf( g_achMsg1,
  37. g_pStat->m_dwSourceScan,
  38. g_pStat->m_fSourceComplete ? g_achMsg2 : g_achMsg3 );
  39. for ( UINT i = 0 ; i < g_dwTarget ; ++i )
  40. {
  41. printf( "(%d,%d), ", g_pStat->m_adwTargets[i*2], g_pStat->m_adwTargets[i*2+1] );
  42. }
  43. printf( "\r" );
  44. if ( g_fDone )
  45. {
  46. break;
  47. }
  48. Sleep( 1000 );
  49. }
  50. return 0;
  51. }
  52. /* INTRINSA ignore = all */
  53. VOID
  54. DisplayErrorMessage(
  55. DWORD dwErr
  56. )
  57. {
  58. LPSTR pErr;
  59. if ( FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
  60. NULL,
  61. dwErr,
  62. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  63. (LPSTR)&pErr,
  64. 0,
  65. NULL ) )
  66. {
  67. LPSTR p;
  68. if ( p = strchr( pErr, '\r' ) )
  69. {
  70. *p = '\0';
  71. }
  72. fputs( pErr, stdout );
  73. LocalFree( pErr );
  74. }
  75. else
  76. {
  77. fputs( g_achMsg9, stdout );
  78. }
  79. }
  80. int __cdecl main( int argc, char*argv[] )
  81. {
  82. IMdSync* pIf;
  83. LPDWORD pdwErr;
  84. HANDLE hThread;
  85. DWORD dwThreadId;
  86. LPSTR pTargets;
  87. int iA;
  88. UINT cT;
  89. int Status = 0;
  90. HRESULT hRes;
  91. DWORD dwFlags = 0;
  92. DWORD iT;
  93. BOOL fAtLeastOnError = FALSE;
  94. if ( argc < 2 )
  95. {
  96. CHAR achMsg[2048];
  97. if ( LoadString( NULL, 100, achMsg, sizeof(achMsg) ) )
  98. {
  99. printf( achMsg );
  100. }
  101. return 3;
  102. }
  103. if ( !LoadString( NULL, 101, g_achMsg1, sizeof(g_achMsg1) ) ||
  104. !LoadString( NULL, 102, g_achMsg2, sizeof(g_achMsg2) ) ||
  105. !LoadString( NULL, 103, g_achMsg3, sizeof(g_achMsg3) ) ||
  106. !LoadString( NULL, 105, g_achMsg5, sizeof(g_achMsg5) ) ||
  107. !LoadString( NULL, 106, g_achMsg6, sizeof(g_achMsg6) ) ||
  108. !LoadString( NULL, 107, g_achMsg7, sizeof(g_achMsg7) ) ||
  109. !LoadString( NULL, 108, g_achMsg8, sizeof(g_achMsg8) ) ||
  110. !LoadString( NULL, 109, g_achMsg9, sizeof(g_achMsg9) ) ||
  111. !LoadString( NULL, 104, g_achMsg4, sizeof(g_achMsg4) ) )
  112. {
  113. DisplayErrorMessage( GetLastError() );
  114. return 1;
  115. }
  116. //
  117. // Count targets, get target name length
  118. //
  119. for ( cT = 1, g_dwTarget=0, iA = 1 ; iA < argc ; ++iA )
  120. {
  121. if ( argv[iA][0] == '-' )
  122. {
  123. switch ( argv[iA][1] )
  124. {
  125. case 'c':
  126. dwFlags |= MD_SYNC_FLAG_CHECK_ADMINEX_SIGNATURE;
  127. break;
  128. }
  129. }
  130. else
  131. {
  132. cT += strlen( argv[iA] ) + 1;
  133. ++g_dwTarget;
  134. }
  135. }
  136. if ( !(g_pStat = (SYNC_STAT*)LocalAlloc( LMEM_FIXED,
  137. sizeof(SYNC_STAT)+sizeof(DWORD)*2*g_dwTarget )) ||
  138. !(pTargets = (LPSTR)LocalAlloc( LMEM_FIXED, cT )) ||
  139. !(pdwErr = (LPDWORD)LocalAlloc( LMEM_FIXED, sizeof(DWORD)*g_dwTarget)) )
  140. {
  141. DisplayErrorMessage( GetLastError() );
  142. return 1;
  143. }
  144. memset ( g_pStat, '\0', sizeof(SYNC_STAT)+sizeof(DWORD)*2*g_dwTarget );
  145. //
  146. // Create target string
  147. //
  148. for ( cT = 0, iA = 1 ; iA < argc ; ++iA )
  149. {
  150. if ( argv[iA][0] != '-' )
  151. {
  152. strcpy( pTargets + cT, argv[iA] );
  153. cT += strlen( argv[iA] ) + 1;
  154. }
  155. }
  156. pTargets[cT] = '\0';
  157. //
  158. // call synchronize method
  159. //
  160. CoInitializeEx( NULL, COINIT_MULTITHREADED );
  161. if ( SUCCEEDED( hRes = CoCreateInstance( CLSID_MDSync, NULL, CLSCTX_INPROC_SERVER, IID_IMDSync, (LPVOID*)&pIf ) ) )
  162. {
  163. hThread = CreateThread( NULL, 0, ::Monitor, NULL, 0, &dwThreadId );
  164. hRes = pIf->Synchronize( pTargets, pdwErr, dwFlags, (LPDWORD)g_pStat );
  165. g_fDone = TRUE;
  166. if ( hThread )
  167. {
  168. WaitForSingleObject( hThread, INFINITE );
  169. }
  170. printf( "\n" );
  171. pIf->Release();
  172. if ( FAILED( hRes ) && hRes != E_FAIL )
  173. {
  174. DisplayErrorMessage( HRESULTTOWIN32( hRes ) );
  175. Status = 2;
  176. }
  177. else
  178. {
  179. for ( cT = 0, iA = 1, iT = 0 ; iA < argc ; ++iA )
  180. {
  181. if ( argv[iA][0] != '-' )
  182. {
  183. if ( pdwErr[iT] )
  184. {
  185. printf( g_achMsg5, argv[iA] );
  186. DisplayErrorMessage( pdwErr[iT] );
  187. printf( g_achMsg6, pdwErr[iT], pdwErr[iT] );
  188. fAtLeastOnError = TRUE;
  189. }
  190. else
  191. {
  192. printf( g_achMsg4, argv[iA] );
  193. }
  194. ++iT;
  195. }
  196. }
  197. if ( fAtLeastOnError )
  198. {
  199. Status = 2;
  200. }
  201. }
  202. }
  203. else
  204. {
  205. DisplayErrorMessage( HRESULTTOWIN32( hRes ) );
  206. Status = 2;
  207. }
  208. CoUninitialize();
  209. LocalFree( g_pStat );
  210. LocalFree( pTargets );
  211. LocalFree( pdwErr );
  212. if ( Status )
  213. {
  214. printf( g_achMsg8 );
  215. }
  216. else
  217. {
  218. printf( g_achMsg7 );
  219. }
  220. return Status;
  221. }