Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek"— Presentation transcript:

1 CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek pavel.jezek@d3s.mff.cuni.cz Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (http://www.msdnaa.net/curriculum/license_curriculum.aspx)

2 Calling a Function from a Win32 DLL Signature of the Win32 function int MessageBox (HWND hWnd, LPTSTR lpText, LPTSTR lpCaption, UINT uType); Invocation from C# using System; using System.Runtime.InteropServices; class Test { [DllImport("user32.dll")] static extern int MessageBox(uint hWnd, string text, string caption, uint type); // no body static void Main() { int res = MessageBox(0, "Isn't that cool?", "", 1); Console.WriteLine("res = " + res); }

3 DllImport Attribute [DllImport( dll.name [, EntryPoint=function-name] [, CharSet=charset-enum]// for string marshaling [, ExactSpelling= (true|false)]// should name be modified acc. to CharSet? [, SetLastError= (true|false)]// true: returns Win32 error info [, CallingConvention=callconv-enum] )] charset-enum:CharSet.Auto (default), CharSet.Ansi, CharSet.Unicode,... callconv-enum:CallingConvention.StdCall (default), CallingConvention.FastCall,...

4 Parameter Marshaling Primitive types of C# are automatically mapped to Win32 types. Standard mapping can be modified as follows: using System.Runtime.InteropServices;... [DllImport("...")] static extern void Foo ( [MarshalAs(UnmanagedType.LPStr)] string s, int x ); LPStrANSI string consisting of bytes LPWStrUnicode string LPTStrdefault: Windows XP/NT/2000 => Unicode, Windows 98 => ANSI

5 Passing Classes and Structs as Param. using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential, Pack=4)]// members are allocated sequentially class Time {// and are aligned at 4 byte boundaries public ushort year; public ushort month;... } [StructLayout(LayoutKind.Explicit)]// members are allocated at specified offsets struct S { [FieldOffset(0)] ushort x; [FieldOffset(2)] int y; } [DllImport("...")] static extern void Foo(Time t); [DllImport("...")] static extern void Foo1([MarshalAs(UnmanagedType.LPStruct)] Time t); [DllImport("...")] static extern void Bar(S s); Foo(new Time()); Bar(new S());

6 IDisposable public interface IDisposable { void Dispose() } public class ObjectDisposedException : InvalidOperationException {}

7 IDisposable class MyClass : IDisposable { private bool disposed = false; … public void DoSomeProcessing() { if (disposed) throw new ObjectDisposedException(); … } ~MyData() { Dispose(false); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!disposed) { if (disposing) { managedResource.Dispose(); } … // Release all native (unmanaged) resources (e.g. close Win32 handles) disposed = true; }


Download ppt "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek"

Similar presentations


Ads by Google