Notes
  • 👀About me
  • â„šī¸Good Reads
  • 🌐Web
    • Web Pentesting Checklist
    • Insecure Deserialization
    • Blind XPath Injection
    • GraphQL
    • Reverse Shells
      • IIS
    • Content-Security-Policy
      • XSS (Static Nonce in CSP)
    • LLM (Large Language Models)
  • 📘Windows API
    • C# - P/Invoke
  • ☕Miscellaneous Topics
    • Phishing with Gophish
    • Pentest Diaries
      • SQL Queries via Grafana
      • LDAP Pass Back Attack
      • Misconfigured File Upload to RCE
  • 🧃Hack The Box
    • Intelligence
    • Seal
    • Under Construction
    • Previse
    • Return
    • Sauna
    • Nest
  • 📕TryHackMe
    • Wordpress CVE-2021-29447
    • Attacktiv
    • Fortress
    • internal
  • đŸ› ī¸Cheatsheet
    • Anti-Forensic Techniques
    • JSON - jq
    • Docker
    • Hidden Secrets
    • Database Exploitation
      • PostgreSQL
        • Blind SQLi script
      • SQL Server
    • C Sharp
    • Reversing
      • Windows
    • SSH
    • Python
      • Miscellaneous Scripts
        • Credential Bruteforcing a CLI service
    • Privilege Escalation
      • Windows
    • socat
    • OSINT
      • Shodan
    • Installation
Powered by GitBook
On this page

Was this helpful?

  1. Windows API

C# - P/Invoke

PreviousLLM (Large Language Models)NextPhishing with Gophish

Last updated 1 year ago

Was this helpful?

List of exported functions in Windows system dll files:

Data Type conversion from 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);
📘
https://strontic.github.io/xcyclopedia/index-dll
https://www.codeproject.com/Articles/9714/Win32-API-C-to-NET