/* * Util.NativeMethods.cs * Authors: Adam Nabinger * Copyright (c) 2007-2008 Cornell University This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ using System; using System.Runtime.InteropServices; using System.Security; namespace Util { internal static class NativeMethods { // Device Info /// /// The GetDC function retrieves a handle to a device context (DC) for the client area of a specified window or for the entire screen. You can use the returned handle in subsequent GDI functions to draw in the DC. The device context is an opaque data structure, whose values are used internally by GDI. /// /// Handle to the window whose DC is to be retrieved. If this value is NULL, GetDC retrieves the DC for the entire screen. /// If the function succeeds, the return value is a handle to the DC for the specified window's client area. If the function fails, the return value is NULL. /// [DllImport("user32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern IntPtr GetDC(IntPtr hwnd); /// /// The ReleaseDC function releases a device context (DC), freeing it for use by other applications. The effect of the ReleaseDC function depends on the type of DC. It frees only common and window DCs. It has no effect on class or private DCs. /// /// Handle to the window whose DC is to be released. /// Handle to the DC to be released. /// The return value indicates whether the DC was released. If the DC was released, the return value is 1. If the DC was not released, the return value is zero. /// [DllImport("user32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern Int32 ReleaseDC(IntPtr hwnd, IntPtr hSDC); /// /// The GetDeviceCaps function retrieves device-specific information for the specified device. /// /// Handle to the DC. /// Specifies the item to return. /// The return value specifies the value of the desired item. /// [DllImport("gdi32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern Int32 GetDeviceCaps(IntPtr hdc, Int32 index); // Input Handler /// /// /// /// /// /// /// internal delegate IntPtr LowLevelProc(Int32 nCode, IntPtr wParam, IntPtr lParam); /// /// The SetWindowsHookEx function installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. These events are associated either with a specific thread or with all threads in the same desktop as the calling thread. /// /// Specifies the type of hook procedure to be installed. /// Pointer to the hook procedure. If the dwThreadId parameter is zero or specifies the identifier of a thread created by a different process, the lpfn parameter must point to a hook procedure in a DLL. Otherwise, lpfn can point to a hook procedure in the code associated with the current process. /// Handle to the DLL containing the hook procedure pointed to by the lpfn parameter. The hMod parameter must be set to NULL if the dwThreadId parameter specifies a thread created by the current process and if the hook procedure is within the code associated with the current process. /// Specifies the identifier of the thread with which the hook procedure is to be associated. If this parameter is zero, the hook procedure is associated with all existing threads running in the same desktop as the calling thread. /// If the function succeeds, the return value is the handle to the hook procedure. If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// [DllImport("user32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern IntPtr SetWindowsHookEx(Int32 idHook, LowLevelProc lpfn, IntPtr hMod, Int32 dwThreadId); /// /// The UnhookWindowsHookEx function removes a hook procedure installed in a hook chain by the SetWindowsHookEx function. /// /// Handle to the hook to be removed. This parameter is a hook handle obtained by a previous call to SetWindowsHookEx. /// [DllImport("user32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern void UnhookWindowsHookEx(IntPtr hhk); /// /// The CallNextHookEx function passes the hook information to the next hook procedure in the current hook chain. A hook procedure can call this function either before or after processing the hook information. /// /// Ignored. /// Specifies the hook code passed to the current hook procedure. The next hook procedure uses this code to determine how to process the hook information. /// Specifies the wParam value passed to the current hook procedure. The meaning of this parameter depends on the type of hook associated with the current hook chain. /// Specifies the lParam value passed to the current hook procedure. The meaning of this parameter depends on the type of hook associated with the current hook chain. /// This value is returned by the next hook procedure in the chain. The current hook procedure must also return this value. The meaning of the return value depends on the hook type. For more information, see the descriptions of the individual hook procedures. /// [DllImport("user32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern IntPtr CallNextHookEx(IntPtr hhk, Int32 nCode, IntPtr wParam, IntPtr lParam); /// /// Retrieves a module handle for the specified module. The module must have been loaded by the calling process. /// /// The name of the loaded module (either a .dll or .exe file). If the file name extension is omitted, the default library extension .dll is appended. The file name string can include a trailing point character (.) to indicate that the module name has no extension. The string does not have to specify a path. When specifying a path, be sure to use backslashes (\), not forward slashes (/). The name is compared (case independently) to the names of modules currently mapped into the address space of the calling process. If this parameter is NULL, GetModuleHandle returns a handle to the file used to create the calling process (.exe file). /// If the function succeeds, the return value is a handle to the specified module. If the function fails, the return value is NULL. To get extended error information, call GetLastError. /// [DllImport("kernel32.dll", CharSet = CharSet.Unicode), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern IntPtr GetModuleHandle(string lpModuleName); /// /// The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off—alternating each time the key is pressed). /// /// Specifies a virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code. If a non-English keyboard layout is used, virtual keys with values in the range ASCII A through Z and 0 through 9 are used to specify most of the character keys. For example, for the German keyboard layout, the virtual key of value ASCII O (0x4F) refers to the "o" key, whereas VK_OEM_1 refers to the "o with umlaut" key. /// The return value specifies the status of the specified virtual key, as follows: If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A key, such as the CAPS LOCK key, is toggled if it is turned on. The key is off and untoggled if the low-order bit is 0. A toggle key's indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled. /// [DllImport("user32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern short GetKeyState(Int32 nVirtKey); // Parallel Port /// /// /// /// /// [DllImport("inpout32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern void Out32(short port, byte data); /// /// /// /// /// [DllImport("inpout32.dll"), SuppressUnmanagedCodeSecurity, System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2118:ReviewSuppressUnmanagedCodeSecurityUsage")] internal static extern byte Inp32(short port); } }