UTILS.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:3k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /*++
  2. DCOM Permission Configuration Sample
  3. Copyright (c) 1996, Microsoft Corporation. All rights reserved.
  4. Module Name:
  5.     utils.cpp
  6. Abstract:
  7.     Miscellaneous utility functions
  8. Author:
  9.     Michael Nelson
  10. Environment:
  11.     Windows NT
  12. --*/
  13. #include <windows.h>
  14. #include <stdio.h>
  15. #include <conio.h>
  16. #include <tchar.h>
  17. #include "ntsecapi.h"
  18. #include "dcomperm.h"
  19. DWORD
  20. GetCurrentUserSID (
  21.     PSID *Sid
  22.     )
  23. {
  24.     TOKEN_USER  *tokenUser;
  25.     HANDLE      tokenHandle;
  26.     DWORD       tokenSize;
  27.     DWORD       sidLength;
  28.     if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &tokenHandle))
  29.     {
  30.         GetTokenInformation (tokenHandle,
  31.                              TokenUser,
  32.                              tokenUser,
  33.                              0,
  34.                              &tokenSize);
  35.         tokenUser = (TOKEN_USER *) malloc (tokenSize);
  36.         if (GetTokenInformation (tokenHandle,
  37.                                  TokenUser,
  38.                                  tokenUser,
  39.                                  tokenSize,
  40.                                  &tokenSize))
  41.         {
  42.             sidLength = GetLengthSid (tokenUser->User.Sid);
  43.             *Sid = (PSID) malloc (sidLength);
  44.             memcpy (*Sid, tokenUser->User.Sid, sidLength);
  45.             CloseHandle (tokenHandle);
  46.         } else
  47.         {
  48.             free (tokenUser);
  49.             return GetLastError();
  50.         }
  51.     } else
  52.     {
  53.         free (tokenUser);
  54.         return GetLastError();
  55.     }
  56.     free (tokenUser);
  57.     return ERROR_SUCCESS;
  58. }
  59. DWORD
  60. GetPrincipalSID (
  61.     LPTSTR Principal,
  62.     PSID *Sid
  63.     )
  64. {
  65.     DWORD        sidSize;
  66.     TCHAR        refDomain [256];
  67.     DWORD        refDomainSize;
  68.     DWORD        returnValue;
  69.     SID_NAME_USE snu;
  70.     sidSize = 0;
  71.     refDomainSize = 255;
  72.     LookupAccountName (NULL,
  73.                        Principal,
  74.                        *Sid,
  75.                        &sidSize,
  76.                        refDomain,
  77.                        &refDomainSize,
  78.                        &snu);
  79.     returnValue = GetLastError();
  80.     if (returnValue != ERROR_INSUFFICIENT_BUFFER)
  81.         return returnValue;
  82.     *Sid = (PSID) malloc (sidSize);
  83.     refDomainSize = 255;
  84.     if (!LookupAccountName (NULL,
  85.                             Principal,
  86.                             *Sid,
  87.                             &sidSize,
  88.                             refDomain,
  89.                             &refDomainSize,
  90.                             &snu))
  91.     {
  92.         return GetLastError();
  93.     }
  94.     return ERROR_SUCCESS;
  95. }
  96. LPTSTR
  97. SystemMessage (
  98.     LPTSTR Buffer,
  99.     HRESULT hr
  100.     )
  101. {
  102.     LPTSTR   message;
  103.     FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
  104.                    FORMAT_MESSAGE_FROM_SYSTEM,
  105.                    NULL,
  106.                    hr,
  107.                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  108.                    (LPTSTR) &message,
  109.                    0,
  110.                    NULL);
  111.     wsprintf (Buffer, TEXT("%s(%lx)n"), message, hr);
  112.     
  113.     LocalFree (message);
  114.     return Buffer;
  115. }