Advertisement
elibelash

Untitled

Dec 16th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.76 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace ConsoleApp
  9. {
  10.     class Program
  11.     {
  12.         class FunctionLoader
  13.         {
  14.             [DllImport("Kernel32.dll")]
  15.             private static extern IntPtr LoadLibrary(string path);
  16.  
  17.             [DllImport("Kernel32.dll")]
  18.             private static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
  19.  
  20.             public static T LoadFunction<T>(string dllPath, string functionName)
  21.             {
  22.                 var hModule = LoadLibrary(dllPath);
  23.                 var functionAddress = GetProcAddress(hModule, functionName);
  24.                 return (T) (object) Marshal.GetDelegateForFunctionPointer(functionAddress, typeof(T));
  25.             }
  26.         }
  27.  
  28.         private delegate void XRefDelegate(IntPtr pyobject);
  29.         private delegate IntPtr PyLong_FromLong(IntPtr pyobject);
  30.         private delegate IntPtr PyErr_Occurred();
  31.  
  32.         static void Main(string[] args)
  33.         {
  34.             //MessageBox = (MessageBoxDelegate)
  35.             var pydll = FindPythonDll(36);
  36.             FunctionLoader.LoadFunction<Action>(pydll, "Py_Initialize")();
  37.             var incr = FunctionLoader.LoadFunction<XRefDelegate>(pydll, "Py_IncRef");
  38.             var dcr = FunctionLoader.LoadFunction<XRefDelegate>(pydll, "Py_DecRef");
  39.  
  40.             var created_int_object = FunctionLoader.LoadFunction<PyLong_FromLong>(pydll, "PyLong_FromLong")((IntPtr) 1098080);
  41.  
  42.             var checkerFunc = FunctionLoader.LoadFunction<PyErr_Occurred>(pydll, "PyErr_Occurred");
  43.             void checkError() {
  44.                 if (checkerFunc() != IntPtr.Zero)
  45.                     throw new Exception();
  46.             }
  47.  
  48.             var a = Refcount(created_int_object);
  49.             checkError();
  50.             incr(created_int_object);
  51.             var b = Refcount(created_int_object);
  52.             Debug.Assert(a+1==b);
  53.             dcr(created_int_object);
  54.             b = Refcount(created_int_object);
  55.             Debug.Assert(a==b);
  56.         }
  57.  
  58.         internal static unsafe long Refcount(IntPtr op)
  59.         {
  60.             var p = (void*)op;
  61.             if ((void*)0 == p)
  62.             {
  63.                 return 0;
  64.             }
  65.             return !Environment.Is64BitProcess ? (*(int*)p) : (*(long*)p);
  66.         }
  67.  
  68.  
  69.         static string FindPythonDll(int version) {
  70.             return Environment.GetEnvironmentVariable("PATH")
  71.                 .Split(';')
  72.                 .Where(p => Regex.IsMatch(p, @"[Pp]ython\d\d[/\\]?$"))
  73.                 .SelectMany(p => Directory.GetFiles(p, "python*.dll").Where(pa => Regex.IsMatch(pa, $@"[Pp]ython{version}\.dll")))
  74.                 .Single();
  75.         }
  76.  
  77.  
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement