Skip to content

Instantly share code, notes, and snippets.

@vmeln
Created February 14, 2014 14:20
Show Gist options
  • Save vmeln/9001758 to your computer and use it in GitHub Desktop.
Save vmeln/9001758 to your computer and use it in GitHub Desktop.
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace TestingConsoleProject
{
public class Entry
{
public const int STD_OUTPUT_HANDLE = -11;
[DllImport("kernel32.dll", SetLastError=true, CallingConvention=CallingConvention.StdCall, CharSet=CharSet.Auto)]
public static extern IntPtr GetStdHandle(int handle);
[DllImport("kernel32.dll", CharSet = CharSet.None, ExactSpelling = false, SetLastError = true)]
internal static extern int WriteFile(IntPtr handle, ref byte bytes, int numBytesToWrite, out int numBytesWritten, IntPtr mustBeZero);
public static void Main()
{
Console.WriteLine(PInvokeWriteLine("Hello world"));
}
public static int PInvokeWriteLine(string message)
{
if (!message.EndsWith(Environment.NewLine))
{
message += Environment.NewLine;
}
byte[] messageBytes = Encoding.ASCII.GetBytes(message);
var handle = GetStdHandle(STD_OUTPUT_HANDLE);
int charsWritten = 0;
WriteFile(handle, ref messageBytes[0], messageBytes.Length, out charsWritten, IntPtr.Zero);
return charsWritten;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment