public static void ShowInactiveTopmost(Form frm) { Syscalls.ShowWindow(frm.Handle, (int)Syscalls.WindowStatus.SW_SHOWNOACTIVATE); Syscalls.SetWindowPos(frm.Handle.ToInt32(), Syscalls.HWND_TOPMOST, frm.Left, frm.Top, frm.Width, frm.Height, Syscalls.SWP_NOACTIVATE); }
private static void CreateProcess( string lpApplicationName, string lpCommandLine, bool bInheritHandles, uint dwCreationFlags, ref Syscalls.STARTUPINFO lpStartupInfo, out Syscalls.PROCESS_INFORMATION lpProcessInformation) { int retval; retval = Syscalls._CreateProcess( lpApplicationName, lpCommandLine, new IntPtr(0), new IntPtr(0), bInheritHandles, dwCreationFlags, new IntPtr(0), null, ref lpStartupInfo, out lpProcessInformation); if (retval == 0) { int errcode = Marshal.GetLastWin32Error(); throw new Exception("Cannot create process (" + errcode + ": " + Syscalls.GetLastErrorStringMessage() + ")"); } }
/// <summary> /// Return a human-readable file size. /// </summary> public static string GetHumanFileSize(ulong Bytes) { StringBuilder sb = new StringBuilder(11); Syscalls.StrFormatByteSize((long)Bytes, sb, sb.Capacity); return(sb.ToString()); }
/// <summary> /// Obtain the low-level information of the file stream specified. /// </summary> public static void GetLowLevelFileInfo(FileStream stream, out UInt64 fileID, out UInt64 fileSize, out DateTime fileDate) { Syscalls.BY_HANDLE_FILE_INFORMATION bhfi; Syscalls.GetFileInformationByHandle(stream.SafeFileHandle.DangerousGetHandle(), out bhfi); fileID = (bhfi.FileIndexHigh << 32) + bhfi.FileIndexLow; fileSize = (bhfi.FileSizeHigh << 32) + bhfi.FileSizeLow; fileDate = DateTime.FromFileTime((Int64)(((UInt64)bhfi.LastWriteTime.dwHighDateTime << 32) + (UInt64)bhfi.LastWriteTime.dwLowDateTime)); }
private void DoStart() { uint reason = Syscalls.WaitForSingleObject(ProcessInfo.hProcess, Syscalls.INFINITE); Debug.Assert(reason == (uint)Syscalls.WAIT_RETURN_REASON.WAIT_OBJECT_0); m_running = false; int exitCode = 0; Syscalls.GetExitCodeProcess(ProcessInfo.hProcess, ref exitCode); if (ProcessEnd != null) { ProcessEnd((object)this, new ProcEndEventArgs(exitCode)); } }
/// <summary> /// Return the last modification date of a given file system path. /// If the path leads to a directory, return the creation time of the directory. /// </summary> /// <param name="fullPath"></param> /// <returns></returns> public static DateTime GetLastModificationDate(string fullPath) { FileStream stream = null; try { if (File.Exists(fullPath)) { Syscalls.BY_HANDLE_FILE_INFORMATION bhfi; stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read); Syscalls.GetFileInformationByHandle(stream.SafeFileHandle.DangerousGetHandle(), out bhfi); DateTime observedDate = DateTime.FromFileTime((Int64)(((UInt64)bhfi.LastWriteTime.dwHighDateTime << 32) + (UInt64)bhfi.LastWriteTime.dwLowDateTime)); return(observedDate); } else if (Directory.Exists(fullPath)) { return(new DirectoryInfo(fullPath).CreationTime.ToLocalTime()); } else { return(DateTime.MinValue); } } catch (IOException) { // If we can't open the file directly, try going with the fileinfo method. try { return(new FileInfo(fullPath).LastWriteTime.ToLocalTime()); } catch (Exception) { return(DateTime.MinValue); } } catch (Exception) { return(DateTime.MinValue); } finally { if (stream != null) { stream.Close(); stream = null; } } }
public static UInt64 GetFileSize(string fullPath) { FileStream stream = null; try { Syscalls.BY_HANDLE_FILE_INFORMATION bhfi; stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read); Syscalls.GetFileInformationByHandle(stream.SafeFileHandle.DangerousGetHandle(), out bhfi); return(((UInt64)bhfi.FileSizeHigh << 32) + bhfi.FileSizeLow); } catch (IOException) { // If we can't open the file directly, try going with the fileinfo method. try { return((UInt64) new FileInfo(fullPath).Length); } catch (Exception) { return(0); } } catch (Exception) { return(0); } finally { if (stream != null) { stream.Close(); stream = null; } } }
public void Terminate(int code) { Syscalls.TerminateProcess(ProcessInfo.hProcess, code); }
/// <summary> /// Hackish method used to reduce the application's working set. /// </summary> public static void MinimizeAppWorkingSet() { GC.Collect(); GC.WaitForPendingFinalizers(); Syscalls.SetProcessWorkingSetSize(Process.GetCurrentProcess().Handle, -1, -1); }
/// <summary> /// Return the process SID, if it can be obtained. Otherwise, an empty string /// is returned. /// </summary> public static String GetProcessSid(Process p) { IntPtr procTokenHandle = IntPtr.Zero; IntPtr pSid = IntPtr.Zero; String strSID = ""; IntPtr tokenInfo = IntPtr.Zero; Syscalls.TOKEN_USER userToken; int tokenInfoLength = 0; try { // Open the process in order to fill out procToken. if (!Syscalls.OpenProcessToken(p.Handle, Syscalls.TOKEN_QUERY, ref procTokenHandle)) { throw Syscalls.LastErrorException(); } // Get the required buffer size by making a first call that will // fail, then allocate tokenInfo accordingly and make the call again. if (!Syscalls.GetTokenInformation( procTokenHandle, Syscalls.TOKEN_INFORMATION_CLASS.TokenUser, tokenInfo, 0, ref tokenInfoLength)) { // ERROR_INSUFFICIENT_BUFFER is expected. Fail on anything else. if (Syscalls.GetLastError() != Syscalls.ERROR_INSUFFICIENT_BUFFER) { throw Syscalls.LastErrorException(); } } // Allocate the right buffer, now that we know the requried size. tokenInfo = Marshal.AllocHGlobal(tokenInfoLength); // Make the real call. if (!Syscalls.GetTokenInformation( procTokenHandle, Syscalls.TOKEN_INFORMATION_CLASS.TokenUser, tokenInfo, tokenInfoLength, ref tokenInfoLength)) { throw Syscalls.LastErrorException(); } userToken = (Syscalls.TOKEN_USER)Marshal.PtrToStructure(tokenInfo, typeof(Syscalls.TOKEN_USER)); pSid = userToken.User.Sid; Syscalls.ConvertSidToStringSid(pSid, ref strSID); return(strSID); } catch (Exception ex) { Logging.LogException(ex); return(""); } finally { if (procTokenHandle != IntPtr.Zero) { Syscalls.CloseHandle(procTokenHandle); } if (tokenInfo != IntPtr.Zero) { Marshal.FreeHGlobal(tokenInfo); } } }
public static void ScrollToBottom(IntPtr _controlHandle) { Syscalls.SendMessage(_controlHandle, (int)Syscalls.ScrollBarOptions.EM_SCROLL, (IntPtr)Syscalls.ScrollBarOptions.SB_PAGEBOTTOM, IntPtr.Zero); }
public static bool KSetForegroundWindow(IntPtr _handle) { return(Syscalls.SetForegroundWindow(_handle)); }