Changeset 993
- Timestamp:
- 5/7/2009 2:57:52 AM (4 years ago)
- Location:
- branches/eraser6
- Files:
-
- 8 edited
-
Eraser/UpdateForm.cs (modified) (1 diff)
-
Manager/EntropySource.cs (modified) (4 diffs)
-
Manager/Manager.csproj (modified) (2 diffs)
-
Util/File.cs (modified) (3 diffs)
-
Util/KernelAPI.cs (modified) (2 diffs)
-
Util/Permissions.cs (modified) (2 diffs)
-
Util/ShellAPI.cs (modified) (2 diffs)
-
Util/UserAPI.cs (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eraser6/Eraser/UpdateForm.cs
r962 r993 161 161 compatibleArchs.Add("any"); 162 162 163 KernelAPI.SYSTEM_INFO info = new KernelAPI.SYSTEM_INFO(); 164 KernelAPI.GetSystemInfo(out info); 165 switch (info.processorArchitecture) 166 { 167 case KernelAPI.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64: 163 switch (KernelAPI.ProcessorArchitecture) 164 { 165 case ProcessorArchitecture.Amd64: 168 166 compatibleArchs.Add("x64"); 169 167 break; 170 171 case KernelAPI.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_IA64: 168 case ProcessorArchitecture.IA64: 172 169 compatibleArchs.Add("ia64"); 173 170 break; 174 175 case KernelAPI.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_INTEL: 171 case ProcessorArchitecture.X86: 176 172 compatibleArchs.Add("x86"); 177 173 break; -
branches/eraser6/Manager/EntropySource.cs
r954 r993 32 32 using Microsoft.Win32.SafeHandles; 33 33 using Eraser.Util; 34 using System.Windows.Forms; 35 using Microsoft.VisualBasic.Devices; 34 36 35 37 namespace Eraser.Manager … … 247 249 List<byte> result = new List<byte>(); 248 250 249 //Process startup information 250 KernelAPI.STARTUPINFO startupInfo = new KernelAPI.STARTUPINFO(); 251 startupInfo.cbSize = (uint)Marshal.SizeOf(typeof(KernelAPI.STARTUPINFO)); 252 KernelAPI.GetStartupInfo(out startupInfo); 253 result.AddRange(StructToBuffer(startupInfo)); 254 255 //System information 256 KernelAPI.SYSTEM_INFO systemInfo = new KernelAPI.SYSTEM_INFO(); 257 KernelAPI.GetSystemInfo(out systemInfo); 258 result.AddRange(StructToBuffer(systemInfo)); 251 //Process information 252 result.AddRange(StructToBuffer(Process.GetCurrentProcess().Id)); 253 result.AddRange(StructToBuffer(Process.GetCurrentProcess().StartTime)); 259 254 260 255 result.AddRange(GetFastEntropy()); … … 300 295 301 296 //Miscellaneous window handles 302 result.AddRange(StructToBuffer(UserAPI.GetCapture())); 303 result.AddRange(StructToBuffer(UserAPI.GetClipboardOwner())); 304 result.AddRange(StructToBuffer(UserAPI.GetClipboardViewer())); 305 result.AddRange(StructToBuffer(UserAPI.GetDesktopWindow())); 306 result.AddRange(StructToBuffer(UserAPI.GetForegroundWindow())); 307 result.AddRange(StructToBuffer(UserAPI.GetMessagePos())); 308 result.AddRange(StructToBuffer(UserAPI.GetMessageTime())); 309 result.AddRange(StructToBuffer(UserAPI.GetOpenClipboardWindow())); 310 result.AddRange(StructToBuffer(UserAPI.GetProcessWindowStation())); 311 result.AddRange(StructToBuffer(KernelAPI.GetCurrentProcessId())); 312 result.AddRange(StructToBuffer(KernelAPI.GetCurrentThreadId())); 313 result.AddRange(StructToBuffer(KernelAPI.GetProcessHeap())); 297 result.AddRange(StructToBuffer(Form.ActiveForm.Handle)); 298 result.AddRange(StructToBuffer(UserAPI.MessagePos)); 299 result.AddRange(StructToBuffer(UserAPI.MessageTime)); 314 300 315 301 //The caret and cursor positions 316 UserAPI.POINT point; 317 UserAPI.GetCaretPos(out point); 318 result.AddRange(StructToBuffer(point)); 319 UserAPI.GetCursorPos(out point); 320 result.AddRange(StructToBuffer(point)); 302 result.AddRange(StructToBuffer(UserAPI.CaretPos)); 303 result.AddRange(StructToBuffer(Cursor.Position)); 304 305 //Currently running threads (dynamic, but not very) 306 Process currProcess = Process.GetCurrentProcess(); 307 foreach (ProcessThread thread in currProcess.Threads) 308 result.AddRange(StructToBuffer(thread.Id)); 309 310 //Various process statistics 311 result.AddRange(StructToBuffer(currProcess.VirtualMemorySize64)); 312 result.AddRange(StructToBuffer(currProcess.MaxWorkingSet)); 313 result.AddRange(StructToBuffer(currProcess.MinWorkingSet)); 314 result.AddRange(StructToBuffer(currProcess.NonpagedSystemMemorySize64)); 315 result.AddRange(StructToBuffer(currProcess.PagedMemorySize64)); 316 result.AddRange(StructToBuffer(currProcess.PagedSystemMemorySize64)); 317 result.AddRange(StructToBuffer(currProcess.PeakPagedMemorySize64)); 318 result.AddRange(StructToBuffer(currProcess.PeakVirtualMemorySize64)); 319 result.AddRange(StructToBuffer(currProcess.PeakWorkingSet64)); 320 result.AddRange(StructToBuffer(currProcess.PrivateMemorySize64)); 321 result.AddRange(StructToBuffer(currProcess.WorkingSet64)); 322 result.AddRange(StructToBuffer(currProcess.HandleCount)); 321 323 322 324 //Amount of free memory 323 KernelAPI.MEMORYSTATUSEX memoryStatus = new KernelAPI.MEMORYSTATUSEX();324 memoryStatus.dwLength = (uint)Marshal.SizeOf(memoryStatus);325 if (KernelAPI.GlobalMemoryStatusEx(ref memoryStatus))326 { 327 result.AddRange(StructToBuffer(memoryStatus.ullAvailPhys));328 result.AddRange(StructToBuffer(memoryStatus.ullAvailVirtual));329 result.AddRange(StructToBuffer(memoryStatus));330 }325 ComputerInfo computerInfo = new ComputerInfo(); 326 result.AddRange(StructToBuffer(computerInfo.AvailablePhysicalMemory)); 327 result.AddRange(StructToBuffer(computerInfo.AvailableVirtualMemory)); 328 329 //Process execution times 330 result.AddRange(StructToBuffer(currProcess.TotalProcessorTime)); 331 result.AddRange(StructToBuffer(currProcess.UserProcessorTime)); 332 result.AddRange(StructToBuffer(currProcess.PrivilegedProcessorTime)); 331 333 332 334 //Thread execution times 333 long creationTime, exitTime, kernelTime, userTime; 334 if (KernelAPI.GetThreadTimes(KernelAPI.GetCurrentThread(), out creationTime, 335 out exitTime, out kernelTime, out userTime)) 336 { 337 result.AddRange(StructToBuffer(creationTime)); 338 result.AddRange(StructToBuffer(kernelTime)); 339 result.AddRange(StructToBuffer(userTime)); 340 } 341 342 //Process execution times 343 if (KernelAPI.GetProcessTimes(KernelAPI.GetCurrentProcess(), out creationTime, 344 out exitTime, out kernelTime, out userTime)) 345 { 346 result.AddRange(StructToBuffer(creationTime)); 347 result.AddRange(StructToBuffer(kernelTime)); 348 result.AddRange(StructToBuffer(userTime)); 335 foreach (ProcessThread thread in currProcess.Threads) 336 { 337 result.AddRange(StructToBuffer(thread.TotalProcessorTime)); 338 result.AddRange(StructToBuffer(thread.UserProcessorTime)); 339 result.AddRange(StructToBuffer(thread.PrivilegedProcessorTime)); 349 340 } 350 341 … … 353 344 354 345 //The high resolution performance counter 355 long perfCount = 0; 356 if (KernelAPI.QueryPerformanceCounter(out perfCount)) 357 result.AddRange(StructToBuffer(perfCount)); 346 result.AddRange(StructToBuffer(KernelAPI.PerformanceCounter)); 358 347 359 348 //Ticks since start up 360 uint tickCount = KernelAPI.GetTickCount(); 361 if (tickCount != 0) 362 result.AddRange(StructToBuffer(tickCount)); 349 result.AddRange(StructToBuffer(Environment.TickCount)); 363 350 364 351 //CryptGenRandom -
branches/eraser6/Manager/Manager.csproj
r949 r993 35 35 </PropertyGroup> 36 36 <ItemGroup> 37 <Reference Include="Microsoft.VisualBasic" /> 37 38 <Reference Include="System" /> 38 39 <Reference Include="System.Core"> … … 40 41 </Reference> 41 42 <Reference Include="System.Data" /> 43 <Reference Include="System.Drawing" /> 42 44 <Reference Include="System.Security" /> 43 45 <Reference Include="System.Windows.Forms" /> -
branches/eraser6/Util/File.cs
r992 r993 104 104 public static string GetFileDescription(string path) 105 105 { 106 ShellAPI. SHFILEINFO shfi = new ShellAPI.SHFILEINFO();107 ShellAPI. SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi),108 ShellAPI. SHGetFileInfoFlags.SHGFI_DISPLAYNAME);106 ShellAPI.NativeMethods.SHFILEINFO shfi = new ShellAPI.NativeMethods.SHFILEINFO(); 107 ShellAPI.NativeMethods.SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 108 ShellAPI.NativeMethods.SHGetFileInfoFlags.SHGFI_DISPLAYNAME); 109 109 return shfi.szDisplayName; 110 110 } … … 120 120 public static Icon GetFileIcon(string path) 121 121 { 122 ShellAPI.SHFILEINFO shfi = new ShellAPI.SHFILEINFO(); 123 ShellAPI.SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 124 ShellAPI.SHGetFileInfoFlags.SHGFI_SMALLICON | ShellAPI.SHGetFileInfoFlags.SHGFI_ICON); 122 ShellAPI.NativeMethods.SHFILEINFO shfi = new ShellAPI.NativeMethods.SHFILEINFO(); 123 ShellAPI.NativeMethods.SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 124 ShellAPI.NativeMethods.SHGetFileInfoFlags.SHGFI_SMALLICON | 125 ShellAPI.NativeMethods.SHGetFileInfoFlags.SHGFI_ICON); 125 126 126 127 if (shfi.hIcon != IntPtr.Zero) … … 327 328 [MarshalAs(UnmanagedType.Bool)] bool bProcessSecurity, 328 329 ref IntPtr lpContext); 329 330 330 331 331 [DllImport("Kernel32.dll", SetLastError = true)] -
branches/eraser6/Util/KernelAPI.cs
r825 r993 32 32 { 33 33 /// <summary> 34 /// Closes an open object handle. 35 /// </summary> 36 /// <param name="hObject">A valid handle to an open object.</param> 37 /// <returns>If the function succeeds, the return value is true. To get 38 /// extended error information, call Marshal.GetLastWin32Error(). 39 /// 40 /// If the application is running under a debugger, the function will throw 41 /// an exception if it receives either a handle value that is not valid 42 /// or a pseudo-handle value. This can happen if you close a handle twice, 43 /// or if you call CloseHandle on a handle returned by the FindFirstFile 44 /// function.</returns> 45 [DllImport("Kernel32.dll", SetLastError = true)] 46 [return: MarshalAs(UnmanagedType.Bool)] 47 public static extern bool CloseHandle(IntPtr hObject); 48 49 /// <summary> 50 /// Retrieves a pseudo handle for the current process. 51 /// </summary> 52 /// <returns>A pseudo handle to the current process.</returns> 53 /// <remarks>A pseudo handle is a special constant, currently (HANDLE)-1, 54 /// that is interpreted as the current process handle. For compatibility 55 /// with future operating systems, it is best to call GetCurrentProcess 56 /// instead of hard-coding this constant value. The calling process can 57 /// use a pseudo handle to specify its own process whenever a process 58 /// handle is required. Pseudo handles are not inherited by child processes. 59 /// 60 /// This handle has the maximum possible access to the process object. 61 /// For systems that support security descriptors, this is the maximum 62 /// access allowed by the security descriptor for the calling process. 63 /// For systems that do not support security descriptors, this is 64 /// PROCESS_ALL_ACCESS. For more information, see Process Security and 65 /// Access Rights. 66 /// 67 /// A process can create a "real" handle to itself that is valid in the 68 /// context of other processes, or that can be inherited by other processes, 69 /// by specifying the pseudo handle as the source handle in a call to the 70 /// DuplicateHandle function. A process can also use the OpenProcess 71 /// function to open a real handle to itself. 72 /// 73 /// The pseudo handle need not be closed when it is no longer needed. 74 /// Calling the CloseHandle function with a pseudo handle has no effect. 75 /// If the pseudo handle is duplicated by DuplicateHandle, the duplicate 76 /// handle must be closed.</remarks> 77 [DllImport("Kernel32.dll", SetLastError = true)] 78 public static extern IntPtr GetCurrentProcess(); 79 80 /// <summary> 81 /// Retrieves the process identifier of the calling process. 82 /// </summary> 83 /// <returns>The return value is the process identifier of the calling 84 /// process.</returns> 85 [DllImport("Kernel32.dll")] 86 public static extern uint GetCurrentProcessId(); 87 88 /// <summary> 89 /// Retrieves a pseudo handle for the calling thread. 90 /// </summary> 91 /// <returns>The return value is a pseudo handle for the current thread.</returns> 92 [DllImport("Kernel32.dll", SetLastError = true)] 93 public static extern IntPtr GetCurrentThread(); 94 95 /// <summary> 96 /// Retrieves the thread identifier of the calling thread. 97 /// </summary> 98 /// <returns>The return value is the thread identifier of the calling 99 /// thread.</returns> 100 [DllImport("Kernel32.dll")] 101 public static extern uint GetCurrentThreadId(); 102 103 /// <summary> 104 /// Retrieves a handle to the heap of the calling process. This handle 105 /// can then be used in subsequent calls to the heap functions. 106 /// </summary> 107 /// <returns>If the function succeeds, the return value is a handle to 108 /// the calling process's heap. 109 /// 110 /// If the function fails, the return value is NULL. To get extended error 111 /// information, call Marshal.GetLastWin32Error.</returns> 112 [DllImport("Kernel32.dll", SetLastError = true)] 113 public static extern IntPtr GetProcessHeap(); 114 115 /// <summary> 116 /// Retrieves timing information for the specified process. 117 /// </summary> 118 /// <param name="hThread">A handle to the process whose timing information 119 /// is sought. This handle must have the PROCESS_QUERY_INFORMATION access 120 /// right. For more information, see Thread Security and Access Rights.</param> 121 /// <param name="lpCreationTime">A reference to a long that receives the 122 /// creation time of the thread.</param> 123 /// <param name="lpExitTime">A reference to a long that receives the exit time 124 /// of the thread. If the thread has not exited, the value of field is 125 /// undefined.</param> 126 /// <param name="lpKernelTime">A reference to a long that receives the 127 /// amount of time that the thread has executed in kernel mode.</param> 128 /// <param name="lpUserTime">A reference to a long that receives the amount 129 /// of time that the thread has executed in user mode.</param> 34 /// Allocates a new console for the calling process. 35 /// </summary> 130 36 /// <returns>If the function succeeds, the return value is nonzero. 131 37 /// 132 38 /// If the function fails, the return value is zero. To get extended error 133 39 /// information, call Marshal.GetLastWin32Error.</returns> 134 [DllImport("Kernel32.dll", SetLastError = true)] 135 [return: MarshalAs(UnmanagedType.Bool)] 136 public static extern bool GetProcessTimes(IntPtr hThread, out long lpCreationTime, 137 out long lpExitTime, out long lpKernelTime, out long lpUserTime); 138 139 /// <summary> 140 /// Retrieves the contents of the STARTUPINFO structure that was specified 141 /// when the calling process was created. 142 /// </summary> 143 /// <param name="lpStartupInfo">A pointer to a STARTUPINFO structure that 144 /// receives the startup information.</param> 145 [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] 146 public static extern void GetStartupInfo(out STARTUPINFO lpStartupInfo); 147 148 /// <summary> 149 /// Retrieves information about the current system. 150 /// </summary> 151 /// <param name="lpSystemInfo">A pointer to a SYSTEM_INFO structure that 152 /// receives the information.</param> 153 [DllImport("Kernel32.dll")] 154 public static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo); 155 156 /// <summary> 157 /// Retrieves timing information for the specified thread. 158 /// </summary> 159 /// <param name="hThread">A handle to the thread whose timing information 160 /// is sought. This handle must have the THREAD_QUERY_INFORMATION access 161 /// right. For more information, see Thread Security and Access Rights.</param> 162 /// <param name="lpCreationTime">A reference to a long that receives the 163 /// creation time of the thread.</param> 164 /// <param name="lpExitTime">A reference to a long that receives the exit time 165 /// of the thread. If the thread has not exited, the value of field is 166 /// undefined.</param> 167 /// <param name="lpKernelTime">A reference to a long that receives the 168 /// amount of time that the thread has executed in kernel mode.</param> 169 /// <param name="lpUserTime">A reference to a long that receives the amount 170 /// of time that the thread has executed in user mode.</param> 40 /// <remarks>A process can be associated with only one console, so the AllocConsole 41 /// function fails if the calling process already has a console. A process can 42 /// use the FreeConsole function to detach itself from its current console, then 43 /// it can call AllocConsole to create a new console or AttachConsole to attach 44 /// to another console. 45 /// 46 /// If the calling process creates a child process, the child inherits the 47 /// new console. 48 /// 49 /// AllocConsole initializes standard input, standard output, and standard error 50 /// handles for the new console. The standard input handle is a handle to the 51 /// console's input buffer, and the standard output and standard error handles 52 /// are handles to the console's screen buffer. To retrieve these handles, use 53 /// the GetStdHandle function. 54 /// 55 /// This function is primarily used by graphical user interface (GUI) application 56 /// to create a console window. GUI applications are initialized without a 57 /// console. Console applications are initialized with a console, unless they 58 /// are created as detached processes (by calling the CreateProcess function 59 /// with the DETACHED_PROCESS flag).</remarks> 60 public static bool AllocConsole() 61 { 62 return NativeMethods.AllocConsole(); 63 } 64 65 /// <summary> 66 /// Detaches the calling process from its console. 67 /// </summary> 171 68 /// <returns>If the function succeeds, the return value is nonzero. 172 69 /// 173 70 /// If the function fails, the return value is zero. To get extended error 174 71 /// information, call Marshal.GetLastWin32Error.</returns> 175 [DllImport("Kernel32.dll", SetLastError = true)] 176 [return: MarshalAs(UnmanagedType.Bool)] 177 public static extern bool GetThreadTimes(IntPtr hThread, out long lpCreationTime, 178 out long lpExitTime, out long lpKernelTime, out long lpUserTime); 179 180 /// <summary> 181 /// Retrieves the number of milliseconds that have elapsed since the system 182 /// was started, up to 49.7 days. 183 /// </summary> 184 /// <returns>The return value is the number of milliseconds that have elapsed 185 /// since the system was started.</returns> 186 /// <remarks>The resolution is limited to the resolution of the system timer. 187 /// This value is also affected by adjustments made by the GetSystemTimeAdjustment 188 /// function. 189 /// 190 /// The elapsed time is stored as a DWORD value. Therefore, the time will 191 /// wrap around to zero if the system is run continuously for 49.7 days. 192 /// To avoid this problem, use GetTickCount64. Otherwise, check for an 193 /// overflow condition when comparing times. 194 /// 195 /// If you need a higher resolution timer, use a multimedia timer or a 196 /// high-resolution timer. 197 /// 198 /// To obtain the time elapsed since the computer was started, retrieve 199 /// the System Up Time counter in the performance data in the registry key 200 /// HKEY_PERFORMANCE_DATA. The value returned is an 8-byte value. For more 201 /// information, see Performance Counters.</remarks> 202 [DllImport("Kernel32.dll")] 203 public static extern uint GetTickCount(); 204 205 /// <summary> 206 /// Retrieves information about the system's current usage of both physical 207 /// and virtual memory. 208 /// </summary> 209 /// <param name="lpBuffer">A pointer to a MEMORYSTATUSEX structure that 210 /// receives information about current memory availability.</param> 211 /// <returns>If the function succeeds, the return value is nonzero. 212 /// If the function fails, the return value is zero. To get extended 213 /// error information, call Marshal.GetLastWin32Error.</returns> 214 [DllImport("Kernel32.dll", SetLastError = true)] 215 [return: MarshalAs(UnmanagedType.Bool)] 216 public static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer); 217 218 /// <summary> 219 /// The QueryPerformanceCounter function retrieves the current value of 220 /// the high-resolution performance counter. 221 /// </summary> 222 /// <param name="lpPerformanceCount">[out] Pointer to a variable that receives 223 /// the current performance-counter value, in counts.</param> 224 /// <returns>If the function succeeds, the return value is nonzero. 225 /// 226 /// If the function fails, the return value is zero. To get extended error 227 /// information, call Marshal.GetLastWin32Error. </returns> 228 [DllImport("Kernel32.dll", SetLastError = true)] 229 [return: MarshalAs(UnmanagedType.Bool)] 230 public static extern bool QueryPerformanceCounter(out long lpPerformanceCount); 231 232 /// <summary> 233 /// Contains information about the current state of both physical and 234 /// virtual memory, including extended memory. The GlobalMemoryStatusEx 235 /// function stores information in this structure. 236 /// </summary> 237 [StructLayout(LayoutKind.Sequential)] 238 public struct MEMORYSTATUSEX 72 /// <remarks>A process can be attached to at most one console. If the calling 73 /// process is not already attached to a console, the error code returned is 74 /// ERROR_INVALID_PARAMETER (87). 75 /// 76 /// A process can use the FreeConsole function to detach itself from its 77 /// console. If other processes share the console, the console is not destroyed, 78 /// but the process that called FreeConsole cannot refer to it. A console is 79 /// closed when the last process attached to it terminates or calls FreeConsole. 80 /// After a process calls FreeConsole, it can call the AllocConsole function to 81 /// create a new console or AttachConsole to attach to another console.</remarks> 82 public static bool FreeConsole() 239 83 { 240 /// <summary> 241 /// The size of the structure, in bytes. You must set this member 242 /// before calling GlobalMemoryStatusEx. 243 /// </summary> 244 public uint dwLength; 245 246 /// <summary> 247 /// A number between 0 and 100 that specifies the approximate percentage 248 /// of physical memory that is in use (0 indicates no memory use and 249 /// 100 indicates full memory use). 250 /// </summary> 251 public uint dwMemoryLoad; 252 253 /// <summary> 254 /// The amount of actual physical memory, in bytes. 255 /// </summary> 256 public ulong ullTotalPhys; 257 258 /// <summary> 259 /// The amount of physical memory currently available, in bytes. This 260 /// is the amount of physical memory that can be immediately reused 261 /// without having to write its contents to disk first. It is the sum 262 /// of the size of the standby, free, and zero lists. 263 /// </summary> 264 public ulong ullAvailPhys; 265 266 /// <summary> 267 /// The current committed memory limit for the system or the current 268 /// process, whichever is smaller, in bytes. To get the system-wide 269 /// committed memory limit, call GetPerformanceInfo. 270 /// </summary> 271 public ulong ullTotalPageFile; 272 273 /// <summary> 274 /// The maximum amount of memory the current process can commit, in 275 /// bytes. This value is equal to or smaller than the system-wide 276 /// available commit value. To calculate the system-wide available 277 /// commit value, call GetPerformanceInfo and subtract the value of 278 /// CommitTotal from the value of CommitLimit. 279 /// </summary> 280 public ulong ullAvailPageFile; 281 282 /// <summary> 283 /// The size of the user-mode portion of the virtual address space of 284 /// the calling process, in bytes. This value depends on the type of 285 /// process, the type of processor, and the configuration of the 286 /// operating system. For example, this value is approximately 2 GB 287 /// for most 32-bit processes on an x86 processor and approximately 288 /// 3 GB for 32-bit processes that are large address aware running on 289 /// a system with 4-gigabyte tuning enabled. 290 /// </summary> 291 public ulong ullTotalVirtual; 292 293 /// <summary> 294 /// The amount of unreserved and uncommitted memory currently in the 295 /// user-mode portion of the virtual address space of the calling 296 /// process, in bytes. 297 /// </summary> 298 public ulong ullAvailVirtual; 299 300 /// <summary> 301 /// Reserved. This value is always 0. 302 /// </summary> 303 private ulong ullAvailExtendedVirtual; 84 return NativeMethods.FreeConsole(); 304 85 } 305 86 306 87 /// <summary> 307 /// Contains information about the current computer system. This includes 308 /// the architecture and type of the processor, the number of processors 309 /// in the system, the page size, and other such information. 310 /// </summary> 311 [StructLayout(LayoutKind.Sequential)] 312 public struct SYSTEM_INFO 88 /// Retrieves the current value of the high-resolution performance counter. 89 /// </summary> 90 public static long PerformanceCounter 313 91 { 314 /// <summary> 315 /// Represents a list of processor architectures. 316 /// </summary> 317 public enum ProcessorArchitecture : ushort 92 get 318 93 { 319 /// <summary> 320 /// x64 (AMD or Intel). 321 /// </summary> 322 PROCESSOR_ARCHITECTURE_AMD64 = 9, 323 324 /// <summary> 325 /// Intel Itanium Processor Family (IPF). 326 /// </summary> 327 PROCESSOR_ARCHITECTURE_IA64 = 6, 328 329 /// <summary> 330 /// x86. 331 /// </summary> 332 PROCESSOR_ARCHITECTURE_INTEL = 0, 333 334 /// <summary> 335 /// Unknown architecture. 336 /// </summary> 337 PROCESSOR_ARCHITECTURE_UNKNOWN = 0xffff 94 long result = 0; 95 if (NativeMethods.QueryPerformanceCounter(out result)) 96 return result; 97 return 0; 338 98 } 339 340 /// <summary>341 /// The processor architecture of the installed operating system.342 /// This member can be one of the ProcessorArchitecture values.343 /// </summary>344 public ProcessorArchitecture processorArchitecture;345 346 /// <summary>347 /// This member is reserved for future use.348 /// </summary>349 private const ushort reserved = 0;350 351 /// <summary>352 /// The page size and the granularity of page protection and commitment.353 /// This is the page size used by the VirtualAlloc function.354 /// </summary>355 public uint pageSize;356 357 /// <summary>358 /// A pointer to the lowest memory address accessible to applications359 /// and dynamic-link libraries (DLLs).360 /// </summary>361 public IntPtr minimumApplicationAddress;362 363 /// <summary>364 /// A pointer to the highest memory address accessible to applications365 /// and DLLs.366 /// </summary>367 public IntPtr maximumApplicationAddress;368 369 /// <summary>370 /// A mask representing the set of processors configured into the system.371 /// Bit 0 is processor 0; bit 31 is processor 31.372 /// </summary>373 public IntPtr activeProcessorMask;374 375 /// <summary>376 /// The number of processors in the system.377 /// </summary>378 public uint numberOfProcessors;379 380 /// <summary>381 /// An obsolete member that is retained for compatibility. Use the382 /// wProcessorArchitecture, wProcessorLevel, and wProcessorRevision383 /// members to determine the type of processor.384 /// Name Value385 /// PROCESSOR_INTEL_386 386386 /// PROCESSOR_INTEL_486 486387 /// PROCESSOR_INTEL_PENTIUM 586388 /// PROCESSOR_INTEL_IA64 2200389 /// PROCESSOR_AMD_X8664 8664390 /// </summary>391 public uint processorType;392 393 /// <summary>394 /// The granularity for the starting address at which virtual memory395 /// can be allocated. For more information, see VirtualAlloc.396 /// </summary>397 public uint allocationGranularity;398 399 /// <summary>400 /// The architecture-dependent processor level. It should be used only401 /// for display purposes. To determine the feature set of a processor,402 /// use the IsProcessorFeaturePresent function.403 ///404 /// If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_INTEL, wProcessorLevel405 /// is defined by the CPU vendor.406 /// If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_IA64, wProcessorLevel407 /// is set to 1.408 /// </summary>409 public ushort processorLevel;410 411 /// <summary>412 /// The architecture-dependent processor revision. The following table413 /// shows how the revision value is assembled for each type of414 /// processor architecture.415 ///416 /// Processor Value417 /// Intel Pentium, Cyrix The high byte is the model and the418 /// or NextGen 586 low byte is the stepping. For example,419 /// if the value is xxyy, the model number420 /// and stepping can be displayed as follows:421 /// Model xx, Stepping yy422 /// Intel 80386 or 80486 A value of the form xxyz.423 /// If xx is equal to 0xFF, y - 0xA is the model424 /// number, and z is the stepping identifier.425 ///426 /// If xx is not equal to 0xFF, xx + 'A'427 /// is the stepping letter and yz is the minor stepping.428 /// </summary>429 public ushort processorRevision;430 99 } 431 100 432 101 /// <summary> 433 /// Specifies the window station, desktop, standard handles, and appearance 434 /// of the main window for a process at creation time. 435 /// </summary> 436 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 437 public struct STARTUPINFO 102 /// Gets the current CPU type of the system. 103 /// </summary> 104 /// <returns>One of the <see cref="ProcessorTypes"/> enumeration values.</returns> 105 public static ProcessorArchitecture ProcessorArchitecture 438 106 { 439 /// <summary> 440 /// The size of the structure, in bytes. 441 /// </summary> 442 public uint cbSize; 443 444 /// <summary> 445 /// Reserved; must be NULL. 446 /// </summary> 447 private readonly IntPtr lpReserved; 448 449 /// <summary> 450 /// The name of the desktop, or the name of both the desktop and window 451 /// station for this process. A backslash in the string indicates that 452 /// the string includes both the desktop and window station names. 453 /// For more information, see Thread Connection to a Desktop. 454 /// </summary> 455 public string lpDesktop 107 get 456 108 { 457 get 109 NativeMethods.SYSTEM_INFO info = new NativeMethods.SYSTEM_INFO(); 110 NativeMethods.GetSystemInfo(out info); 111 112 switch (info.processorArchitecture) 458 113 { 459 return Marshal.PtrToStringAuto(lpDesktop2); 460 } 461 set 462 { 463 lpDesktop2 = Marshal.StringToHGlobalUni(value); 114 case NativeMethods.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64: 115 return ProcessorArchitecture.Amd64; 116 case NativeMethods.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_IA64: 117 return ProcessorArchitecture.IA64; 118 case NativeMethods.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_INTEL: 119 return ProcessorArchitecture.X86; 120 default: 121 return ProcessorArchitecture.None; 464 122 } 465 123 } 466 private IntPtr lpDesktop2;467 468 /// <summary>469 /// For console processes, this is the title displayed in the title470 /// bar if a new console window is created. If NULL, the name of the471 /// executable file is used as the window title instead. This parameter472 /// must be NULL for GUI or console processes that do not create a473 /// new console window.474 /// </summary>475 public string lpTitle476 {477 get478 {479 return Marshal.PtrToStringAuto(lpTitle2);480 }481 set482 {483 lpTitle2 = Marshal.StringToHGlobalUni(value);484 }485 }486 private IntPtr lpTitle2;487 488 /// <summary>489 /// If dwFlags specifies STARTF_USEPOSITION, this member is the x490 /// offset of the upper left corner of a window if a new window is491 /// created, in pixels. Otherwise, this member is ignored.492 ///493 /// The offset is from the upper left corner of the screen. For GUI494 /// processes, the specified position is used the first time the495 /// new process calls CreateWindow to create an overlapped window496 /// if the x parameter of CreateWindow is CW_USEDEFAULT.497 /// </summary>498 public int dwX;499 500 /// <summary>501 /// If dwFlags specifies STARTF_USEPOSITION, this member is the y502 /// offset of the upper left corner of a window if a new window is503 /// created, in pixels. Otherwise, this member is ignored.504 ///505 /// The offset is from the upper left corner of the screen. For GUI506 /// processes, the specified position is used the first time the new507 /// process calls CreateWindow to create an overlapped window if the508 /// y parameter of CreateWindow is CW_USEDEFAULT.509 /// </summary>510 public int dwY;511 512 /// <summary>513 /// If dwFlags specifies STARTF_USESIZE, this member is the width of514 /// the window if a new window is created, in pixels. Otherwise,515 /// this member is ignored.516 ///517 /// For GUI processes, this is used only the first time the new process518 /// calls CreateWindow to create an overlapped window if the nWidth519 /// parameter of CreateWindow is CW_USEDEFAULT.520 /// </summary>521 public int dwXSize;522 523 /// <summary>524 /// If dwFlags specifies STARTF_USESIZE, this member is the height of525 /// the window if a new window is created, in pixels. Otherwise, this526 /// member is ignored.527 ///528 /// For GUI processes, this is used only the first time the new process529 /// calls CreateWindow to create an overlapped window if the nHeight530 /// parameter of CreateWindow is CW_USEDEFAULT.531 /// </summary>532 public int dwYSize;533 534 /// <summary>535 /// If dwFlags specifies STARTF_USECOUNTCHARS, if a new console window536 /// is created in a console process, this member specifies the screen537 /// buffer width, in character columns. Otherwise, this member is ignored.538 /// </summary>539 public int dwXCountChars;540 541 /// <summary>542 /// If dwFlags specifies STARTF_USECOUNTCHARS, if a new console window543 /// is created in a console process, this member specifies the screen544 /// buffer height, in character rows. Otherwise, this member is ignored.545 /// </summary>546 public int dwYCountChars;547 548 /// <summary>549 /// If dwFlags specifies STARTF_USEFILLATTRIBUTE, this member is the550 /// initial text and background colors if a new console window is551 /// created in a console application. Otherwise, this member is ignored.552 ///553 /// This value can be any combination of the following values:554 /// FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_RED, FOREGROUND_INTENSITY,555 /// BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_RED, and BACKGROUND_INTENSITY.556 ///557 /// For example, the following combination of values produces red text558 /// on a white background:559 /// FOREGROUND_RED| BACKGROUND_RED| BACKGROUND_GREEN| BACKGROUND_BLUE560 /// </summary>561 public int dwFillAttribute;562 563 /// <summary>564 /// A bit field that determines whether certain STARTUPINFO members565 /// are used when the process creates a window.566 /// </summary>567 public int dwFlags;568 569 /// <summary>570 /// If dwFlags specifies STARTF_USESHOWWINDOW, this member can be any571 /// of the SW_ constants defined in Winuser.h. Otherwise, this member is ignored.572 ///573 /// For GUI processes, wShowWindow specifies the default value the574 /// first time ShowWindow is called. The nCmdShow parameter of ShowWindow575 /// is ignored. In subsequent calls to ShowWindow, the wShowWindow member576 /// is used if the nCmdShow parameter of ShowWindow is set to SW_SHOWDEFAULT.577 /// </summary>578 public short wShowWindow;579 580 /// <summary>581 /// Reserved for use by the C Run-time; must be zero.582 /// </summary>583 private const short cbReserved2 = 0;584 585 /// <summary>586 /// Reserved for use by the C Run-time; must be NULL.587 /// </summary>588 private readonly IntPtr lpReserved2;589 590 /// <summary>591 /// If dwFlags specifies STARTF_USESTDHANDLES, this member is the592 /// standard input handle for the process. Otherwise, this member is593 /// ignored and the default for standard input is the keyboard buffer.594 /// </summary>595 public IntPtr hStdInput;596 597 /// <summary>598 /// If dwFlags specifies STARTF_USESTDHANDLES, this member is the599 /// standard output handle for the process. Otherwise, this member is600 /// ignored and the default for standard output is the console window's601 /// buffer.602 /// </summary>603 public IntPtr hStdOutput;604 605 /// <summary>606 /// If dwFlags specifies STARTF_USESTDHANDLES, this member is the607 /// standard error handle for the process. Otherwise, this member is608 /// ignored and the default for standard error is the console window's609 /// buffer.610 /// </summary>611 public IntPtr hStdError;612 124 } 613 125 … … 666 178 /// This function does not stop the screen saver from executing. 667 179 /// </remarks> 668 [DllImport("Kernel32.dll")] 669 public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); 670 671 /// <summary> 672 /// Execution state values to be used in conjuction with SetThreadExecutionState 673 /// </summary> 674 public enum EXECUTION_STATE : uint 180 public static ThreadExecutionState SetThreadExecutionState(ThreadExecutionState esFlags) 675 181 { 676 /// <summary> 677 /// Enables away mode. This value must be specified with ES_CONTINUOUS. 182 return (ThreadExecutionState)NativeMethods.SetThreadExecutionState( 183 (NativeMethods.EXECUTION_STATE)esFlags); 184 } 185 186 /// <summary> 187 /// Stores Kernel32.dll functions, structs and constants. 188 /// </summary> 189 internal class NativeMethods 190 { 191 /// <summary> 192 /// Closes an open object handle. 193 /// </summary> 194 /// <param name="hObject">A valid handle to an open object.</param> 195 /// <returns>If the function succeeds, the return value is true. To get 196 /// extended error information, call Marshal.GetLastWin32Error(). 678 197 /// 679 /// Away mode should be used only by media-recording and media-distribution 680 /// applications that must perform critical background processing on 681 /// desktop computers while the computer appears to be sleeping. 682 /// See remarks. 198 /// If the application is running under a debugger, the function will throw 199 /// an exception if it receives either a handle value that is not valid 200 /// or a pseudo-handle value. This can happen if you close a handle twice, 201 /// or if you call CloseHandle on a handle returned by the FindFirstFile 202 /// function.</returns> 203 [DllImport("Kernel32.dll", SetLastError = true)] 204 [return: MarshalAs(UnmanagedType.Bool)] 205 public static extern bool CloseHandle(IntPtr hObject); 206 207 /// <summary> 208 /// Retrieves a pseudo handle for the current process. 209 /// </summary> 210 /// <returns>A pseudo handle to the current process.</returns> 211 /// <remarks>A pseudo handle is a special constant, currently (HANDLE)-1, 212 /// that is interpreted as the current process handle. For compatibility 213 /// with future operating systems, it is best to call GetCurrentProcess 214 /// instead of hard-coding this constant value. The calling process can 215 /// use a pseudo handle to specify its own process whenever a process 216 /// handle is required. Pseudo handles are not inherited by child processes. 683 217 /// 684 /// Windows Server 2003 and Windows XP/2000: ES_AWAYMODE_REQUIRED is 685 /// not supported. 686 /// </summary> 687 ES_AWAYMODE_REQUIRED = 0x00000040, 688 689 /// <summary> 690 /// Informs the system that the state being set should remain in effect 691 /// until the next call that uses ES_CONTINUOUS and one of the other 692 /// state flags is cleared. 693 /// </summary> 694 ES_CONTINUOUS = 0x80000000, 695 696 /// <summary> 697 /// Forces the display to be on by resetting the display idle timer. 698 /// </summary> 699 ES_DISPLAY_REQUIRED = 0x00000002, 700 701 /// <summary> 702 /// Forces the system to be in the working state by resetting the system 703 /// idle timer. 704 /// </summary> 705 ES_SYSTEM_REQUIRED = 0x00000001, 706 707 /// <summary> 708 /// This value is not supported. If ES_USER_PRESENT is combined with 709 /// other esFlags values, the call will fail and none of the specified 710 /// states will be set. 218 /// This handle has the maximum possible access to the process object. 219 /// For systems that support security descriptors, this is the maximum 220 /// access allowed by the security descriptor for the calling process. 221 /// For systems that do not support security descriptors, this is 222 /// PROCESS_ALL_ACCESS. For more information, see Process Security and 223 /// Access Rights. 711 224 /// 712 /// Windows Server 2003 and Windows XP/2000: Informs the system that a 713 /// user is present and resets the display and system idle timers. 714 /// ES_USER_PRESENT must be called with ES_CONTINUOUS. 715 /// </summary> 716 ES_USER_PRESENT = 0x00000004 225 /// A process can create a "real" handle to itself that is valid in the 226 /// context of other processes, or that can be inherited by other processes, 227 /// by specifying the pseudo handle as the source handle in a call to the 228 /// DuplicateHandle function. A process can also use the OpenProcess 229 /// function to open a real handle to itself. 230 /// 231 /// The pseudo handle need not be closed when it is no longer needed. 232 /// Calling the CloseHandle function with a pseudo handle has no effect. 233 /// If the pseudo handle is duplicated by DuplicateHandle, the duplicate 234 /// handle must be closed.</remarks> 235 [DllImport("Kernel32.dll", SetLastError = true)] 236 public static extern IntPtr GetCurrentProcess(); 237 238 /// <summary> 239 /// Retrieves the contents of the STARTUPINFO structure that was specified 240 /// when the calling process was created. 241 /// </summary> 242 /// <param name="lpStartupInfo">A pointer to a STARTUPINFO structure that 243 /// receives the startup information.</param> 244 [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)] 245 public static extern void GetStartupInfo(out STARTUPINFO lpStartupInfo); 246 247 /// <summary> 248 /// Retrieves information about the current system. 249 /// </summary> 250 /// <param name="lpSystemInfo">A pointer to a SYSTEM_INFO structure that 251 /// receives the information.</param> 252 [DllImport("Kernel32.dll")] 253 public static extern void GetSystemInfo(out SYSTEM_INFO lpSystemInfo); 254 255 /// <summary> 256 /// The QueryPerformanceCounter function retrieves the current value of 257 /// the high-resolution performance counter. 258 /// </summary> 259 /// <param name="lpPerformanceCount">[out] Pointer to a variable that receives 260 /// the current performance-counter value, in counts.</param> 261 /// <returns>If the function succeeds, the return value is nonzero. 262 /// 263 /// If the function fails, the return value is zero. To get extended error 264 /// information, call Marshal.GetLastWin32Error. </returns> 265 [DllImport("Kernel32.dll", SetLastError = true)] 266 [return: MarshalAs(UnmanagedType.Bool)] 267 public static extern bool QueryPerformanceCounter(out long lpPerformanceCount); 268 269 /// <summary> 270 /// Contains information about the current computer system. This includes 271 /// the architecture and type of the processor, the number of processors 272 /// in the system, the page size, and other such information. 273 /// </summary> 274 [StructLayout(LayoutKind.Sequential)] 275 internal struct SYSTEM_INFO 276 { 277 /// <summary> 278 /// Represents a list of processor architectures. 279 /// </summary> 280 public enum ProcessorArchitecture : ushort 281 { 282 /// <summary> 283 /// x64 (AMD or Intel). 284 /// </summary> 285 PROCESSOR_ARCHITECTURE_AMD64 = 9, 286 287 /// <summary> 288 /// Intel Itanium Processor Family (IPF). 289 /// </summary> 290 PROCESSOR_ARCHITECTURE_IA64 = 6, 291 292 /// <summary> 293 /// x86. 294 /// </summary> 295 PROCESSOR_ARCHITECTURE_INTEL = 0, 296 297 /// <summary> 298 /// Unknown architecture. 299 /// </summary> 300 PROCESSOR_ARCHITECTURE_UNKNOWN = 0xffff 301 } 302 303 /// <summary> 304 /// The processor architecture of the installed operating system. 305 /// This member can be one of the ProcessorArchitecture values. 306 /// </summary> 307 public ProcessorArchitecture processorArchitecture; 308 309 /// <summary> 310 /// This member is reserved for future use. 311 /// </summary> 312 private const ushort reserved = 0; 313 314 /// <summary> 315 /// The page size and the granularity of page protection and commitment. 316 /// This is the page size used by the VirtualAlloc function. 317 /// </summary> 318 public uint pageSize; 319 320 /// <summary> 321 /// A pointer to the lowest memory address accessible to applications 322 /// and dynamic-link libraries (DLLs). 323 /// </summary> 324 public IntPtr minimumApplicationAddress; 325 326 /// <summary> 327 /// A pointer to the highest memory address accessible to applications 328 /// and DLLs. 329 /// </summary> 330 public IntPtr maximumApplicationAddress; 331 332 /// <summary> 333 /// A mask representing the set of processors configured into the system. 334 /// Bit 0 is processor 0; bit 31 is processor 31. 335 /// </summary> 336 public IntPtr activeProcessorMask; 337 338 /// <summary> 339 /// The number of processors in the system. 340 /// </summary> 341 public uint numberOfProcessors; 342 343 /// <summary> 344 /// An obsolete member that is retained for compatibility. Use the 345 /// wProcessorArchitecture, wProcessorLevel, and wProcessorRevision 346 /// members to determine the type of processor. 347 /// Name Value 348 /// PROCESSOR_INTEL_386 386 349 /// PROCESSOR_INTEL_486 486 350 /// PROCESSOR_INTEL_PENTIUM 586 351 /// PROCESSOR_INTEL_IA64 2200 352 /// PROCESSOR_AMD_X8664 8664 353 /// </summary> 354 public uint processorType; 355 356 /// <summary> 357 /// The granularity for the starting address at which virtual memory 358 /// can be allocated. For more information, see VirtualAlloc. 359 /// </summary> 360 public uint allocationGranularity; 361 362 /// <summary> 363 /// The architecture-dependent processor level. It should be used only 364 /// for display purposes. To determine the feature set of a processor, 365 /// use the IsProcessorFeaturePresent function. 366 /// 367 /// If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_INTEL, wProcessorLevel 368 /// is defined by the CPU vendor. 369 /// If wProcessorArchitecture is PROCESSOR_ARCHITECTURE_IA64, wProcessorLevel 370 /// is set to 1. 371 /// </summary> 372 public ushort processorLevel; 373 374 /// <summary> 375 /// The architecture-dependent processor revision. The following table 376 /// shows how the revision value is assembled for each type of 377 /// processor architecture. 378 /// 379 /// Processor Value 380 /// Intel Pentium, Cyrix The high byte is the model and the 381 /// or NextGen 586 low byte is the stepping. For example, 382 /// if the value is xxyy, the model number 383 /// and stepping can be displayed as follows: 384 /// Model xx, Stepping yy 385 /// Intel 80386 or 80486 A value of the form xxyz. 386 /// If xx is equal to 0xFF, y - 0xA is the model 387 /// number, and z is the stepping identifier. 388 /// 389 /// If xx is not equal to 0xFF, xx + 'A' 390 /// is the stepping letter and yz is the minor stepping. 391 /// </summary> 392 public ushort processorRevision; 393 } 394 395 /// <summary> 396 /// Specifies the window station, desktop, standard handles, and appearance 397 /// of the main window for a process at creation time. 398 /// </summary> 399 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 400 public struct STARTUPINFO 401 { 402 /// <summary> 403 /// The size of the structure, in bytes. 404 /// </summary> 405 public uint cbSize; 406 407 /// <summary> 408 /// Reserved; must be NULL. 409 /// </summary> 410 private readonly IntPtr lpReserved; 411 412 /// <summary> 413 /// The name of the desktop, or the name of both the desktop and window 414 /// station for this process. A backslash in the string indicates that 415 /// the string includes both the desktop and window station names. 416 /// For more information, see Thread Connection to a Desktop. 417 /// </summary> 418 public string lpDesktop 419 { 420 get 421 { 422 return Marshal.PtrToStringAuto(lpDesktop2); 423 } 424 set 425 { 426 lpDesktop2 = Marshal.StringToHGlobalUni(value); 427 } 428 } 429 private IntPtr lpDesktop2; 430 431 /// <summary> 432 /// For console processes, this is the title displayed in the title 433 /// bar if a new console window is created. If NULL, the name of the 434 /// executable file is used as the window title instead. This parameter 435 /// must be NULL for GUI or console processes that do not create a 436 /// new console window. 437 /// </summary> 438 public string lpTitle 439 { 440 get 441 { 442 return Marshal.PtrToStringAuto(lpTitle2); 443 } 444 set 445 { 446 lpTitle2 = Marshal.StringToHGlobalUni(value); 447 } 448 } 449 private IntPtr lpTitle2; 450 451 /// <summary> 452 /// If dwFlags specifies STARTF_USEPOSITION, this member is the x 453 /// offset of the upper left corner of a window if a new window is 454 /// created, in pixels. Otherwise, this member is ignored. 455 /// 456 /// The offset is from the upper left corner of the screen. For GUI 457 /// processes, the specified position is used the first time the 458 /// new process calls CreateWindow to create an overlapped window 459 /// if the x parameter of CreateWindow is CW_USEDEFAULT. 460 /// </summary> 461 public int dwX; 462 463 /// <summary> 464 /// If dwFlags specifies STARTF_USEPOSITION, this member is the y 465 /// offset of the upper left corner of a window if a new window is 466 /// created, in pixels. Otherwise, this member is ignored. 467 /// 468 /// The offset is from the upper left corner of the screen. For GUI 469 /// processes, the specified position is used the first time the new 470 /// process calls CreateWindow to create an overlapped window if the 471 /// y parameter of CreateWindow is CW_USEDEFAULT. 472 /// </summary> 473 public int dwY; 474 475 /// <summary> 476 /// If dwFlags specifies STARTF_USESIZE, this member is the width of 477 /// the window if a new window is created, in pixels. Otherwise, 478 /// this member is ignored. 479 /// 480 /// For GUI processes, this is used only the first time the new process 481 /// calls CreateWindow to create an overlapped window if the nWidth 482 /// parameter of CreateWindow is CW_USEDEFAULT. 483 /// </summary> 484 public int dwXSize; 485 486 /// <summary> 487 /// If dwFlags specifies STARTF_USESIZE, this member is the height of 488 /// the window if a new window is created, in pixels. Otherwise, this 489 /// member is ignored. 490 /// 491 /// For GUI processes, this is used only the first time the new process 492 /// calls CreateWindow to create an overlapped window if the nHeight 493 /// parameter of CreateWindow is CW_USEDEFAULT. 494 /// </summary> 495 public int dwYSize; 496 497 /// <summary> 498 /// If dwFlags specifies STARTF_USECOUNTCHARS, if a new console window 499 /// is created in a console process, this member specifies the screen 500 /// buffer width, in character columns. Otherwise, this member is ignored. 501 /// </summary> 502 public int dwXCountChars; 503 504 /// <summary> 505 /// If dwFlags specifies STARTF_USECOUNTCHARS, if a new console window 506 /// is created in a console process, this member specifies the screen 507 /// buffer height, in character rows. Otherwise, this member is ignored. 508 /// </summary> 509 public int dwYCountChars; 510 511 /// <summary> 512 /// If dwFlags specifies STARTF_USEFILLATTRIBUTE, this member is the 513 /// initial text and background colors if a new console window is 514 /// created in a console application. Otherwise, this member is ignored. 515 /// 516 /// This value can be any combination of the following values: 517 /// FOREGROUND_BLUE, FOREGROUND_GREEN, FOREGROUND_RED, FOREGROUND_INTENSITY, 518 /// BACKGROUND_BLUE, BACKGROUND_GREEN, BACKGROUND_RED, and BACKGROUND_INTENSITY. 519 /// 520 /// For example, the following combination of values produces red text 521 /// on a white background: 522 /// FOREGROUND_RED| BACKGROUND_RED| BACKGROUND_GREEN| BACKGROUND_BLUE 523 /// </summary> 524 public int dwFillAttribute; 525 526 /// <summary> 527 /// A bit field that determines whether certain STARTUPINFO members 528 /// are used when the process creates a window. 529 /// </summary> 530 public int dwFlags; 531 532 /// <summary> 533 /// If dwFlags specifies STARTF_USESHOWWINDOW, this member can be any 534 /// of the SW_ constants defined in Winuser.h. Otherwise, this member is ignored. 535 /// 536 /// For GUI processes, wShowWindow specifies the default value the 537 /// first time ShowWindow is called. The nCmdShow parameter of ShowWindow 538 /// is ignored. In subsequent calls to ShowWindow, the wShowWindow member 539 /// is used if the nCmdShow parameter of ShowWindow is set to SW_SHOWDEFAULT. 540 /// </summary> 541 public short wShowWindow; 542 543 /// <summary> 544 /// Reserved for use by the C Run-time; must be zero. 545 /// </summary> 546 private const short cbReserved2 = 0; 547 548 /// <summary> 549 /// Reserved for use by the C Run-time; must be NULL. 550 /// </summary> 551 private readonly IntPtr lpReserved2; 552 553 /// <summary> 554 /// If dwFlags specifies STARTF_USESTDHANDLES, this member is the 555 /// standard input handle for the process. Otherwise, this member is 556 /// ignored and the default for standard input is the keyboard buffer. 557 /// </summary> 558 public IntPtr hStdInput; 559 560 /// <summary> 561 /// If dwFlags specifies STARTF_USESTDHANDLES, this member is the 562 /// standard output handle for the process. Otherwise, this member is 563 /// ignored and the default for standard output is the console window's 564 /// buffer. 565 /// </summary> 566 public IntPtr hStdOutput; 567 568 /// <summary> 569 /// If dwFlags specifies STARTF_USESTDHANDLES, this member is the 570 /// standard error handle for the process. Otherwise, this member is 571 /// ignored and the default for standard error is the console window's 572 /// buffer. 573 /// </summary> 574 public IntPtr hStdError; 575 } 576 577 [DllImport("Kernel32.dll")] 578 public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags); 579 580 /// <summary> 581 /// Execution state values to be used in conjuction with SetThreadExecutionState 582 /// </summary> 583 [Flags] 584 public enum EXECUTION_STATE : uint 585 { 586 ES_AWAYMODE_REQUIRED = 0x00000040, 587 ES_CONTINUOUS = 0x80000000, 588 ES_DISPLAY_REQUIRED = 0x00000002, 589 ES_SYSTEM_REQUIRED = 0x00000001, 590 ES_USER_PRESENT = 0x00000004 591 } 592 593 [DllImport("Kernel32.dll", SetLastError = true)] 594 public static extern bool AllocConsole(); 595 596 [DllImport("Kernel32.dll", SetLastError = true)] 597 public static extern bool FreeConsole(); 717 598 } 718 719 /// <summary> 720 /// Allocates a new console for the calling process. 721 /// </summary> 722 /// <returns>If the function succeeds, the return value is nonzero. 723 /// 724 /// If the function fails, the return value is zero. To get extended error 725 /// information, call Marshal.GetLastWin32Error.</returns> 726 /// <remarks>A process can be associated with only one console, so the AllocConsole 727 /// function fails if the calling process already has a console. A process can 728 /// use the FreeConsole function to detach itself from its current console, then 729 /// it can call AllocConsole to create a new console or AttachConsole to attach 730 /// to another console. 731 /// 732 /// If the calling process creates a child process, the child inherits the 733 /// new console. 734 /// 735 /// AllocConsole initializes standard input, standard output, and standard error 736 /// handles for the new console. The standard input handle is a handle to the 737 /// console's input buffer, and the standard output and standard error handles 738 /// are handles to the console's screen buffer. To retrieve these handles, use 739 /// the GetStdHandle function. 740 /// 741 /// This function is primarily used by graphical user interface (GUI) application 742 /// to create a console window. GUI applications are initialized without a 743 /// console. Console applications are initialized with a console, unless they 744 /// are created as detached processes (by calling the CreateProcess function 745 /// with the DETACHED_PROCESS flag).</remarks> 746 [DllImport("Kernel32.dll", SetLastError = true)] 747 public static extern bool AllocConsole(); 748 749 /// <summary> 750 /// Detaches the calling process from its console. 751 /// </summary> 752 /// <returns>If the function succeeds, the return value is nonzero. 753 /// 754 /// If the function fails, the return value is zero. To get extended error 755 /// information, call Marshal.GetLastWin32Error.</returns> 756 /// <remarks>A process can be attached to at most one console. If the calling 757 /// process is not already attached to a console, the error code returned is 758 /// ERROR_INVALID_PARAMETER (87). 759 /// 760 /// A process can use the FreeConsole function to detach itself from its 761 /// console. If other processes share the console, the console is not destroyed, 762 /// but the process that called FreeConsole cannot refer to it. A console is 763 /// closed when the last process attached to it terminates or calls FreeConsole. 764 /// After a process calls FreeConsole, it can call the AllocConsole function to 765 /// create a new console or AttachConsole to attach to another console.</remarks> 766 [DllImport("Kernel32.dll", SetLastError = true)] 767 public static extern bool FreeConsole(); 599 } 600 601 public enum ThreadExecutionState 602 { 603 /// <summary> 604 /// Enables away mode. This value must be specified with ES_CONTINUOUS. 605 /// 606 /// Away mode should be used only by media-recording and media-distribution 607 /// applications that must perform critical background processing on 608 /// desktop computers while the computer appears to be sleeping. 609 /// See remarks. 610 /// 611 /// Windows Server 2003 and Windows XP/2000: ES_AWAYMODE_REQUIRED is 612 /// not supported. 613 /// </summary> 614 AwayModeRequired = (int)KernelAPI.NativeMethods.EXECUTION_STATE.ES_AWAYMODE_REQUIRED, 615 616 /// <summary> 617 /// Informs the system that the state being set should remain in effect 618 /// until the next call that uses ES_CONTINUOUS and one of the other 619 /// state flags is cleared. 620 /// </summary> 621 Continuous = unchecked((int)KernelAPI.NativeMethods.EXECUTION_STATE.ES_CONTINUOUS), 622 623 /// <summary> 624 /// Forces the display to be on by resetting the display idle timer. 625 /// </summary> 626 DisplayRequired = (int)KernelAPI.NativeMethods.EXECUTION_STATE.ES_DISPLAY_REQUIRED, 627 628 /// <summary> 629 /// Forces the system to be in the working state by resetting the system 630 /// idle timer. 631 /// </summary> 632 SystemRequired = (int)KernelAPI.NativeMethods.EXECUTION_STATE.ES_SYSTEM_REQUIRED, 633 634 /// <summary> 635 /// This value is not supported. If ES_USER_PRESENT is combined with 636 /// other esFlags values, the call will fail and none of the specified 637 /// states will be set. 638 /// 639 /// Windows Server 2003 and Windows XP/2000: Informs the system that a 640 /// user is present and resets the display and system idle timers. 641 /// ES_USER_PRESENT must be called with ES_CONTINUOUS. 642 /// </summary> 643 UserPresent = (int)KernelAPI.NativeMethods.EXECUTION_STATE.ES_USER_PRESENT 768 644 } 769 645 } -
branches/eraser6/Util/Permissions.cs
r348 r993 63 63 //Get the process token. 64 64 IntPtr hToken = IntPtr.Zero; 65 bool result = OpenProcessToken(KernelAPI.GetCurrentProcess(), TOKEN_QUERY, out hToken); 65 bool result = OpenProcessToken(KernelAPI.NativeMethods.GetCurrentProcess(), 66 TOKEN_QUERY, out hToken); 66 67 if (!result || hToken == IntPtr.Zero) 67 68 throw new Exception("Could not open process token."); … … 85 86 finally 86 87 { 87 KernelAPI. CloseHandle(hToken);88 KernelAPI.NativeMethods.CloseHandle(hToken); 88 89 Marshal.FreeHGlobal(pElevationType); 89 90 } -
branches/eraser6/Util/ShellAPI.cs
r992 r993 24 24 using System.Text; 25 25 using System.Runtime.InteropServices; 26 using System.Windows.Forms; 26 27 27 28 namespace Eraser.Util … … 30 31 { 31 32 /// <summary> 32 /// Empties the Recycle Bin on the specified drive. 33 /// </summary> 34 /// <param name="hwnd">A handle to the parent window of any dialog boxes 35 /// that might be displayed during the operation. This parameter can be 36 /// NULL.</param> 37 /// <param name="pszRootPath">The address of a null-terminated string of 38 /// maximum length MAX_PATH that contains the path of the root drive on 39 /// which the Recycle Bin is located. This parameter can contain the address 40 /// of a string formatted with the drive, folder, and subfolder names, for 41 /// example c:\windows\system\. It can also contain an empty string or NULL. 42 /// If this value is an empty string or NULL, all Recycle Bins on all drives 43 /// will be emptied.</param> 44 /// <param name="dwFlags">One or more of the SHEmptyRecycleBinFlags</param> 45 /// <returns>Returns S_OK if successful, or a COM-defined error value 46 /// otherwise.</returns> 47 [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] 48 public static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, 49 SHEmptyRecycleBinFlags dwFlags); 50 51 public enum SHEmptyRecycleBinFlags : uint 33 /// Empties the recycle bin for the current user. 34 /// </summary> 35 /// <param name="flags">The list of flags to pass to the shell regarding 36 /// the user feedback, etc.</param> 37 public static void EmptyRecycleBin(EmptyRecycleBinFlags flags) 38 { 39 NativeMethods.SHEmptyRecycleBin(Form.ActiveForm.Handle, null, 40 (NativeMethods.SHEmptyRecycleBinFlags)flags); 41 } 42 43 /// <summary> 44 /// Encapsulates all functions, structs and constants from Shell32.dll 45 /// </summary> 46 internal class NativeMethods 52 47 { 53 48 /// <summary> 54 /// No dialog box confirming the deletion of the objects will be displayed.49 /// Empties the Recycle Bin on the specified drive. 55 50 /// </summary> 56 SHERB_NOCONFIRMATION = 0x00000001, 51 /// <param name="hwnd">A handle to the parent window of any dialog boxes 52 /// that might be displayed during the operation. This parameter can be 53 /// NULL.</param> 54 /// <param name="pszRootPath">The address of a null-terminated string of 55 /// maximum length MAX_PATH that contains the path of the root drive on 56 /// which the Recycle Bin is located. This parameter can contain the address 57 /// of a string formatted with the drive, folder, and subfolder names, for 58 /// example c:\windows\system\. It can also contain an empty string or NULL. 59 /// If this value is an empty string or NULL, all Recycle Bins on all drives 60 /// will be emptied.</param> 61 /// <param name="dwFlags">One or more of the SHEmptyRecycleBinFlags</param> 62 /// <returns>Returns S_OK if successful, or a COM-defined error value 63 /// otherwise.</returns> 64 [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] 65 public static extern uint SHEmptyRecycleBin(IntPtr hwnd, string pszRootPath, 66 SHEmptyRecycleBinFlags dwFlags); 67 68 public enum SHEmptyRecycleBinFlags : uint 69 { 70 SHERB_NOCONFIRMATION = 0x00000001, 71 SHERB_NOPROGRESSUI = 0x00000002, 72 SHERB_NOSOUND = 0x00000004 73 } 57 74 58 75 /// <summary> 59 /// No dialog box indicating the progress will be displayed. 76 /// Retrieves information about an object in the file system, such as a 77 /// file, folder, directory, or drive root. 60 78 /// </summary> 61 SHERB_NOPROGRESSUI = 0x00000002, 62 63 /// <summary> 64 /// No sound will be played when the operation is complete. 65 /// </summary> 66 SHERB_NOSOUND = 0x00000004 67 } 68 69 /// <summary> 70 /// Retrieves information about an object in the file system, such as a 71 /// file, folder, directory, or drive root. 72 /// </summary> 73 /// <param name="path">[in] A pointer to a null-terminated string of maximum 74 /// length MAX_PATH that contains the path and file name. Both absolute 75 /// and relative paths are valid. 76 /// 77 /// If the uFlags parameter includes the SHGFI_PIDL flag, this parameter 78 /// must be the address of an ITEMIDLIST (PIDL) structure that contains 79 /// the list of item identifiers that uniquely identifies the file within 80 /// the Shell's namespace. The pointer to an item identifier list (PIDL) 81 /// must be a fully qualified PIDL. Relative PIDLs are not allowed. 82 /// 83 /// If the uFlags parameter includes the SHGFI_USEFILEATTRIBUTES flag, 84 /// this parameter does not have to be a valid file name. The function 85 /// will proceed as if the file exists with the specified name and with 86 /// the file attributes passed in the dwFileAttributes parameter. This 87 /// allows you to obtain information about a file type by passing just 88 /// the extension for pszPath and passing FILE_ATTRIBUTE_NORMAL in 89 /// dwFileAttributes. 90 /// 91 /// This string can use either short (the 8.3 form) or long file names.</param> 92 /// <param name="fileAttributes">[in] A combination of one or more file 93 /// attribute flags (FILE_ATTRIBUTE_ values as defined in Winnt.h). If 94 /// uFlags does not include the SHGFI_USEFILEATTRIBUTES flag, this 95 /// parameter is ignored.</param> 96 /// <param name="psfi">[out] The address of a SHFILEINFO structure to 97 /// receive the file information.</param> 98 /// <param name="cbFileInfo">[in] The size, in bytes, of the SHFILEINFO 99 /// structure pointed to by the psfi parameter.</param> 100 /// <param name="uFlags">[in] The flags that specify the file information to retrieve. 101 /// This parameter can be a combination of the values in SHGetFileInfoFlags</param> 102 /// <returns>Returns a value whose meaning depends on the uFlags parameter. 103 /// 104 /// If uFlags does not contain SHGFI_EXETYPE or SHGFI_SYSICONINDEX, the return 105 /// value is nonzero if successful, or zero otherwise. 106 /// 107 /// If uFlags contains the SHGFI_EXETYPE flag, the return value specifies 108 /// the type of the executable file. It will be one of the following values. 109 /// 0 Nonexecutable file or an error condition. 110 /// LOWORD = NE or PE and HIWORD = Windows version Microsoft Windows application. 111 /// LOWORD = MZ and HIWORD = 0 Windows 95, Windows 98: Microsoft MS-DOS .exe, .com, or .bat file 112 /// Microsoft Windows NT, Windows 2000, Windows XP: MS-DOS .exe or .com file 113 /// LOWORD = PE and HIWORD = 0 Windows 95, Windows 98: Microsoft Win32 console application 114 /// Windows NT, Windows 2000, Windows XP: Win32 console application or .bat file 115 /// </returns> 116 [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] 117 public static extern IntPtr SHGetFileInfo(string path, uint fileAttributes, 118 ref SHFILEINFO psfi, int cbFileInfo, SHGetFileInfoFlags uFlags); 119 120 public enum SHGetFileInfoFlags 121 { 122 /// <summary> 123 /// Retrieve the handle to the icon that represents the file and the 124 /// index of the icon within the system image list. The handle is 125 /// copied to the hIcon member of the structure specified by psfi, 126 /// and the index is copied to the iIcon member. 127 /// </summary> 128 SHGFI_ICON = 0x000000100, 129 130 /// <summary> 131 /// Retrieve the display name for the file. The name is copied to the 132 /// szDisplayName member of the structure specified in psfi. The returned 133 /// display name uses the long file name, if there is one, rather than 134 /// the 8.3 form of the file name. 135 /// </summary> 136 SHGFI_DISPLAYNAME = 0x000000200, 137 138 /// <summary> 139 /// Retrieve the string that describes the file's type. The string 140 /// is copied to the szTypeName member of the structure specified in 141 /// psfi. 142 /// </summary> 143 SHGFI_TYPENAME = 0x000000400, 144 145 /// <summary> 146 /// Retrieve the item attributes. The attributes are copied to the 147 /// dwAttributes member of the structure specified in the psfi parameter. 148 /// These are the same attributes that are obtained from 149 /// IShellFolder::GetAttributesOf. 150 /// </summary> 151 SHGFI_ATTRIBUTES = 0x000000800, 152 153 /// <summary> 154 /// Retrieve the name of the file that contains the icon representing 155 /// the file specified by pszPath, as returned by the 156 /// IExtractIcon::GetIconLocation method of the file's icon handler. 157 /// Also retrieve the icon index within that file. The name of the 158 /// file containing the icon is copied to the szDisplayName member 159 /// of the structure specified by psfi. The icon's index is copied to 160 /// that structure's iIcon member. 161 /// </summary> 162 SHGFI_ICONLOCATION = 0x000001000, 163 164 /// <summary> 165 /// Retrieve the type of the executable file if pszPath identifies an 166 /// executable file. The information is packed into the return value. 167 /// This flag cannot be specified with any other flags. 168 /// </summary> 169 SHGFI_EXETYPE = 0x000002000, 170 171 /// <summary> 172 /// Retrieve the index of a system image list icon. If successful, 173 /// the index is copied to the iIcon member of psfi. The return value 174 /// is a handle to the system image list. Only those images whose 175 /// indices are successfully copied to iIcon are valid. Attempting 176 /// to access other images in the system image list will result in 177 /// undefined behavior. 178 /// </summary> 179 SHGFI_SYSICONINDEX = 0x000004000, 180 181 /// <summary> 182 /// Modify SHGFI_ICON, causing the function to add the link overlay 183 /// to the file's icon. The SHGFI_ICON flag must also be set. 184 /// </summary> 185 SHGFI_LINKOVERLAY = 0x000008000, 186 187 /// <summary> 188 /// Modify SHGFI_ICON, causing the function to blend the file's icon 189 /// with the system highlight color. The SHGFI_ICON flag must also 190 /// be set. 191 /// </summary> 192 SHGFI_SELECTED = 0x000010000, 193 194 /// <summary> 195 /// Modify SHGFI_ATTRIBUTES to indicate that the dwAttributes member 196 /// of the SHFILEINFO structure at psfi contains the specific attributes 197 /// that are desired. These attributes are passed to IShellFolder::GetAttributesOf. 198 /// If this flag is not specified, 0xFFFFFFFF is passed to 199 /// IShellFolder::GetAttributesOf, requesting all attributes. This flag 200 /// cannot be specified with the SHGFI_ICON flag. 201 /// </summary> 202 SHGFI_ATTR_SPECIFIED = 0x000020000, 203 204 /// <summary> 205 /// Modify SHGFI_ICON, causing the function to retrieve the file's 206 /// large icon. The SHGFI_ICON flag must also be set. 207 /// </summary> 208 SHGFI_LARGEICON = 0x000000000, 209 210 /// <summary> 211 /// Modify SHGFI_ICON, causing the function to retrieve the file's 212 /// small icon. Also used to modify SHGFI_SYSICONINDEX, causing the 213 /// function to return the handle to the system image list that 214 /// contains small icon images. The SHGFI_ICON and/or 215 /// SHGFI_SYSICONINDEX flag must also be set. 216 /// </summary> 217 SHGFI_SMALLICON = 0x000000001, 218 219 /// <summary> 220 /// Modify SHGFI_ICON, causing the function to retrieve the file's 221 /// open icon. Also used to modify SHGFI_SYSICONINDEX, causing the 222 /// function to return the handle to the system image list that 223 /// contains the file's small open icon. A container object displays 224 /// an open icon to indicate that the container is open. The SHGFI_ICON 225 /// and/or SHGFI_SYSICONINDEX flag must also be set. 226 /// </summary> 227 SHGFI_OPENICON = 0x000000002, 228 229 /// <summary> 230 /// Modify SHGFI_ICON, causing the function to retrieve a Shell-sized 231 /// icon. If this flag is not specified the function sizes the icon 232 /// according to the system metric values. The SHGFI_ICON flag must 233 /// also be set. 234 /// </summary> 235 SHGFI_SHELLICONSIZE = 0x000000004, 236 237 /// <summary> 238 /// Indicate that pszPath is the address of an ITEMIDLIST structure 239 /// rather than a path name. 240 /// </summary> 241 SHGFI_PIDL = 0x000000008, 242 243 /// <summary> 244 /// Indicates that the function should not attempt to access the file 245 /// specified by pszPath. Rather, it should act as if the file specified 246 /// by pszPath exists with the file attributes passed in dwFileAttributes. 247 /// This flag cannot be combined with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, 248 /// or SHGFI_PIDL flags. 249 /// </summary> 250 SHGFI_USEFILEATTRIBUTES = 0x000000010, 251 252 /// <summary> 253 /// Version 5.0. Apply the appropriate overlays to the file's icon. 254 /// The SHGFI_ICON flag must also be set. 255 /// </summary> 256 SHGFI_ADDOVERLAYS = 0x000000020, 257 258 /// <summary> 259 /// Version 5.0. Return the index of the overlay icon. The value of 260 /// the overlay index is returned in the upper eight bits of the iIcon 261 /// member of the structure specified by psfi. This flag requires that 262 /// the SHGFI_ICON be set as well. 263 /// </summary> 264 SHGFI_OVERLAYINDEX = 0x000000040 265 } 266 267 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 268 public struct SHFILEINFO 269 { 270 /// <summary> 271 /// A handle to the icon that represents the file. You are responsible 272 /// for destroying this handle with DestroyIcon when you no longer need it. 273 /// </summary> 274 public IntPtr hIcon; 275 276 /// <summary> 277 /// The index of the icon image within the system image list. 278 /// </summary> 279 public int iIcon; 280 281 /// <summary> 282 /// An array of values that indicates the attributes of the file object. 283 /// For information about these values, see the IShellFolder::GetAttributesOf 284 /// method. 285 /// </summary> 286 public uint dwAttributes; 287 288 /// <summary> 289 /// A string that contains the name of the file as it appears in the 290 /// Microsoft Windows Shell, or the path and file name of the file 291 /// that contains the icon representing the file. 292 /// </summary> 293 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 294 public string szDisplayName; 295 296 /// <summary> 297 /// A string that describes the type of file. 298 /// </summary> 299 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] 300 public string szTypeName; 79 /// <param name="path">[in] A pointer to a null-terminated string of maximum 80 /// length MAX_PATH that contains the path and file name. Both absolute 81 /// and relative paths are valid. 82 /// 83 /// If the uFlags parameter includes the SHGFI_PIDL flag, this parameter 84 /// must be the address of an ITEMIDLIST (PIDL) structure that contains 85 /// the list of item identifiers that uniquely identifies the file within 86 /// the Shell's namespace. The pointer to an item identifier list (PIDL) 87 /// must be a fully qualified PIDL. Relative PIDLs are not allowed. 88 /// 89 /// If the uFlags parameter includes the SHGFI_USEFILEATTRIBUTES flag, 90 /// this parameter does not have to be a valid file name. The function 91 /// will proceed as if the file exists with the specified name and with 92 /// the file attributes passed in the dwFileAttributes parameter. This 93 /// allows you to obtain information about a file type by passing just 94 /// the extension for pszPath and passing FILE_ATTRIBUTE_NORMAL in 95 /// dwFileAttributes. 96 /// 97 /// This string can use either short (the 8.3 form) or long file names.</param> 98 /// <param name="fileAttributes">[in] A combination of one or more file 99 /// attribute flags (FILE_ATTRIBUTE_ values as defined in Winnt.h). If 100 /// uFlags does not include the SHGFI_USEFILEATTRIBUTES flag, this 101 /// parameter is ignored.</param> 102 /// <param name="psfi">[out] The address of a SHFILEINFO structure to 103 /// receive the file information.</param> 104 /// <param name="cbFileInfo">[in] The size, in bytes, of the SHFILEINFO 105 /// structure pointed to by the psfi parameter.</param> 106 /// <param name="uFlags">[in] The flags that specify the file information 107 /// to retrieve. 108 /// This parameter can be a combination of the values in SHGetFileInfoFlags</param> 109 /// <returns>Returns a value whose meaning depends on the uFlags parameter. 110 /// 111 /// If uFlags does not contain SHGFI_EXETYPE or SHGFI_SYSICONINDEX, the return 112 /// value is nonzero if successful, or zero otherwise. 113 /// 114 /// If uFlags contains the SHGFI_EXETYPE flag, the return value specifies 115 /// the type of the executable file. It will be one of the following values. 116 /// 0 Nonexecutable file or an error condition. 117 /// LOWORD = NE or PE and HIWORD = Windows version Microsoft Windows application. 118 /// LOWORD = MZ and HIWORD = 0 Windows 95, Windows 98: Microsoft MS-DOS .exe, .com, or .bat file 119 /// Microsoft Windows NT, Windows 2000, Windows XP: MS-DOS .exe or .com file 120 /// LOWORD = PE and HIWORD = 0 Windows 95, Windows 98: Microsoft Win32 console application 121 /// Windows NT, Windows 2000, Windows XP: Win32 console application or .bat file 122 /// </returns> 123 [DllImport("Shell32.dll", CharSet = CharSet.Unicode)] 124 public static extern IntPtr SHGetFileInfo(string path, uint fileAttributes, 125 ref SHFILEINFO psfi, int cbFileInfo, SHGetFileInfoFlags uFlags); 126 127 public enum SHGetFileInfoFlags 128 { 129 /// <summary> 130 /// Retrieve the handle to the icon that represents the file and the 131 /// index of the icon within the system image list. The handle is 132 /// copied to the hIcon member of the structure specified by psfi, 133 /// and the index is copied to the iIcon member. 134 /// </summary> 135 SHGFI_ICON = 0x000000100, 136 137 /// <summary> 138 /// Retrieve the display name for the file. The name is copied to the 139 /// szDisplayName member of the structure specified in psfi. The returned 140 /// display name uses the long file name, if there is one, rather than 141 /// the 8.3 form of the file name. 142 /// </summary> 143 SHGFI_DISPLAYNAME = 0x000000200, 144 145 /// <summary> 146 /// Retrieve the string that describes the file's type. The string 147 /// is copied to the szTypeName member of the structure specified in 148 /// psfi. 149 /// </summary> 150 SHGFI_TYPENAME = 0x000000400, 151 152 /// <summary> 153 /// Retrieve the item attributes. The attributes are copied to the 154 /// dwAttributes member of the structure specified in the psfi parameter. 155 /// These are the same attributes that are obtained from 156 /// IShellFolder::GetAttributesOf. 157 /// </summary> 158 SHGFI_ATTRIBUTES = 0x000000800, 159 160 /// <summary> 161 /// Retrieve the name of the file that contains the icon representing 162 /// the file specified by pszPath, as returned by the 163 /// IExtractIcon::GetIconLocation method of the file's icon handler. 164 /// Also retrieve the icon index within that file. The name of the 165 /// file containing the icon is copied to the szDisplayName member 166 /// of the structure specified by psfi. The icon's index is copied to 167 /// that structure's iIcon member. 168 /// </summary> 169 SHGFI_ICONLOCATION = 0x000001000, 170 171 /// <summary> 172 /// Retrieve the type of the executable file if pszPath identifies an 173 /// executable file. The information is packed into the return value. 174 /// This flag cannot be specified with any other flags. 175 /// </summary> 176 SHGFI_EXETYPE = 0x000002000, 177 178 /// <summary> 179 /// Retrieve the index of a system image list icon. If successful, 180 /// the index is copied to the iIcon member of psfi. The return value 181 /// is a handle to the system image list. Only those images whose 182 /// indices are successfully copied to iIcon are valid. Attempting 183 /// to access other images in the system image list will result in 184 /// undefined behavior. 185 /// </summary> 186 SHGFI_SYSICONINDEX = 0x000004000, 187 188 /// <summary> 189 /// Modify SHGFI_ICON, causing the function to add the link overlay 190 /// to the file's icon. The SHGFI_ICON flag must also be set. 191 /// </summary> 192 SHGFI_LINKOVERLAY = 0x000008000, 193 194 /// <summary> 195 /// Modify SHGFI_ICON, causing the function to blend the file's icon 196 /// with the system highlight color. The SHGFI_ICON flag must also 197 /// be set. 198 /// </summary> 199 SHGFI_SELECTED = 0x000010000, 200 201 /// <summary> 202 /// Modify SHGFI_ATTRIBUTES to indicate that the dwAttributes member 203 /// of the SHFILEINFO structure at psfi contains the specific attributes 204 /// that are desired. These attributes are passed to IShellFolder::GetAttributesOf. 205 /// If this flag is not specified, 0xFFFFFFFF is passed to 206 /// IShellFolder::GetAttributesOf, requesting all attributes. This flag 207 /// cannot be specified with the SHGFI_ICON flag. 208 /// </summary> 209 SHGFI_ATTR_SPECIFIED = 0x000020000, 210 211 /// <summary> 212 /// Modify SHGFI_ICON, causing the function to retrieve the file's 213 /// large icon. The SHGFI_ICON flag must also be set. 214 /// </summary> 215 SHGFI_LARGEICON = 0x000000000, 216 217 /// <summary> 218 /// Modify SHGFI_ICON, causing the function to retrieve the file's 219 /// small icon. Also used to modify SHGFI_SYSICONINDEX, causing the 220 /// function to return the handle to the system image list that 221 /// contains small icon images. The SHGFI_ICON and/or 222 /// SHGFI_SYSICONINDEX flag must also be set. 223 /// </summary> 224 SHGFI_SMALLICON = 0x000000001, 225 226 /// <summary> 227 /// Modify SHGFI_ICON, causing the function to retrieve the file's 228 /// open icon. Also used to modify SHGFI_SYSICONINDEX, causing the 229 /// function to return the handle to the system image list that 230 /// contains the file's small open icon. A container object displays 231 /// an open icon to indicate that the container is open. The SHGFI_ICON 232 /// and/or SHGFI_SYSICONINDEX flag must also be set. 233 /// </summary> 234 SHGFI_OPENICON = 0x000000002, 235 236 /// <summary> 237 /// Modify SHGFI_ICON, causing the function to retrieve a Shell-sized 238 /// icon. If this flag is not specified the function sizes the icon 239 /// according to the system metric values. The SHGFI_ICON flag must 240 /// also be set. 241 /// </summary> 242 SHGFI_SHELLICONSIZE = 0x000000004, 243 244 /// <summary> 245 /// Indicate that pszPath is the address of an ITEMIDLIST structure 246 /// rather than a path name. 247 /// </summary> 248 SHGFI_PIDL = 0x000000008, 249 250 /// <summary> 251 /// Indicates that the function should not attempt to access the file 252 /// specified by pszPath. Rather, it should act as if the file specified 253 /// by pszPath exists with the file attributes passed in dwFileAttributes. 254 /// This flag cannot be combined with the SHGFI_ATTRIBUTES, SHGFI_EXETYPE, 255 /// or SHGFI_PIDL flags. 256 /// </summary> 257 SHGFI_USEFILEATTRIBUTES = 0x000000010, 258 259 /// <summary> 260 /// Version 5.0. Apply the appropriate overlays to the file's icon. 261 /// The SHGFI_ICON flag must also be set. 262 /// </summary> 263 SHGFI_ADDOVERLAYS = 0x000000020, 264 265 /// <summary> 266 /// Version 5.0. Return the index of the overlay icon. The value of 267 /// the overlay index is returned in the upper eight bits of the iIcon 268 /// member of the structure specified by psfi. This flag requires that 269 /// the SHGFI_ICON be set as well. 270 /// </summary> 271 SHGFI_OVERLAYINDEX = 0x000000040 272 } 273 274 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] 275 public struct SHFILEINFO 276 { 277 /// <summary> 278 /// A handle to the icon that represents the file. You are responsible 279 /// for destroying this handle with DestroyIcon when you no longer need it. 280 /// </summary> 281 public IntPtr hIcon; 282 283 /// <summary> 284 /// The index of the icon image within the system image list. 285 /// </summary> 286 public int iIcon; 287 288 /// <summary> 289 /// An array of values that indicates the attributes of the file object. 290 /// For information about these values, see the IShellFolder::GetAttributesOf 291 /// method. 292 /// </summary> 293 public uint dwAttributes; 294 295 /// <summary> 296 /// A string that contains the name of the file as it appears in the 297 /// Microsoft Windows Shell, or the path and file name of the file 298 /// that contains the icon representing the file. 299 /// </summary> 300 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] 301 public string szDisplayName; 302 303 /// <summary> 304 /// A string that describes the type of file. 305 /// </summary> 306 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)] 307 public string szTypeName; 308 } 301 309 } 302 310 } 311 312 [Flags] 313 public enum EmptyRecycleBinFlags : uint 314 { 315 /// <summary> 316 /// No flags specified. 317 /// </summary> 318 None = 0, 319 320 /// <summary> 321 /// No dialog box confirming the deletion of the objects will be displayed. 322 /// </summary> 323 NoConfirmation = ShellAPI.NativeMethods.SHEmptyRecycleBinFlags.SHERB_NOCONFIRMATION, 324 325 /// <summary> 326 /// No dialog box indicating the progress will be displayed. 327 /// </summary> 328 NoProgressUI = ShellAPI.NativeMethods.SHEmptyRecycleBinFlags.SHERB_NOPROGRESSUI, 329 330 /// <summary> 331 /// No sound will be played when the operation is complete. 332 /// </summary> 333 NoSound = ShellAPI.NativeMethods.SHEmptyRecycleBinFlags.SHERB_NOSOUND 334 } 303 335 } -
branches/eraser6/Util/UserAPI.cs
r461 r993 24 24 using System.Text; 25 25 using System.Runtime.InteropServices; 26 using System.Drawing; 26 27 27 28 namespace Eraser.Util … … 30 31 { 31 32 /// <summary> 32 /// The GetCapture function retrieves a handle to the window (if any) that 33 /// has captured the mouse. Only one window at a time can capture the mouse; 34 /// this window receives mouse input whether or not the cursor is within 35 /// its borders. 33 /// Gets the current position of the system caret. 36 34 /// </summary> 37 /// <returns></returns> 38 [DllImport("User32.dll")] 39 public static extern IntPtr GetCapture(); 35 public static Point CaretPos 36 { 37 get 38 { 39 Point result = new Point(); 40 if (NativeMethods.GetCaretPos(out result)) 41 return result; 42 return Point.Empty; 43 } 44 } 40 45 41 46 /// <summary> 42 /// The GetCaretPos function copies the caret's position to the specified 43 /// POINT structure. 47 /// Gets the cursor position fot the last message retrieved by GetMessage. 44 48 /// </summary> 45 /// <param name="lpPoint">[out] Pointer to the POINT structure that is to 46 /// receive the client coordinates of the caret.</param> 47 /// <returns>If the function succeeds, the return value is nonzero. 48 /// If the function fails, the return value is zero. To get extended error 49 /// information, call Marshal.GetLastWin32Error.</returns> 50 [DllImport("User32.dll", SetLastError = true)] 51 [return: MarshalAs(UnmanagedType.Bool)] 52 public static extern bool GetCaretPos(out POINT lpPoint); 49 public static uint MessagePos 50 { 51 get 52 { 53 return NativeMethods.GetMessagePos(); 54 } 55 } 53 56 54 57 /// <summary> 55 /// Retrieves the cursor's position, in screen coordinates.58 /// Gets the message time for the last message retrieved by GetMessage. 56 59 /// </summary> 57 /// <param name="lpPoint">[out] Pointer to a POINT structure that receives58 /// the screen coordinates of the cursor.</param>59 /// <returns>Returns nonzero if successful or zero otherwise. Toget60 /// extended error information, call GetLastError.</returns>61 [DllImport("User32.dll", SetLastError = true)]62 [return: MarshalAs(UnmanagedType.Bool)]63 public static extern bool GetCursorPos(out POINT lpPoint);60 public static int MessageTime 61 { 62 get 63 { 64 return NativeMethods.GetMessageTime(); 65 } 66 } 64 67 65 68 /// <summary> 66 /// The GetClipboardOwner function retrieves the window handle of the 67 /// current owner of the clipboard. 69 /// Classes, structs and constants imported from User32.dll 68 70 /// </summary> 69 /// <returns>If the function succeeds, the return value is the handle to 70 /// the window that owns the clipboard. 71 /// 72 /// If the clipboard is not owned, the return value is NULL. To get 73 /// extended error information, call Marshal.GetLastWin32Error.</returns> 74 [DllImport("User32.dll", SetLastError = true)] 75 public static extern IntPtr GetClipboardOwner(); 76 77 /// <summary> 78 /// The GetClipboardViewer function retrieves the handle to the first 79 /// window in the clipboard viewer chain. 80 /// </summary> 81 /// <returns>If the function succeeds, the return value is the handle to 82 /// the first window in the clipboard viewer chain. 83 /// 84 /// If there is no clipboard viewer, the return value is NULL. To get 85 /// extended error information, call Marshal.GetLastWin32Error. </returns> 86 [DllImport("User32.dll", SetLastError = true)] 87 public static extern IntPtr GetClipboardViewer(); 88 89 /// <summary> 90 /// The GetDesktopWindow function returns a handle to the desktop window. 91 /// The desktop window covers the entire screen. The desktop window is 92 /// the area on top of which other windows are painted. 93 /// </summary> 94 /// <returns>The return value is a handle to the desktop window.</returns> 95 [DllImport("User32.dll")] 96 public static extern IntPtr GetDesktopWindow(); 97 98 /// <summary> 99 /// The GetForegroundWindow function returns a handle to the foreground 100 /// window (the window with which the user is currently working). The 101 /// system assigns a slightly higher priority to the thread that creates 102 /// the foreground window than it does to other threads. 103 /// </summary> 104 /// <returns>The return value is a handle to the foreground window. The 105 /// foreground window can be NULL in certain circumstances, such as when 106 /// a window is losing activation.</returns> 107 [DllImport("User32.dll")] 108 public static extern IntPtr GetForegroundWindow(); 109 110 /// <summary> 111 /// The GetMessagePos function retrieves the cursor position for the 112 /// last message retrieved by the GetMessage function. 113 /// </summary> 114 /// <returns>The return value specifies the x- and y-coordinates of the 115 /// cursor position. The x-coordinate is the low order short and the 116 /// y-coordinate is the high-order short.</returns> 117 [DllImport("User32.dll")] 118 public static extern uint GetMessagePos(); 119 120 /// <summary> 121 /// The GetMessageTime function retrieves the message time for the last 122 /// message retrieved by the GetMessage function. The time is a long 123 /// integer that specifies the elapsed time, in milliseconds, from the 124 /// time the system was started to the time the message was created 125 /// (that is, placed in the thread's message queue). 126 /// </summary> 127 /// <returns>The return value specifies the message time.</returns> 128 [DllImport("User32.dll")] 129 public static extern int GetMessageTime(); 130 131 /// <summary> 132 /// The GetOpenClipboardWindow function retrieves the handle to the 133 /// window that currently has the clipboard open. 134 /// </summary> 135 /// <returns>If the function succeeds, the return value is the handle to 136 /// the window that has the clipboard open. If no window has the 137 /// clipboard open, the return value is NULL. To get extended error 138 /// information, call Marshal.GetLastWin32Error.</returns> 139 [DllImport("User32.dll", SetLastError = true)] 140 public static extern IntPtr GetOpenClipboardWindow(); 141 142 /// <summary> 143 /// Retrieves a handle to the current window station for the calling process. 144 /// </summary> 145 /// <returns>If the function succeeds, the return value is a handle to 146 /// the window station. 147 /// 148 /// If the function fails, the return value is NULL. To get extended error 149 /// information, call Marshal.GetLastWin32Error.</returns> 150 [DllImport("User32.dll", SetLastError = true)] 151 public static extern IntPtr GetProcessWindowStation(); 152 153 /// <summary> 154 /// The POINT structure defines the x- and y- coordinates of a point. 155 /// </summary> 156 public struct POINT 71 internal class NativeMethods 157 72 { 158 73 /// <summary> 159 /// Specifies the x-coordinate of the point. 74 /// The GetCaretPos function copies the caret's position to the specified 75 /// POINT structure. 160 76 /// </summary> 161 public int x; 77 /// <param name="lpPoint">[out] Pointer to the POINT structure that is to 78 /// receive the client coordinates of the caret.</param> 79 /// <returns>If the function succeeds, the return value is nonzero. 80 /// If the function fails, the return value is zero. To get extended error 81 /// information, call Marshal.GetLastWin32Error.</returns> 82 [DllImport("User32.dll", SetLastError = true)] 83 [return: MarshalAs(UnmanagedType.Bool)] 84 public static extern bool GetCaretPos(out Point lpPoint); 162 85 163 86 /// <summary> 164 /// Specifies the y-coordinate of the point. 87 /// The GetMessagePos function retrieves the cursor position for the 88 /// last message retrieved by the GetMessage function. 165 89 /// </summary> 166 public int y; 90 /// <returns>The return value specifies the x- and y-coordinates of the 91 /// cursor position. The x-coordinate is the low order short and the 92 /// y-coordinate is the high-order short.</returns> 93 [DllImport("User32.dll")] 94 public static extern uint GetMessagePos(); 95 96 /// <summary> 97 /// The GetMessageTime function retrieves the message time for the last 98 /// message retrieved by the GetMessage function. The time is a long 99 /// integer that specifies the elapsed time, in milliseconds, from the 100 /// time the system was started to the time the message was created 101 /// (that is, placed in the thread's message queue). 102 /// </summary> 103 /// <returns>The return value specifies the message time.</returns> 104 [DllImport("User32.dll")] 105 public static extern int GetMessageTime(); 167 106 } 168 107 }
Note: See TracChangeset
for help on using the changeset viewer.
