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[];
// Hack Hack Hack
// Too lazy to figure out the array at the moment...
//public COLORREF[16] ColorTable;
public COLORREF color0;
public COLORREF color1;
public COLORREF color2;
public COLORREF color3;
public static CONSOLE_SCREEN_BUFFER_INFO_EX Create()
{
return new CONSOLE_SCREEN_BUFFER_INFO_EX { cbSize = 96 };
}
}
public COLORREF color4;
public COLORREF color5;
public COLORREF color6;
public COLORREF color7;
//[StructLayout(LayoutKind.Sequential)]
//struct COLORREF
//{
// public byte R;
// public byte G;
// public byte B;
//}
public COLORREF color8;
public COLORREF color9;
public COLORREF colorA;
public COLORREF colorB;
[StructLayout(LayoutKind.Sequential)]
public struct COLORREF
{
public uint ColorDWORD;
public void SetColor(System.Drawing.Color color)
{
ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
}
public COLORREF colorC;
public COLORREF colorD;
public COLORREF colorE;
public COLORREF colorF;
}
//[StructLayout(LayoutKind.Sequential)]
//struct COLORREF
//{
// public byte R;
// public byte G;
// public byte B;
//}
[StructLayout(LayoutKind.Sequential)]
public struct CONSOLE_FONT_INFO
public struct COLORREF
{
public int nFont;
public COORD dwFontSize;
}
public uint ColorDWORD;
[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 struct CONSOLE_FONT_INFO
{
public COORD dwMousePosition;
public uint dwButtonState;
public uint dwControlKeyState;
public uint dwEventFlags;
public int nFont;
public COORD dwFontSize;
}
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 unsafe struct CONSOLE_FONT_INFO_EX
{
public uint dwCommandId;
}
public uint cbSize;
public uint nFont;
public COORD dwFontSize;
public ushort FontFamily;
public ushort FontWeight;
fixed char FaceName[LF_FACESIZE];
[StructLayout(LayoutKind.Sequential)]
public struct FOCUS_EVENT_RECORD
{
public uint bSetFocus;
}
const uint LF_FACESIZE = 32;
}
//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
public struct INPUT_RECORD
{
[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;
}
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.Sequential)]
public struct CONSOLE_HISTORY_INFO
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct KEY_EVENT_RECORD
{
ushort cbSize;
ushort HistoryBufferSize;
ushort NumberOfHistoryBuffers;
uint dwFlags;
[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 CONSOLE_SELECTION_INFO
public struct MOUSE_EVENT_RECORD
{
uint Flags;
COORD SelectionAnchor;
SMALL_RECT Selection;
// 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
public COORD dwMousePosition;
public uint dwButtonState;
public uint dwControlKeyState;
public uint dwEventFlags;
}
// Enumerated type for the control messages sent to the handler routine
enum CtrlTypes : uint
public struct WINDOW_BUFFER_SIZE_RECORD
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
}
}
public COORD dwSize;
public WINDOW_BUFFER_SIZE_RECORD(short x, short y)
{
dwSize = new COORD();
dwSize.X = x;
dwSize.Y = y;
}
}
VB Signature:
Please add some!
User-Defined Types:
None.
[StructLayout(LayoutKind.Sequential)]
public struct MENU_EVENT_RECORD
{
public uint dwCommandId;
}
Alternative Managed API:
Do you know one? Please contribute it!
[StructLayout(LayoutKind.Sequential)]
public struct FOCUS_EVENT_RECORD
{
public uint bSetFocus;
}
Notes:
None.
//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:
Declare Function ConsoleFunctions Lib "kernel32.dll" (TODO) As TODO
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).