Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than kernel32, prefix the name with the module name and a period.
ConsoleFunctions (kernel32)
.
The Console Functions from MSDN gathered in one class.
Warning!
This has not been tested and is provided for convenience as a roll-up of the individual console functions here on pinvoke.net into one location.
C# Signature:
using System;
using System.Text;
using System.Runtime.InteropServices;
// HISTORY
///
// Version 0.1: wherin PInvoke.net snippets are copied in, and missing functions coded and submitted to PInvoke.net
// 11/21/2012 Correcting signature for GetConsoleScreenBufferInfoEx and cleaned up CONSOLE_SCREEN_BUFFER_INFO_EX.ColorTable
namespace ConsoleClassLibrary
{
/// <summary>
///
/// --- begin MSDN ---
/// http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx
/// Console Functions
/// The following functions are used to access a console.
/// --- end MSDN ---
///
/// </summary>
class ConsoleFunctions
{
// http://pinvoke.net/default.aspx/kernel32/AddConsoleAlias.html
[DllImport("kernel32", SetLastError = true)]
static extern bool AddConsoleAlias(
string Source,
string Target,
string ExeName
);
// http://pinvoke.net/default.aspx/kernel32/GetConsoleProcessList.html
// TODO: Test - what's an out uint[] during interop? This probably isn't quite right, but provides a starting point:
[DllImport("kernel32.dll", SetLastError = true)]
static extern uint GetConsoleProcessList(
out uint[] ProcessList,
uint ProcessCount
);
// http://pinvoke.net/default.aspx/kernel32/HandlerRoutine.html
// Delegate type to be used as the Handler Routine for SCCH
delegate bool ConsoleCtrlDelegate(CtrlTypes CtrlType);
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
}
public struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}
public struct CONSOLE_SCREEN_BUFFER_INFO
{
public COORD dwSize;
public COORD dwCursorPosition;
public short wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_SCREEN_BUFFER_INFO_EX
{
public uint cbSize;
public COORD dwSize;
public COORD dwCursorPosition;
public short wAttributes;
public SMALL_RECT srWindow;
public COORD dwMaximumWindowSize;
public ushort wPopupAttributes;
public bool bFullscreenSupported;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public COLORREF ColorTable[];
public static CONSOLE_SCREEN_BUFFER_INFO_EX Create()
{
return new CONSOLE_SCREEN_BUFFER_INFO_EX { cbSize = 96 };
}
}
//[StructLayout(LayoutKind.Sequential)]
//struct COLORREF
//{
// public byte R;
// public byte G;
// public byte B;
//}
[StructLayout(LayoutKind.Sequential)]
public struct COLORREF
{
public uint ColorDWORD;
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_FONT_INFO
{
public int nFont;
public COORD dwFontSize;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct CONSOLE_FONT_INFO_EX
{
public uint cbSize;
public uint nFont;
public COORD dwFontSize;
public ushort FontFamily;
public ushort FontWeight;
fixed char FaceName[LF_FACESIZE];
const uint LF_FACESIZE = 32;
}
[StructLayout(LayoutKind.Explicit)]
public struct INPUT_RECORD
{
[FieldOffset(0)]
public ushort EventType;
[FieldOffset(4)]
public KEY_EVENT_RECORD KeyEvent;
[FieldOffset(4)]
public MOUSE_EVENT_RECORD MouseEvent;
[FieldOffset(4)]
public WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
[FieldOffset(4)]
public MENU_EVENT_RECORD MenuEvent;
[FieldOffset(4)]
public FOCUS_EVENT_RECORD FocusEvent;
};
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct KEY_EVENT_RECORD
{
[FieldOffset(0), MarshalAs(UnmanagedType.Bool)]
public bool bKeyDown;
[FieldOffset(4), MarshalAs(UnmanagedType.U2)]
public ushort wRepeatCount;
[FieldOffset(6), MarshalAs(UnmanagedType.U2)]
//public VirtualKeys wVirtualKeyCode;
public ushort wVirtualKeyCode;
[FieldOffset(8), MarshalAs(UnmanagedType.U2)]
public ushort wVirtualScanCode;
[FieldOffset(10)]
public char UnicodeChar;
[FieldOffset(12), MarshalAs(UnmanagedType.U4)]
//public ControlKeyState dwControlKeyState;
public uint dwControlKeyState;
}
[StructLayout(LayoutKind.Sequential)]
public struct MOUSE_EVENT_RECORD
{
public COORD dwMousePosition;
public uint dwButtonState;
public uint dwControlKeyState;
public uint dwEventFlags;
}
public struct WINDOW_BUFFER_SIZE_RECORD
{
public COORD dwSize;
public WINDOW_BUFFER_SIZE_RECORD(short x, short y)
{
dwSize = new COORD();
dwSize.X = x;
dwSize.Y = y;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct MENU_EVENT_RECORD
{
public uint dwCommandId;
}
[StructLayout(LayoutKind.Sequential)]
public struct FOCUS_EVENT_RECORD
{
public uint bSetFocus;
}
//CHAR_INFO struct, which was a union in the old days
// so we want to use LayoutKind.Explicit to mimic it as closely
// as we can
[StructLayout(LayoutKind.Explicit)]
public struct CHAR_INFO
{
[FieldOffset(0)]
char UnicodeChar;
[FieldOffset(0)]
char AsciiChar;
[FieldOffset(2)] //2 bytes seems to work properly
UInt16 Attributes;
}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_CURSOR_INFO
{
uint Size;
bool Visible;
}
// Flags values:
const uint CONSOLE_MOUSE_DOWN = 0x0008; // Mouse is down
const uint CONSOLE_MOUSE_SELECTION = 0x0004; //Selecting with the mouse
const uint CONSOLE_NO_SELECTION = 0x0000; //No selection
const uint CONSOLE_SELECTION_IN_PROGRESS = 0x0001; //Selection has begun
const uint CONSOLE_SELECTION_NOT_EMPTY = 0x0002; //Selection rectangle is not empty
}
// Enumerated type for the control messages sent to the handler routine
enum CtrlTypes : uint
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
}
}
VB Signature:
Please add some!
User-Defined Types:
None.
Alternative Managed API:
Do you know one? Please contribute it!
Notes:
None.
Tips & Tricks:
Please add some!
Sample Code:
Please add some!
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).