SHARE
TWEET

Untitled

a guest Oct 21st, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Runtime.InteropServices;
  3.  
  4. namespace PInvokeTest
  5. {
  6.     class Program
  7.     {
  8.         [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi, SetLastError = false)]
  9.         public delegate int printf_int(string format, int arg0);
  10.  
  11.         static void Main(string[] args)
  12.         {
  13.             var lib = NativeLibrary.Load("msvcrt.dll");
  14.             var ptr = NativeLibrary.GetExport(lib, "printf");
  15.             var func = Marshal.GetDelegateForFunctionPointer<printf_int>(ptr);
  16.             var len = func.Invoke("Hello, World! %d\n", 123);
  17.             Console.WriteLine($"printf returns {len}");
  18.             NativeLibrary.Free(lib);
  19.             Console.WriteLine("Hello World!");
  20.         }
  21.     }
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top