Click here to Skip to main content
Click here to Skip to main content


, 13 May 2007
Rate this:
Please Sign up or sign in to vote.






Using the code


Hence only the service has been rewritten in C#.



I will update that too soon.


// Structure for the CreateProcessAsuser API
        public struct SECURITY_ATTRIBUTES
            public int Length;
            public IntPtr lpSecurityDescriptor;
            public bool bInheritHandle;
//Structure for the DuplicateTokenEx API
        enum TOKEN_TYPE : int
            TokenPrimary = 1,
            TokenImpersonation = 2
//Structure for the SetTokenInformation API
        public enum TOKEN_INFORMATION_CLASS : int
            TokenUser = 1,
        // MaxTokenInfoClass should always be the last enum
//Structure for CreateProcess API
        public struct STARTUPINFO
            public int cb;
            public String lpReserved;
            public String lpDesktop;
            public String lpTitle;
            public uint dwX;
            public uint dwY;
            public uint dwXSize;
            public uint dwYSize;
            public uint dwXCountChars;
            public uint dwYCountChars;
            public uint dwFillAttribute;
            public uint dwFlags;
            public short wShowWindow;
            public short cbReserved2;
            public IntPtr lpReserved2;
            public IntPtr hStdInput;
            public IntPtr hStdOutput;
            public IntPtr hStdError;
//Structure for CreateProcess API
        public struct PROCESS_INFORMATION
            public IntPtr hProcess;
            public IntPtr hThread;
            public uint dwProcessId;
            public uint dwThreadId;
//Structure for LookupPriveleges API
        internal struct LUID
            public int LowPart;
            public int HighPart;
        }//end struct
        internal struct LUID_AND_ATRIBUTES
            public LUID Luid;
            public int Attributes;
        }//end struct
//Structure for the AdjustTokenPrivileges API

        internal struct TOKEN_PRIVILEGES
            internal int PrivilegeCount;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
            internal int[] Privileges;

//Defined for Token rights

        public const int READ_CONTROL = 0x00020000;
        public const int STANDARD_RIGHTS_REQUIRED = 0x000F0000;
        public const int STANDARD_RIGHTS_READ = READ_CONTROL;
        public const int STANDARD_RIGHTS_WRITE = READ_CONTROL;
        public const int STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
        public const int STANDARD_RIGHTS_ALL = 0x001F0000;
        public const int SPECIFIC_RIGHTS_ALL = 0x0000FFFF;
        public const int TOKEN_ASSIGN_PRIMARY = 0x0001;
        public const int TOKEN_DUPLICATE = 0x0002;
        public const int TOKEN_IMPERSONATE = 0x0004;
        public const int TOKEN_QUERY = 0x0008;
        public const int TOKEN_QUERY_SOURCE = 0x0010;
        public const int TOKEN_ADJUST_PRIVILEGES = 0x0020;
        public const int TOKEN_ADJUST_GROUPS = 0x0040;
        public const int TOKEN_ADJUST_DEFAULT = 0x0080;
        public const int TOKEN_ADJUST_SESSIONID = 0x0100;
                                      TOKEN_ASSIGN_PRIMARY |
                                      TOKEN_DUPLICATE |
                                      TOKEN_IMPERSONATE |
                                      TOKEN_QUERY |
                                      TOKEN_QUERY_SOURCE |
                                      TOKEN_ADJUST_PRIVILEGES |
                                      TOKEN_ADJUST_GROUPS |
        public const int TOKEN_ALL_ACCESS = TOKEN_ALL_ACCESS_P | 
        public const int TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
                                      TOKEN_ADJUST_PRIVILEGES |
                                      TOKEN_ADJUST_GROUPS |

//Defined for Process Rights
        public const uint MAXIMUM_ALLOWED = 0x2000000;
        public const int CREATE_NEW_PROCESS_GROUP = 0x00000200;
        public const int CREATE_UNICODE_ENVIRONMENT = 0x00000400;
        public const int IDLE_PRIORITY_CLASS = 0x40;
        public const int NORMAL_PRIORITY_CLASS = 0x20;
        public const int HIGH_PRIORITY_CLASS = 0x80;
        public const int REALTIME_PRIORITY_CLASS = 0x100;
        public const int CREATE_NEW_CONSOLE = 0x00000010;
        public const string SE_DEBUG_NAME = "SeDebugPrivilege";
        public const string SE_RESTORE_NAME = "SeRestorePrivilege";
        public const string SE_BACKUP_NAME = "SeBackupPrivilege";
        public const int SE_PRIVILEGE_ENABLED = 0x0002;
        public const int ERROR_NOT_ALL_ASSIGNED = 1300;

//structure for the Process32First API
        private struct PROCESSENTRY32
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string szExeFile;



A list of licenses authors might use can be found here

About the Author

Singapore Singapore

Comments and Discussions

QuestionSome simplifications PinmemberX-Tech10-May-12 13:18 
GeneralMy vote of 4 PinmemberMember 802439017-Jan-12 21:19 
GeneralMy vote of 4 Pinmembermlcc200022-Sep-10 5:18 
GeneralRe: My vote of 4 PinmemberMember 802439017-Jan-12 21:00 
GeneralRequest for code PinmemberMember 450769716-Jun-10 17:09 
GeneralPlease convert to VB PinmemberKyferEz23-Feb-09 9:35 
QuestionBut How do I launch an application under current user as elevated on session 1 ? PinmemberYukoop25-Nov-08 11:22 
So I can launch an application under system on session 1 winsta0\default from a service in session 0. But How do I launch the same application as a user (who is actually an admin at the same time), but in order to allow the application to perform certain restricted tasks, make it elevated. I heard something about token elevation using GetTokenInformation() and Token Linked Token. My service and application are both C# code. Any ideas how to do that please?
On another note, by making the application elevated I think the UAC popups will still come up. correct?
GeneralYou are a lifesaver Pinmemberpkorosi29-Jul-08 23:37 
GeneralRe: You are a lifesaver PinmemberJaisvar30-Jul-08 2:32 
Generalthe declaration of SetTokenInformation is not correct Pinmembercurlfw3-Jun-07 14:31 
QuestionHow to use? Pinmemberstevenyoung13-May-07 17:36 



| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 13 May 2007
Article Copyright 2007 by Jaisvar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid