home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2005 November / PCWELT_11_2005.ISO / pcwsoft / Commandbar-Source.z.exe / ZCommon / Win32.cs < prev    next >
Encoding:
Text File  |  2002-06-10  |  12.1 KB  |  419 lines

  1. // Pavel Zolnikov[http://www.codeproject.com/script/profile/whos_who.asp?id=35980], 2002
  2.  
  3. using System;
  4. using System.Security.Permissions;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace ZCommon
  8. {
  9.     /// <summary>
  10.     /// Declarations of some Win32 API functions and structures.
  11.     /// </summary>
  12.     abstract public class Win32
  13.     {
  14.         [DllImport("kernel32.dll")]
  15.         public static extern Int32 GetCurrentThreadId();
  16.  
  17.         [DllImport("kernel32.dll")]
  18.         public static extern bool AllocConsole();
  19.  
  20.         [DllImport("kernel32.dll")]
  21.         public static extern bool SetConsoleCtrlHandler( ConsoleHandlerRoutine routine, bool add );
  22.         public delegate bool ConsoleHandlerRoutine(ConsoleEvents eventId);
  23.         public enum ConsoleEvents
  24.         {
  25.             CTRL_C_EVENT        =0,
  26.             CTRL_BREAK_EVENT    =1,
  27.             CTRL_CLOSE_EVENT    =2,
  28.             CTRL_LOGOFF_EVENT   =5,
  29.             CTRL_SHUTDOWN_EVENT =6
  30.         }
  31.  
  32.         [DllImport("kernel32.dll")]
  33.         public static extern bool FreeConsole();
  34.         
  35.         [DllImport("kernel32.dll")]
  36.         public static extern bool AttachConsole(int processId);
  37.  
  38.         [DllImport("kernel32.dll")]
  39.         public static extern IntPtr GetConsoleWindow();
  40.  
  41.         [DllImport("kernel32.dll")]
  42.         public static extern IntPtr GetStdHandle( Int32 nStdHandle );
  43.  
  44.         [DllImport("kernel32.dll")]
  45.         public static extern bool CloseHandle(IntPtr h);
  46.  
  47.         [DllImport("kernel32.dll")]
  48.         public static extern COORD GetLargestConsoleWindowSize();
  49.  
  50.         [DllImport("kernel32.dll")]
  51.         public static extern COORD GetConsoleFontSize( IntPtr hOut, Int32 index );
  52.  
  53.         [DllImport("kernel32.dll")]
  54.         public static extern bool GetCurrentConsoleFont( IntPtr hOut, bool bMaximumWnd, out CONSOLE_FONT_INFO ConsoleCurrentFont );
  55.  
  56.         [DllImport("kernel32.dll")]
  57.         public static extern bool SetConsoleActiveScreenBuffer( IntPtr hBuf );
  58.  
  59.         [DllImport("kernel32.dll")]
  60.         public static extern bool GetConsoleScreenBufferInfo( IntPtr hOut, out CONSOLE_SCREEN_BUFFER_INFO csbi);
  61.  
  62.         [DllImport("kernel32.dll",CharSet=CharSet.Unicode)]
  63.         public static extern bool WriteConsoleInput( 
  64.             IntPtr hIn,
  65.             [MarshalAs(UnmanagedType.LPStruct)]
  66.             KEY_INPUT_RECORD r,
  67.             Int32 count,
  68.             out Int32 countOut );
  69.  
  70.         [DllImport("kernel32.dll",CharSet=CharSet.Unicode)]
  71.         public static extern bool PeekConsoleInput(
  72.             IntPtr hConsoleInput,         
  73.             [Out]
  74.             [MarshalAs(UnmanagedType.LPStruct)]
  75.             FOCUS_INPUT_RECORD lpBuffer, 
  76.             Int32 nLength,                
  77.             out Int32 lpNumberOfEventsRead
  78.             );
  79.  
  80.         [DllImport("kernel32.dll")]
  81.         public static extern bool GetNumberOfConsoleInputEvents( IntPtr hIn, out Int32 num);
  82.  
  83.         [DllImport("user32.dll")]
  84.         public static extern IntPtr SetFocus( IntPtr hwnd );
  85.  
  86.         [DllImport("user32.dll")]
  87.         public static extern IntPtr GetFocus();
  88.  
  89.         [CLSCompliant(false)]
  90.         [DllImport("kernel32.dll")]
  91.         public static extern IntPtr CreateConsoleScreenBuffer( 
  92.             DESIRED_ACCESS dwDesiredAccess,                            
  93.             FILE_SHARE dwShareMode,                                
  94.  
  95.             [MarshalAs(UnmanagedType.LPStruct)]
  96.             SECURITY_ATTRIBUTES lpSecurityAttributes,
  97.             Int32 dwFlags,                                    
  98.             IntPtr lpScreenBufferData 
  99.             );
  100.  
  101.         [DllImport("kernel32.dll")]
  102.         public static extern bool SetConsoleCursorPosition( IntPtr hOut, COORD newPos );
  103.  
  104.         [DllImport("kernel32.dll",SetLastError=true)]
  105.         public static extern bool SetConsoleScreenBufferSize( IntPtr hOut, COORD newSize );
  106.  
  107.         [DllImport("kernel32.dll")]
  108.         public static extern bool WriteConsole(
  109.             IntPtr hConsoleOutput,          
  110.             String lpBuffer,           
  111.             Int32 nNumberOfCharsToWrite,     
  112.             out Int32 lpNumberOfCharsWritten, 
  113.             IntPtr lpReserved               
  114.             );
  115.  
  116.         [DllImport("kernel32.dll",CharSet=CharSet.Unicode)]
  117.         public static extern bool CreateProcessW(
  118.             IntPtr    lpApplicationName,   
  119.             IntPtr    lpCommandLine,       
  120.             IntPtr    lpProcessAttributes, 
  121.             IntPtr    lpThreadAttributes,  
  122.             Int32    bInheritHandles,       
  123.             Int32    dwCreationFlags,      
  124.             IntPtr    lpEnvironment,       
  125.             IntPtr    lpCurrentDirectory,
  126.             [MarshalAs(UnmanagedType.LPStruct)][In]
  127.             STARTUPINFO lpStartupInfo,       
  128.             IntPtr    lpProcessInformation 
  129.             );
  130.  
  131.         [DllImport("kernel32.dll",CharSet=CharSet.Unicode)]
  132.         public static extern bool CreateProcessW(
  133.             String    applicationName,   
  134.             String    commandLine,       
  135.             IntPtr    lpProcessAttributes, 
  136.             IntPtr    lpThreadAttributes,  
  137.             bool    bInheritHandles,       
  138.             Int32    dwCreationFlags,      
  139.             IntPtr    lpEnvironment,       
  140.             String    lpCurrentDirectory,
  141.             [MarshalAs(UnmanagedType.LPStruct)][In]
  142.             STARTUPINFO lpStartupInfo,       
  143.             [MarshalAs(UnmanagedType.LPStruct)][In]
  144.             PROCESS_INFORMATION    lpProcessInformation 
  145.             );
  146.  
  147.         [DllImport("user32.dll")]
  148.         public static extern bool IsWindow( IntPtr hwnd);
  149.  
  150.         [DllImport("user32.dll")]
  151.         public static extern bool MoveWindow(IntPtr hwnd, Int32 x, Int32 y, Int32 h, Int32 w, bool repaint);
  152.  
  153.         [DllImport("user32.dll", SetLastError=true)]
  154.         public static extern IntPtr SetParent( IntPtr child, IntPtr newParent );
  155.  
  156.         [DllImport("user32.dll")]
  157.         public static extern bool GetWindowInfo( IntPtr hwnd, out WINDOWINFO wi);
  158.  
  159.         [DllImport("user32.dll")]
  160.         public static extern bool PostMessage( IntPtr hwnd, Int32 msg, Int32 wparam, Int32 lparam);
  161.  
  162.         [DllImport("user32.dll")]
  163.         public static extern IntPtr SendMessage( IntPtr hwnd, Int32 msg, Int32 wparam, Int32 lparam);
  164.  
  165.         [DllImport("user32.dll")]
  166.         public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
  167.  
  168.         [DllImport("user32.dll")]
  169.         public static extern bool RemoveMenu( IntPtr hMenu, Int32 nPosition, Int32 nFlags );
  170.  
  171.         [DllImport("user32.dll")]
  172.         public static extern Int32 GetMenuItemCount( IntPtr hMenu );
  173.  
  174.         [DllImport("user32.dll",CharSet=CharSet.Unicode)]
  175.         public static extern bool GetMenuItemInfo( 
  176.             IntPtr hMenu,
  177.             Int32 item,
  178.             bool bByPosition,
  179.  
  180.             [MarshalAs(UnmanagedType.LPStruct)]
  181.             [In][Out]
  182.             MENUITEMINFO mii 
  183.             );
  184.  
  185.         [DllImport("user32.dll")]
  186.         public static extern bool ShowWindow( IntPtr hWnd, Int32 nCmdShow );
  187.  
  188.         [DllImport("user32.dll")]
  189.         public static extern bool  SetWindowPos(
  190.             IntPtr hWnd,             
  191.             IntPtr hWndInsertAfter,  
  192.             Int32 X,                 
  193.             Int32 Y,                 
  194.             Int32 cx,                
  195.             Int32 cy,                
  196.             Int32 uFlags            
  197.             );
  198.  
  199.  
  200.         
  201.         [DllImport("kernel32.dll")]//, CharSet=CharSet.Unicode
  202.         public static extern IntPtr GetProcAddress( IntPtr hmod, String name );
  203.  
  204.         [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
  205.         public static extern IntPtr GetModuleHandle( String lpModuleName );
  206.  
  207.         [DllImport("kernel32.dll", CharSet=CharSet.Unicode)]
  208.         public static extern bool WriteProcessMemory(
  209.             IntPtr hProcess,                    // handle to process
  210.             IntPtr lpBaseAddress,                // base    of memory area
  211.             IntPtr lpBuffer,                    // data buffer
  212.             Int32 nSize,                        // count of    bytes to write
  213.             out    Int32 lpNumberOfBytesWritten    // count of bytes    written
  214.             );
  215.  
  216.         [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
  217.             public class MENUITEMINFO 
  218.         {
  219.             public readonly Int32    cbSize = Marshal.SizeOf(typeof(Win32.MENUITEMINFO)); 
  220.             public MIIM                fMask; 
  221.             public Int32            fType; 
  222.             public Int32            fState; 
  223.             public Int32            wID; 
  224.             public IntPtr            hSubMenu; 
  225.             public IntPtr            hbmpChecked; 
  226.             public IntPtr            hbmpUnchecked; 
  227.             public IntPtr            dwItemData; 
  228.  
  229.             [MarshalAs(UnmanagedType.LPWStr, SizeConst=255)]
  230.             public String            dwTypeData = new String(' ',256); 
  231.             public readonly Int32    cch = 255;
  232.             public IntPtr            hbmpItem;
  233.         }
  234.  
  235.         public struct RECT
  236.         {
  237.             public Int32    left;
  238.             public Int32    top;
  239.             public Int32    right;
  240.             public Int32    bottom;
  241.         }
  242.  
  243.         public struct WINDOWINFO 
  244.         {
  245.             public Int32        cbSize;
  246.             public RECT            rcWindow;
  247.             public RECT            rcClient;
  248.             public Int32        dwStyle;
  249.             public Int32        dwExStyle;
  250.             public Int32        dwWindowStatus;
  251.             public Int32        cxWindowBorders;
  252.             public Int32        cyWindowBorders;
  253.             public Int16        atomWindowType;
  254.             public Int16        wCreatorVersion;
  255.         }
  256.  
  257.         [StructLayout(LayoutKind.Sequential)]
  258.             public class SECURITY_ATTRIBUTES
  259.         {
  260.             public readonly Int32    nLength = Marshal.SizeOf(typeof(Win32.SECURITY_ATTRIBUTES));
  261.             public IntPtr            lpSecurityDescriptor; 
  262.             public bool                bInheritHandle; 
  263.         }
  264.  
  265.         public struct COORD 
  266.         { 
  267.             public Int16 X; 
  268.             public Int16 Y; 
  269.         }
  270.  
  271.         public struct CONSOLE_FONT_INFO 
  272.         {
  273.             public Int32  nFont;
  274.             public COORD  dwFontSize;
  275.         }
  276.         public struct SMALL_RECT
  277.         {
  278.             public Int16 Left; 
  279.             public Int16 Top; 
  280.             public Int16 Right; 
  281.             public Int16 Bottom; 
  282.         }
  283.  
  284.         public struct CONSOLE_SCREEN_BUFFER_INFO 
  285.         { 
  286.             public COORD      dwSize; 
  287.             public COORD      dwCursorPosition; 
  288.             public Int16      wAttributes; 
  289.             public SMALL_RECT srWindow; 
  290.             public Int16      dwMaximumWindowSize; 
  291.         }
  292.  
  293.         
  294.  
  295.         /// <summary>
  296.         /// Result of expanding KeyEvent field inside INPUT_RECORD structure.
  297.         /// </summary>
  298.         /// <remarks>
  299.         /// Here is INPUT_RECORD definition :
  300.         ///    <code>    typedef struct _INPUT_RECORD 
  301.         ///                { 
  302.         ///                    WORD EventType; 
  303.         ///                    union 
  304.         ///                    { 
  305.         ///                    KEY_EVENT_RECORD KeyEvent; 
  306.         ///                    MOUSE_EVENT_RECORD MouseEvent; 
  307.         ///                    WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent; 
  308.         ///                    MENU_EVENT_RECORD MenuEvent; 
  309.         ///                    FOCUS_EVENT_RECORD FocusEvent; 
  310.         ///                } Event; 
  311.         ///        } INPUT_RECORD; </code>
  312.         /// We have to do this expanding to overcome lack of union{} support in C#.
  313.         /// </remarks>
  314.         [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
  315.             public class KEY_INPUT_RECORD 
  316.         {
  317.             public Int16 EventType = KEY_EVENT;//this is only one of several possible cases of the INPUT_RECORD structure
  318.  
  319.             //the rest of fields are from KEY_EVENT_RECORD :
  320.             public bool        bKeyDown;
  321.             public Int16    wRepeatCount;
  322.             public Int16    wVirtualKeyCode;
  323.             public Int16    wVirtualScanCode;
  324.             public char        UnicodeChar;
  325.             public Int32    dwControlKeyState;
  326.         }
  327.  
  328.         [StructLayout(LayoutKind.Sequential)]
  329.         public class PROCESS_INFORMATION 
  330.         { 
  331.             public IntPtr hProcess; 
  332.             public IntPtr hThread; 
  333.             public Int32 dwProcessId; 
  334.             public Int32 dwThreadId; 
  335.         } 
  336.  
  337.  
  338.         [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
  339.         public class STARTUPINFO 
  340.         { 
  341.             public Int32    cb = Marshal.SizeOf(typeof(STARTUPINFO));    
  342.             public IntPtr    lpReserved;    
  343.             public IntPtr    lpDesktop; 
  344.             public IntPtr    lpTitle; 
  345.             public Int32    dwX; 
  346.             public Int32    dwY; 
  347.             public Int32    dwXSize; 
  348.             public Int32    dwYSize; 
  349.             public Int32    dwXCountChars; 
  350.             public Int32    dwYCountChars; 
  351.             public Int32    dwFillAttribute; 
  352.             public Int32    dwFlags; 
  353.             public Int16    wShowWindow; 
  354.             public Int16    cbReserved2; 
  355.             public IntPtr    lpReserved2; 
  356.             public IntPtr    hStdInput; 
  357.             public IntPtr    hStdOutput;    
  358.             public IntPtr    hStdError; 
  359.         }
  360.  
  361.         [StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
  362.             public class FOCUS_INPUT_RECORD
  363.         {
  364.             public Int16 EventType;
  365.             public bool bSetFocus;
  366.             [MarshalAs(UnmanagedType.ByValTStr, SizeConst=255)]
  367.             public String        wszTitle = new String(' ',256);
  368.         }
  369.         
  370.         public const Int32 CONSOLE_TEXTMODE_BUFFER        = 1;
  371.         public const Int32 KEY_EVENT                    = 0x0001;
  372.         public const Int32 FOCUS_EVENT                    = 0x0010; 
  373.         public const Int32 SW_SHOW                        = 5;
  374.         public const Int32 WM_KEYDOWN                    = 0x100;
  375.         public const Int32 WM_COMMAND                    = 0x112;
  376.         public const Int32 WM_CLOSE                        = 0x0010;
  377.         public const int CREATE_NEW_CONSOLE                = 0x00000010;
  378.  
  379.  
  380.         [Flags]
  381.         public enum MIIM : int
  382.         {
  383.             STATE      = 0x00000001,
  384.             ID         = 0x00000002,
  385.             SUBMENU    = 0x00000004,
  386.             CHECKMARKS = 0x00000008,
  387.             TYPE       = 0x00000010,
  388.             DATA       = 0x00000020,
  389.             STRING     = 0x00000040,
  390.             BITMAP     = 0x00000080,
  391.             FTYPE      = 0x00000100
  392.         }
  393.         
  394.  
  395.         [Flags]
  396.         [CLSCompliant(false)]
  397.         public enum DESIRED_ACCESS : uint 
  398.         {
  399.             GENERIC_READ        =            0x80000000,
  400.             GENERIC_WRITE       =            0x40000000,
  401.             GENERIC_EXECUTE     =            0x20000000,
  402.             GENERIC_ALL            =            0x10000000
  403.         }
  404.  
  405.         public enum FILE_SHARE : int
  406.         {
  407.             READ     =           0x00000001,
  408.             WRITE    =           0x00000002, 
  409.             DELETE   =           0x00000004 
  410.         }
  411.  
  412.         public enum STD_HANDLE
  413.         {
  414.             INPUT    = -10,
  415.             OUTPUT    = -11,
  416.             ERROR    = -12
  417.         }
  418.     }
  419. }