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

Slides:



Advertisements
Similar presentations
P/Invoke Made Easy Wei-Chen Wang.
Advertisements

Based on Java Software Development, 5th Ed. By Lewis &Loftus
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
.NET.NET Interoperability Liran Ben Haim
P/Invoke Made Easy Wei-Chen Wang. Marshaling governs how data is passed between managed and unmanaged memory during platform invoke. Function Call Interop.
Threading and P/Invoke Tom Roeder CS fa. Finalization Recall C++ destructors: ~MyClass() { // cleanup } called when object is deleted does cleanup.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
Advanced .NET Programming I 13th Lecture
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 11 th Lecture Pavel Ježek
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
Basic Java Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
Introduction to C # – Part 2 Stephen Turner Software Design Engineer Microsoft UK.
Lecture 11 Dynamic link libraries. Differences between static libraries and DLLs In static library code is added to the executable. In DLL, the code is.
Software Engineering in Robotics Interfacing to external functions Henrik I. Christensen –
Advanced C# Eric Gunnerson Program Manager Visual C#.NET Microsoft Corporation.
Module 14: Attributes. Overview Overview of Attributes Defining Custom Attributes Retrieving Attribute Values.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 5 th Lecture Pavel Ježek
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
1.2 Primitive Data Types and Variables
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
Introduction to C# Anders Hejlsberg Distinguished Engineer Developer Division Microsoft Corporation.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 14 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 3 rd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 7 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 2 nd Lecture Pavel Ježek
Advanced .NET Programming I 11th Lecture
Advanced .NET Programming I 2nd Lecture
Examples of Classes & Objects
Advanced .NET Programming II 6th Lecture
Advanced .NET Programming II 4th Lecture
Advanced .NET Programming I 4th Lecture
Advanced .NET Programming I 7th Lecture
Advanced .NET Programming I 6th Lecture
Computing with C# and the .NET Framework
null, true, and false are also reserved.
An Introduction to Java – Part II
.NET and .NET Core 10. Enabling Contracts Pan Wuming 2017.
CIS 199 Final Review.
Advanced .NET Programming I 13th Lecture
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2018
Advanced .NET Programming I 8th Lecture
Advanced .NET Programming I 5th Lecture
C# Language & .NET Platform 10th Lecture
- This slide is intentionally left blank -
Advanced .NET Programming I 3rd Lecture
Advanced .NET Programming I 4th Lecture
C# Language & .NET Platform 11th Lecture
Advanced .NET Programming I 6th Lecture
C# Language & .NET Platform 3rd Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 4th Lecture
C# Language & .NET Platform 8th Lecture
C# Language & .NET Platform 12th Lecture
Presentation transcript:

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek 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 (

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); }

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,...

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

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());

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

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; }