p invoke and cil
play

P/Invoke and CIL functions in COM objects, C/C++ DLLs, etc. E.g. - PDF document

2/15/2008 P/Invoke Allows managed code to call unmanaged P/Invoke and CIL functions in COM objects, C/C++ DLLs, etc. E.g. access to Win32 API Mingsheng Hong To declare unmanaged functions CS 215, Spring 2008 Use DllImport


  1. 2/15/2008 P/Invoke  Allows managed code to call unmanaged P/Invoke and CIL functions in COM objects, C/C++ DLLs, etc.  E.g. access to Win32 API Mingsheng Hong  To declare unmanaged functions CS 215, Spring 2008  Use DllImport attribute and static extern [DllImport(“kernel32.dll”)] static extern int GetProcessHeap(); P/Invoke Example Steps in P/Invoke using System.Runtime.InteropServices;  Locates implementing DLL namespace HelloWorld { class MyClass {  Loads DLL into memory [DllImport (“user32.dll”, CharSet=CharSet.Ansi)] static extern int MessageBox(int hwnd,  Finds function address string msg, string caption, int t);  Pushes arguments on stack, marshalling data public static void Main() {  Transfers control to unmanaged code MessageBox(0, "Hello World!", "Caption", 0); } } } P/Invoke Callbacks Callback Example public class SampleClass {  Unmanaged code can call back to managed delegate bool CallBack(int hwnd, int lParam); code Unmanaged parameter is function pointer [DllImport("user32.dll")]  static extern int EnumWindows(CallBack x, int lParam); In managed code, must supply parameter as delegate  P/Invoke creates callback thunk  // report the window handle  Passes address of thunk as callback parameter public bool Report(int hwnd, int lParam) { Console.Write(“Window handle is ” + hwnd); Unmanaged Code Managed Code return true; } .NET Application DLL Call passes pointer to public static void Main() { DLL function callback function CallBack myCallBack = new CallBack(Report); EnumWindows(myCallBack, 0); Implementation of callback } function } 1

  2. 2/15/2008 C++/CLI CIL  Write managed C++ code  Recall: two stage compilation  compile with /clr  C# compiler: C# code  CIL code  Just-in-time (JIT) compiler: CIL  native code  generates CIL from C++  new key words  Common Intermediate Language  __gc, __box, __typeof, __interface, __property  very close to C# (and other OO languages)  #pragma managed/unmanaged  define classes, structs, inheritance, methods  Very useful for native access to C++ libraries  assembly-like statements  build a “managed wrapper” CIL Hello World Example .assembly extern mscorlib {} //automatically added  Stack language .assembly hello {} Compile with  instead of registers, everything is from stack ilasm /exe /debug hello.il .class Program { .method static public void Main() cil managed {  main operations take operands from stack .entrypoint //designates this method as the entry pt  e.g. .locals init (string name) //create a local var int i = 137; 138 137 1 ldstr “World" //load the string onto eval stack 137 int j = 1; stloc.0 //store the string into the first local var int k = i + j; ldstr "Hello, {0}!" ldloc name //load local var onto eval stack call void [mscorlib] System.Console::WriteLine( string, object) //call method with stack items as params ret } } CIL Directives Load/Store Operations  .assembly  ldloc / stloc  pushes contents of local var (or index) onto stack  . class  pops and stores in local var (or index)  define any type  ldc  extends: extend some other type  ldc.i4 50000  if extend System.ValueType , then value type  ldc.i4.1  . method, .field, .property, .event  ldc.i4.m1  .locals: names and types for local vars  ldnull  . entrypoint  ldfld / stfld  .maxstack  ldsfld int32 A::fielda 2

  3. 2/15/2008 Load/Store Example CIL Operations .locals init ([1] int32 a, [0] int32 b)  Integer operations ldc.i4.5  add, mul, sub, div, rem, neg stloc.0 ldc.i4 10  Boxing stloc.1  removes the value type from stack ldloc a  creates an object on the managed heap that call void [mscorlib] boxes the value type System.Console::WriteLine(int32)  places a reference to the newly created object ldloc b back on the evaluation stack call void [mscorlib]  e.g. ldc.i4.3 System.Console::WriteLine(int32) box int32  Data type conversions: conv.* CIL Operations Control Flow Operations  ceq/cgt/clt  Construct objects  newobj instance void A::.ctor()  pop top two elements of stack  check =, >, <  Invoke functions call void [mscorlib]  push true or false onto stack  System.Console::WriteLine(string, object)  br/beq/bne/bgt/blt/brfalse/brtrue call instance int32 A::F()  callvirt instance int32 A::G()  do the comparison and jump   use to implement structured control flow Control Flow Example CIL Tools ldc.i4.3 CIL ldc.i4.1 binary cgt brtrue greater C# C# compiler ILDASM ILASM Native code ldstr "{0} is less than or equal {1}" code br end CIL textual greater: ldstr "{0} is greater than {1}" end: ldc.i4.3  Roundtripping box int32 ldc.i4.1  ildasm /out=new_program.il program.exe box int32  Edit the CIL code in new_program.il call void [mscorlib]  ilasm newadd.il System.Console::WriteLine(string, object, object) 3

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend