using System;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
using System.Text;
namespace WinAPI
{
class Program
{
# P/Invoke
//[HandleProcessCorruptedStateExceptions] // Helps catch the System.AccessViolationException exception
static void Main(string[] args)
{
try
{
# Code
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
# P/Invoke
[DllImport("advapi32.dll", CharSet = CharSet.Ansi,SetLastError = true)]
private static extern bool GetUserNameA(StringBuilder lpBuffer,ref uint pcbBuffer);
# Code
StringBuilder username = new StringBuilder(300);
uint length = 300;
GetUserNameA(username,ref length);
Console.WriteLine(username);
# P/Invoke
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool GetComputerNameA(StringBuilder lpBuffer, ref ushort nSize);
# Code
bool success;
StringBuilder name = new StringBuilder(260);
ushort size = 260;
success = GetComputerNameA(name, ref size);
Console.WriteLine(name);