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.

164 lines
4.7 KiB

  1. /*++
  2. Copyright (c) 2000-2001 Microsoft Corporation
  3. Module Name:
  4. client.cpp
  5. Abstract:
  6. Test program to drive the VSS Writer Shim contained in VssAPI.DLL
  7. Author:
  8. Stefan R. Steiner [ssteiner] 01-30-2000
  9. Revision History:
  10. --*/
  11. /////////////////////////////////////////////////////////////////////////////
  12. // Defines
  13. // C4290: C++ Exception Specification ignored
  14. #pragma warning(disable:4290)
  15. // warning C4511: 'CVssCOMApplication' : copy constructor could not be generated
  16. #pragma warning(disable:4511)
  17. // warning C4127: conditional expression is constant
  18. #pragma warning(disable:4127)
  19. /////////////////////////////////////////////////////////////////////////////
  20. // Includes
  21. #include <windows.h>
  22. #include <wtypes.h>
  23. #include <stddef.h>
  24. #include <stdio.h>
  25. #include <objbase.h>
  26. #include <vss.h>
  27. typedef HRESULT ( APIENTRY *PFUNC_RegisterSnapshotSubscriptions )( void );
  28. typedef HRESULT ( APIENTRY *PFUNC_UnregisterSnapshotSubscriptions )( void );
  29. typedef HRESULT ( APIENTRY *PFUNC_SimulateSnapshotFreeze )( PWCHAR pwszSnapshotSetId, PWCHAR pwszVolumeNamesList );
  30. typedef HRESULT ( APIENTRY *PFUNC_SimulateSnapshotThaw )( PWCHAR pwszSnapshotSetId );
  31. static BOOL AssertPrivilege( LPCWSTR privName );
  32. /////////////////////////////////////////////////////////////////////////////
  33. // WinMain
  34. extern "C" int __cdecl wmain( int argc, WCHAR *argv[] )
  35. {
  36. HINSTANCE hInstLib;
  37. PFUNC_RegisterSnapshotSubscriptions pFnRegisterSS;
  38. PFUNC_UnregisterSnapshotSubscriptions pFnUnregisterSS;
  39. HRESULT hr;
  40. if ( !AssertPrivilege( SE_BACKUP_NAME ) )
  41. {
  42. wprintf( L"AssertPrivilege returned error, rc:%d\n", GetLastError() );
  43. return 2;
  44. }
  45. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  46. if ( FAILED( hr ) )
  47. {
  48. wprintf( L"CoInitialize() returned rc:%d\n", GetLastError() );
  49. return 1;
  50. }
  51. // Get a handle to the DLL module
  52. hInstLib = LoadLibrary( L"VssAPI.dll" );
  53. if ( hInstLib != NULL )
  54. {
  55. pFnRegisterSS = ( PFUNC_RegisterSnapshotSubscriptions )GetProcAddress( hInstLib, "RegisterSnapshotSubscriptions" );
  56. if ( pFnRegisterSS != NULL )
  57. wprintf( L"pFnRegisterSS returned: 0x%08x\n", ( pFnRegisterSS )() );
  58. else
  59. wprintf( L"Couldn't import RegisterSnapshotSubscriptions function, rc:%d\n", GetLastError() );
  60. wprintf( L"\nPress return to continue...\n" );
  61. getchar();
  62. wprintf( L"continuing...\n" );
  63. pFnUnregisterSS = ( PFUNC_UnregisterSnapshotSubscriptions )GetProcAddress( hInstLib, "UnregisterSnapshotSubscriptions" );
  64. if ( pFnUnregisterSS != NULL )
  65. wprintf( L"pFnUnregisterSS returned: 0x%08x\n", ( pFnUnregisterSS )() );
  66. else
  67. wprintf( L"Couldn't import UnregisterSnapshotSubscriptions function, rc:%d\n", GetLastError() );
  68. FreeLibrary( hInstLib );
  69. }
  70. else
  71. printf( "LoadLibrary error, rc:%d\n", GetLastError() );
  72. // Uninitialize COM library
  73. CoUninitialize();
  74. return 0;
  75. UNREFERENCED_PARAMETER( argv );
  76. UNREFERENCED_PARAMETER( argc );
  77. }
  78. static BOOL AssertPrivilege( LPCWSTR privName )
  79. {
  80. HANDLE tokenHandle;
  81. BOOL stat = FALSE;
  82. if ( OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &tokenHandle ) )
  83. {
  84. LUID value;
  85. if ( LookupPrivilegeValue( NULL, privName, &value ) )
  86. {
  87. TOKEN_PRIVILEGES newState;
  88. DWORD error;
  89. newState.PrivilegeCount = 1;
  90. newState.Privileges[0].Luid = value;
  91. newState.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  92. /*
  93. * We will always call GetLastError below, so clear
  94. * any prior error values on this thread.
  95. */
  96. SetLastError( ERROR_SUCCESS );
  97. stat = AdjustTokenPrivileges(
  98. tokenHandle,
  99. FALSE,
  100. &newState,
  101. (DWORD)0,
  102. NULL,
  103. NULL );
  104. /*
  105. * Supposedly, AdjustTokenPriveleges always returns TRUE
  106. * (even when it fails). So, call GetLastError to be
  107. * extra sure everything's cool.
  108. */
  109. if ( (error = GetLastError()) != ERROR_SUCCESS )
  110. {
  111. stat = FALSE;
  112. }
  113. if ( !stat )
  114. {
  115. wprintf( L"AdjustTokenPrivileges for %s failed with %d",
  116. privName,
  117. error );
  118. }
  119. }
  120. CloseHandle( tokenHandle );
  121. }
  122. return stat;
  123. }