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 "stdafx.h"
25 #include <windows.h>
26 #include <stdio.h>
27 #include <conio.h>
28 #include <tchar.h>
29 #include "ntsecapi.h"
30 #include "dcomperm.h"
31 
32 DWORD
GetCurrentUserSID(PSID * Sid)33 GetCurrentUserSID (
34     PSID *Sid
35     )
36 {
37     TOKEN_USER  *tokenUser=NULL;
38     HANDLE      tokenHandle;
39     DWORD       tokenSize;
40     DWORD       sidLength;
41 
42     if (OpenProcessToken (GetCurrentProcess(), TOKEN_QUERY, &tokenHandle))
43     {
44         GetTokenInformation (tokenHandle,
45                              TokenUser,
46                              tokenUser,
47                              0,
48                              &tokenSize);
49 
50         tokenUser = (TOKEN_USER *) malloc (tokenSize);
51 
52         if (GetTokenInformation (tokenHandle,
53                                  TokenUser,
54                                  tokenUser,
55                                  tokenSize,
56                                  &tokenSize))
57         {
58             sidLength = GetLengthSid (tokenUser->User.Sid);
59             *Sid = (PSID) malloc (sidLength);
60 
61             memcpy (*Sid, tokenUser->User.Sid, sidLength);
62             CloseHandle (tokenHandle);
63         } else
64         {
65             free (tokenUser);
66             return GetLastError();
67         }
68     } else
69     {
70         free (tokenUser);
71         return GetLastError();
72     }
73 
74     free (tokenUser);
75     return ERROR_SUCCESS;
76 }
77