home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / dcom / dcomperm / utils.cpp < prev    next >
C/C++ Source or Header  |  1997-04-07  |  3KB  |  143 lines

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