Created
February 14, 2014 14:20
-
-
Save vmeln/9001758 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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