Skip to content

Instantly share code, notes, and snippets.

@rkttu
Last active June 6, 2023 09:19
Show Gist options
  • Save rkttu/359f6a42f0c720f182de2064bb7b20ac to your computer and use it in GitHub Desktop.
Save rkttu/359f6a42f0c720f182de2064bb7b20ac to your computer and use it in GitHub Desktop.
P/Invoke with PowerShell (and small portion of C# code)
# Windows uses msvcrt.dll
Add-Type -Path .\test.cs
[Sample]::printf_win32("Hello, World %d!`r`n", 123)
# *nix uses libc
Add-Type -Path ./test.cs
[Sample]::printf_linux("Hello, World %d!`n", 123)
using System;
using System.Runtime.InteropServices;
public static class Sample {
[DllImport("msvcrt.dll", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.Cdecl,
SetLastError = false, ExactSpelling = true, EntryPoint = "printf")]
public static extern int printf_win32(string fmt, int arg0);
[DllImport("libc", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.Cdecl,
SetLastError = false, ExactSpelling = true, EntryPoint = "printf")]
public static extern int printf_linux(string fmt, int arg0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment