utils.cpp

Go to the documentation of this file.
00001 /*++
00002 
00003 DCOM Permission Configuration Sample
00004 Copyright (c) 1996, Microsoft Corporation. All rights reserved.
00005 
00006 Module Name:
00007 
00008     utils.cpp
00009 
00010 Abstract:
00011 
00012     Miscellaneous utility functions
00013 
00014 Author:
00015 
00016     Michael Nelson
00017 
00018 Environment:
00019 
00020     Windows NT
00021 
00022 --*/
00023 
00024 #include <windows.h>
00025 #include <stdio.h>
00026 #include <conio.h>
00027 #include <tchar.h>
00028 #include <malloc.h>
00029 #include "ntsecapi.h"
00030 #include "dcomperm.h"
00031 
00032 DWORD
00033 GetCurrentUserSID (
00034     PSID *Sid
00035     )
00036 {
00037     TOKEN_USER  *tokenUser = NIL;
00038     HANDLE      tokenHandle = NIL;
00039     DWORD       tokenSize = 0;
00040     DWORD       sidLength = 0;
00041 
00042     if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &tokenHandle))
00043     {
00044         GetTokenInformation (tokenHandle,
00045                              TokenUser,
00046                              tokenUser,
00047                              0,
00048                              &tokenSize);
00049 
00050         tokenUser = (TOKEN_USER *) malloc (tokenSize);
00051 
00052         if (GetTokenInformation (tokenHandle,
00053                                  TokenUser,
00054                                  tokenUser,
00055                                  tokenSize,
00056                                  &tokenSize))
00057         {
00058             sidLength = GetLengthSid (tokenUser->User.Sid);
00059             *Sid = (PSID) malloc (sidLength);
00060 
00061             memcpy (*Sid, tokenUser->User.Sid, sidLength);
00062             CloseHandle (tokenHandle);
00063         } else
00064         {
00065             free (tokenUser);
00066             return GetLastError();
00067         }
00068     } else
00069     {
00070         free (tokenUser);
00071         return GetLastError();
00072     }
00073 
00074     free (tokenUser);
00075     return ERROR_SUCCESS;
00076 }
00077 
00078 DWORD
00079 GetPrincipalSID (
00080     LPTSTR Principal,
00081     PSID *Sid
00082     )
00083 {
00084     DWORD        sidSize = 0;
00085     TCHAR        refDomain [256];
00086     DWORD        refDomainSize = 0;
00087     DWORD        returnValue = 0;
00088     SID_NAME_USE snu;
00089 
00090     sidSize = 0;
00091     refDomainSize = 255;
00092 
00093     LookupAccountName (NULL,
00094                        Principal,
00095                        *Sid,
00096                        &sidSize,
00097                        refDomain,
00098                        &refDomainSize,
00099                        &snu);
00100 
00101     returnValue = GetLastError();
00102     if (returnValue != ERROR_INSUFFICIENT_BUFFER)
00103         return returnValue;
00104 
00105     *Sid = (PSID) malloc (sidSize);
00106     refDomainSize = 255;
00107 
00108     if (!LookupAccountName (NULL,
00109                             Principal,
00110                             *Sid,
00111                             &sidSize,
00112                             refDomain,
00113                             &refDomainSize,
00114                             &snu))
00115     {
00116         return GetLastError();
00117     }
00118 
00119     return ERROR_SUCCESS;
00120 }
00121 
00122 LPTSTR
00123 SystemMessage (
00124     LPTSTR Buffer,
00125     HRESULT hr
00126     )
00127 {
00128     LPTSTR   message;
00129 
00130     FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
00131                    FORMAT_MESSAGE_FROM_SYSTEM,
00132                    NULL,
00133                    hr,
00134                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
00135                    (LPTSTR) &message,
00136                    0,
00137                    NULL);
00138 
00139     wsprintf (Buffer, TEXT("%s(%lx)\n"), message, hr);
00140     
00141     LocalFree (message);
00142     return Buffer;
00143 }
00144 

Generated on Fri Nov 28 04:28:49 2008 for HOOPLE Libraries by  doxygen 1.5.1