C# - P/Invoke
List of exported functions in Windows system dll files: https://strontic.github.io/xcyclopedia/index-dll
Data Type conversion from C++ to .NET: https://www.codeproject.com/Articles/9714/Win32-API-C-to-NET
Boilerplate Code
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);
            }
        }
    }
}GetUserNameA
# 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);GetComputerNameA
# 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);Last updated
Was this helpful?