using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace UnmanagedCode { public class User32 { [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options); } } """ # Bytecode, no disk forl to LoadLibrary from assembly = Generate(unmanaged_code, 'UnmanagedCode', inMemory=True) print("Type : %s" % (type(assembly))) print("CodeBase: %s" % (assembly.CodeBase)) print("FullName: %s" % (assembly.FullName)) print("Dynamic? %s" % (assembly.IsDynamic)) print("Location: %s" % (assembly.Location)) clr.AddReference(assembly) from UnmanagedCode import User32 from System import IntPtr User32.MessageBox(IntPtr.Zero, "Hello World", "Platform Invoke Sample", 0)
return compile.CompiledAssembly return compile.PathToAssembly unmanaged_code = """ using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace UnmanagedCode { public class User32 { [DllImport("user32.dll", CharSet=CharSet.Auto)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options); } } """ # Bytecode, no disk forl to LoadLibrary from assembly = Generate(unmanaged_code, 'UnmanagedCode', inMemory=True) print(assembly) clr.AddReference(assembly) from UnmanagedCode import User32 User32.MessageBox(0, "Hello World", "Platform Invoke Sample", 0)