Using system; using system.runtime.interopservices;
Namespace fastcsharp {class class1 {
[Stathread] unsafe static void main (String [] args) {// Cominvoke (); // helloWorld (); // ClassInvoke (); // PointerInvoke (); unsafemethod ();
// Call a COM component // TLBIMP tool public static void cominvoke () {// string filename = "c: //windows/clock.avi"; string filename = @ "C: / documents and settings / administrator / desktop / WebApplication1 / heart Center Mi Zhou /dgmz.wma "; QuartzTypeLib.FilgraphManager graphManager = new QuartzTypeLib.FilgraphManagerClass (); QuartzTypeLib.IMediaControl mc = (QuartzTypeLib.IMediaControl) graphManager; mc.RenderFile (fileName); mc.Run (); Console.writeline ("Hello World"); console.readline ();} public static void helloworld () {Win32Invoke.MessageBox (0, "Hello World", "Platform Invoke Sample", 0);} // Transfer Structure Variable Public static void structinvoke () {Point P = new point (); px = 1; py = 1; R = 0; r.Right = 2; r.right = 2; r .top = 0; BOOL B = Win32invoke.ptinRect (Ref r, p); System.Console.Writeline ("Point [1,1] IS INT RECT [0, 0, 2, 2]?" b);} // Pass an object public static void classinvode () {mysystemTime t = new mysystemtime (); win32invoke.getsystemtime (t); console .Writeline ("System Time IS" T. Wyear "/" T.WMONTH "/" T. WDAY "" T.WHOUR ":" T.Wminute ": T. WSecond);
} // transmit a C # function pointer to a function Dll public static void PointerInvoke () {CallBack myCallBack = new CallBack (Class1.Report); Win32Invoke.EnumWindows (myCallBack, 0);} public static bool Report (int hwnd, int lParam) {Console.Writeline ("Window Handle IS" HWND); Return True;} // Pointer Function Unsafe Static Void SquareParam (INT * P) {// Unsafe Mthod: Takes Pointer To Int * P * = * P;} / / Unsafe method: user address-of operator (&) unsafe public static void unsafeMethod () {INT i = 5; SquareParam (& I); console.writeline ("5 * 5 ==" i); P P PT = New P (); Pt.x = 5; pt.y = 6; // fixed keyword is telling the C # resource recovery Next, do not calculate a variable address, do not change any changes to this variable // pin Pt in Place Fixed (INT * POINTER = & PT.X) {SquareParam (POINTER);} // Pt Now Nupinned Console.Writeline ("pt.x == {0} Pt.y == {1}", PT. x, pb.y);}} public class win32invoke {[DLLIMPORT ("User32.dll", charset = charset.auto)] Public Static Extern Int MessageBox (int hwnd, string text, string caption, uint type); [DLLIMPORT ("User32.dll")]] public static extern bool PtInRect (ref Rect r, Point p); [DllImport ( "Kernel32.dll")] public static extern void GetSystemTime (MySystemTime st); [DllImport ( "user32.dll")] public static extern int EnumWindows ( Callback x, int y);
public struct Point {public int x; public int y;} public class P {public int x; public int y;} [StructLayout (LayoutKind.Explicit)] public struct Rect {[FieldOffset (0)] public int left; [FieldOffset (4)] public int top; [FieldOffset (8)] public int right; [FieldOffset (12)] public int bottom;} [StructLayout (LayoutKind.Sequential)] public class MySystemTime {public ushort wYear; public ushort wMonth; public ushort wDayOfWeek; public ushort wDay; public ushort wHour; public ushort wMinute; public ushort wSecond; public ushort wMilliseconds;} // delegate public delegate bool CallBack (int hwnd, int lParam);}