IAccessControl Reader.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:5k
源码类别:

Windows编程

开发平台:

Visual C++

  1. #define _WIN32_DCOM
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <iaccess.h> // For IAccessControl
  5. // Need to define this ourselves!
  6. const IID IID_IAccessControl = {0xEEDD23E0,0x8410,0x11CE,{0xA1,0xC3,0x08,0x00,0x2B,0x2B,0x8D,0x8F}};
  7. // This is the header stored in the registry
  8. typedef struct
  9. {
  10.     WORD version;
  11.     WORD pad;
  12.     GUID classid;
  13. } SPermissionHeader;
  14. void main()
  15. {
  16.     HRESULT hr = CoInitialize(NULL);
  17.     if(FAILED(hr))
  18. printf("Couldn't initialize COMn");
  19.     HKEY key = 0;
  20.     hr = RegOpenKeyEx(HKEY_CLASSES_ROOT, "AppID\{10000002-0000-0000-0000-000000000001}",
  21. 0, KEY_READ, &key);
  22.     if(FAILED(hr))
  23.     printf("Couldn't open the AppID keyn");
  24.     // Read the value from the registry
  25. DWORD dwSize = 0;
  26.     hr = RegQueryValueEx(key, "AccessPermission", NULL, NULL, NULL, &dwSize);
  27.     if(FAILED(hr) || dwSize == 0)
  28. {
  29.     printf("Couldn't read the size of the registry valuen");
  30.     printf("Use DCOMCNFG to configure custom access permissions for the Inside COM+ Sample Componentn");
  31. exit(0);
  32. }
  33. void* pMemory = (void*)CoTaskMemAlloc(dwSize);
  34.     hr = RegQueryValueEx(key, "AccessPermission", NULL, NULL, (unsigned char*)pMemory, &dwSize);
  35.     if(FAILED(hr))
  36.     printf("Couldn't read the registry valuen");
  37.     IStream* pStream;
  38.     hr = CreateStreamOnHGlobal(pMemory, TRUE, &pStream);
  39.     if(FAILED(hr))
  40. printf("Couldn't create stream from memory blockn");
  41. IPersistStream* pPersistStream = NULL;
  42.     hr = CoCreateInstance(CLSID_DCOMAccessControl, NULL, CLSCTX_INPROC_SERVER,
  43. IID_IPersistStream, (void**)&pPersistStream);
  44.     if(FAILED(hr))
  45. printf("Couldn't create DCOM access control objectn");
  46.     LARGE_INTEGER size;
  47.     size.QuadPart = sizeof(SPermissionHeader);
  48.     hr = pStream->Seek(size, STREAM_SEEK_SET, NULL);
  49.     if(FAILED(hr))
  50. printf("Couldn't seek in streamn");
  51. hr = pPersistStream->Load(pStream);
  52.     if(FAILED(hr))
  53. printf("Couldn't load streamn");
  54. IAccessControl* pAccessControl = NULL;
  55. hr = pPersistStream->QueryInterface(IID_IAccessControl, (void**)&pAccessControl);
  56.     if(FAILED(hr))
  57. printf("Couldn't get IAccessControln");
  58. ACTRL_ACCESSW* pAccess = NULL;
  59. TRUSTEEW* pOwner = NULL;
  60. TRUSTEEW* pGroup = NULL;
  61. hr = pAccessControl->GetAllAccessRights(NULL, &pAccess, &pOwner, &pGroup);
  62. if(FAILED(hr))
  63. printf("GetAllAccessRights failedn");
  64. printf("ACTRL_ACCESS.Entries = %dn", pAccess->cEntries);
  65. for(unsigned count = 0; count < pAccess->cEntries; count++)
  66. {
  67. printf("pAccess->pPropertyAccessList[%d].pAccessEntryList->cEntries = %dn", count, pAccess->pPropertyAccessList[count].pAccessEntryList->cEntries);
  68. for(unsigned count2 = 0; count2 < pAccess->pPropertyAccessList[count].pAccessEntryList->cEntries; count2++)
  69. {
  70. if(pAccess->pPropertyAccessList[count].pAccessEntryList->pAccessList[count2].Trustee.MultipleTrusteeOperation == NO_MULTIPLE_TRUSTEE)
  71. printf("pAccessList[%d].Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEEn", count2);
  72. else
  73. printf("pAccessList[%d].Trustee.MultipleTrusteeOperation = TRUSTEE_IS_IMPERSONATEn", count2);
  74. switch(pAccess->pPropertyAccessList[count].pAccessEntryList->pAccessList[count2].Trustee.TrusteeForm)
  75. {
  76. case TRUSTEE_IS_SID:
  77. printf("pAccessList[%d].Trustee.TrusteeForm = TRUSTEE_IS_SIDn", count2);
  78. /* Add code for SID here if you are interested */
  79. break;
  80. case TRUSTEE_IS_NAME:
  81. printf("pAccessList[%d].Trustee.TrusteeForm = TRUSTEE_IS_NAMEn", count2);
  82. wprintf(L"pAccessList[%d].Trustee.ptstrName = %sn", count2, pAccess->pPropertyAccessList[count].pAccessEntryList->pAccessList[count2].Trustee.ptstrName);
  83. break;
  84. case TRUSTEE_BAD_FORM:
  85. printf("pAccessList[%d].Trustee.TrusteeForm = TRUSTEE_BAD_FORMn", count2);
  86. break;
  87. }
  88. switch(pAccess->pPropertyAccessList[count].pAccessEntryList->pAccessList[count2].Trustee.TrusteeType)
  89. {
  90. case TRUSTEE_IS_UNKNOWN:
  91. printf("pAccessList[%d].Trustee.TrusteeType = TRUSTEE_IS_UNKNOWNn", count2);
  92. break;
  93. case TRUSTEE_IS_USER:
  94. printf("pAccessList[%d].Trustee.TrusteeType = TRUSTEE_IS_USERn", count2);
  95. break;
  96. case TRUSTEE_IS_GROUP:
  97. printf("pAccessList[%d].Trustee.TrusteeType = TRUSTEE_IS_GROUPn", count2);
  98. break;
  99. case TRUSTEE_IS_DOMAIN:
  100. printf("pAccessList[%d].Trustee.TrusteeType = TRUSTEE_IS_DOMAINn", count2);
  101. break;
  102. case TRUSTEE_IS_ALIAS:
  103. printf("pAccessList[%d].Trustee.TrusteeType = TRUSTEE_IS_ALIASn", count2);
  104. break;
  105. case TRUSTEE_IS_WELL_KNOWN_GROUP:
  106. printf("pAccessList[%d].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUPn", count2);
  107. break;
  108. case TRUSTEE_IS_DELETED:
  109. printf("pAccessList[%d].Trustee.TrusteeType = TRUSTEE_IS_DELETEDn", count2);
  110. break;
  111. case TRUSTEE_IS_INVALID:
  112. printf("pAccessList[%d].Trustee.TrusteeType = TRUSTEE_IS_INVALIDn", count2);
  113. break;
  114. }
  115. if(pAccess->pPropertyAccessList[count].pAccessEntryList->pAccessList[count2].fAccessFlags == ACTRL_ACCESS_ALLOWED)
  116. printf("pAccessList[%d].fAccessFlags = ACTRL_ACCESS_ALLOWEDn", count2);
  117. else
  118. printf("pAccessList[%d].fAccessFlags = ACTRL_ACCESS_DENIEDn", count2);
  119. }
  120. }
  121.     // Release everything and bail out
  122. CoTaskMemFree(pAccess);
  123. CoTaskMemFree(pOwner);
  124. CoTaskMemFree(pGroup);
  125.     pPersistStream->Release();
  126.     pStream->Release();
  127.     pAccessControl->Release();
  128.     RegCloseKey(key);
  129.     CoUninitialize();
  130. }