internal TerminalSessionCollection(TerminalServer server) : base(new System.Collections.Generic.List <TerminalSession>()) { int count = 0; IntPtr ptr = IntPtr.Zero; using (WtsServerCookie cookie = server.OpenServer()) { if (!NativeMethods.WTSEnumerateSessions(cookie.Handle, 0, 1, out ptr, out count)) { throw PscxException.LastWin32Exception(); } } try { foreach (WTS_SESSION_INFO info in Utils.ReadNativeArray <WTS_SESSION_INFO>(ptr, count)) { Items.Add(new TerminalSession(server, info)); } } finally { NativeMethods.WTSFreeMemory(ptr); } }
private static void AdjustToken() { using (Process process = Process.GetCurrentProcess()) { SafeTokenHandle hToken = SafeTokenHandle.InvalidHandle; try { if (!NativeMethods.OpenProcessToken(process.Handle, TokenAccessLevels.AdjustPrivileges, ref hToken)) { throw PscxException.LastWin32Exception(); } TokenPrivilegeCollection privileges = new TokenPrivilegeCollection(); privileges.Enable(TokenPrivilege.Backup); privileges.Enable(TokenPrivilege.Restore); Utils.AdjustTokenPrivileges(hToken, privileges); } finally { hToken.Dispose(); } } }
public unsafe TokenPrivilegeCollection(WindowsIdentity identity) { int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; using (SafeTokenHandle tokenHandle = new SafeTokenHandle(identity.Token, false)) { if (!TryGetTokenPrivileges(tokenHandle, buffer, out bufferSize)) { buffer = new byte[bufferSize]; if (!TryGetTokenPrivileges(tokenHandle, buffer, out bufferSize)) { throw PscxException.LastWin32Exception(); } } } fixed(void *ptr = buffer) { TOKEN_PRIVILEGES * tp = (TOKEN_PRIVILEGES *)(ptr); LUID_AND_ATTRIBUTES *la = &tp->Privileges; int remaining = tp->PrivilegeCount; while (remaining-- > 0) { Add(new TokenPrivilege(*la++)); } } }
internal WtsServerCookie(IntPtr handle) { if (handle == IntPtr.Zero) { throw PscxException.LastWin32Exception(); } _handle = handle; }
public void Logoff(Boolean wait) { using (WtsServerCookie cookie = _server.OpenServer()) { if (!NativeMethods.WTSLogoffSession(cookie.Handle, _sessionId, wait)) { throw PscxException.LastWin32Exception(); } } }
public void Shutdown(WtsShutdownType type) { using (WtsServerCookie cookie = OpenServer()) { if (!NativeMethods.WTSShutdownSystem(cookie.Handle, (int)(type))) { throw PscxException.LastWin32Exception(); } } }
public void TerminateProcess(int processId, int exitCode) { using (WtsServerCookie cookie = OpenServer()) { if (!NativeMethods.WTSTerminateProcess(cookie.Handle, processId, exitCode)) { throw PscxException.LastWin32Exception(); } } }
public TokenPrivilege(string systemName, string privilege, bool enabled) { if (NativeMethods.LookupPrivilegeValue(systemName, privilege, ref value.Luid)) { value.Attributes = (enabled ? PrivilegeStatus.Enabled : PrivilegeStatus.Disabled); } else { throw PscxException.LastWin32Exception(); } }
private AssemblyCacheType GetCacheType(string path) { WriteDebug("GetCacheType: " + path); if (PSDriveInfo == null) { // TODO: extract root from provider qualified path, e.g. assemblycache::\\gac\system.web throw PscxException.NotImplementedYet("Provider-qualified paths are not yet supported. Please map a new drive to this location."); } string root = PSDriveInfo.Root; return(Utils.ParseEnumOrThrow <AssemblyCacheType>(root)); }
public unsafe bool TryGetTokenPrivileges(SafeTokenHandle hToken, byte[] bytes, out int bufferSize) { fixed(void *buffer = bytes) { if (!UnsafeNativeMethods.GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenPrivileges, buffer, bytes.Length, out bufferSize)) { if (Marshal.GetLastWin32Error() == NativeMethods.ERROR_INSUFFICIENT_BUFFER) { return(false); } throw PscxException.LastWin32Exception(); } } return(true); }
void IPscxErrorHandler.WriteLastWin32Error(string errorId, ErrorCategory category, object target) { ErrorHandler.WriteWin32Error(PscxException.LastWin32Exception(), errorId, category, target); }