2555A_05

  • Upload
    myxro

  • View
    220

  • Download
    0

Embed Size (px)

Citation preview

  • 8/13/2019 2555A_05

    1/16

    Module 5:Interoperating with

    Managed Objects

  • 8/13/2019 2555A_05

    2/16

    Overview

    Using .NET and COM Components in a Windows FormsApplication

    Calling Win32 APIs from Windows Forms Applications

  • 8/13/2019 2555A_05

    3/16

    Lesson: Using .NET and COM Components in aWindows Forms Application

    COM vs .NET

    How to Call COM Components from .NET

    Role of the RCW How to Generate Interop Assemblies

    Private, Shared, and Primary Interop Assemblies

    Practice: Using COM Components in .NET-BasedApplications

  • 8/13/2019 2555A_05

    4/16

    COM vs .NET

    COM Model .NET Framework Model

    Interface based Object based

    GUIDs Strong names

    Binary standard Type standard

    Type library Metadata

    HResults Exceptions

    Reference counting Garbage collection

    Immutable Resilient

  • 8/13/2019 2555A_05

    5/16

    How to Call COM Components from .NET

    The CLR enables managed code to call COM objectsthrough a proxy called the Runtime Callable Wrapper(RCW)

    The RCW is a managed object and subject to garbagecollection

    COM Object A

    COM Object B

    RCW

    RCW

    .NET Client

    .NET Client

    Unmanaged Managed

    http://localhost/var/www/apps/conversion/tmp/scratch_9/2555A_05A005.htm
  • 8/13/2019 2555A_05

    6/16

    Role of the RCW

    Maintains object lifetime

    Marshals method calls between managed andunmanaged code

    Consumes selected COM interfaces without exposing

    them to the .NET client

    COM

    Object RCW.NET

    Client

    Unmanaged Managed

    INew

    IUnknown

    IDispatch

    INew

  • 8/13/2019 2555A_05

    7/16

    How to Generate Interop Assemblies

    Unmanaged Managed

    Interop

    Assembly

    A

    B

    C

    D

    Tlbimp.exe

    A B

    C D

    COM object

    COM object

  • 8/13/2019 2555A_05

    8/16

    Private, Shared, and Primary Interop Assemblies

    Application1.exe

    Metadata

    MSIL

    C:\Application1

    Productlib.dll

    Metadata

    Application2.exe

    Metadata

    MSIL

    C:\Application2

    Productlib.dll

    MetadataPrivate Interop

    Assemblies

    Productlib.dll

    MetadataGAC

    Strong Named

    Assemblies

    Primary Interop Assembly is an assembly signed by thecompany that produced the original type library

  • 8/13/2019 2555A_05

    9/16

    Practice: Using COM Components in .NET-BasedApplications

    In this practice, you will

    Register the COM Component

    Add reference to the COM component

    Add code to use the COM component in a.NET-based application

    Begin reviewing the objectives

    for this practice activity15 min

  • 8/13/2019 2555A_05

    10/16

    Lesson: Calling Win32 APIs from Windows FormsApplications

    Platform Invocation Services

    How to Define Functions by Using the DllImportAttribute

    How to Call Win32 APIs from a Windows FormsApplication

    Practice: Calling Win32 APIs

  • 8/13/2019 2555A_05

    11/16

    Platform Invocation Services

    A platform invoke call to an unmanaged DLL function

    The PInvoke Model

    Unmanaged Managed

    Standard

    marshaling service

    DLL

    DLL

    function

    Platform

    invoke

    Common languageruntime

    Assembly

    Metadata

    IL code

    CompilerManaged

    source

    code

  • 8/13/2019 2555A_05

    12/16

    How to Define Functions by Using the DllImportAttribute

    DllImport attribute is used to define functions

    Parameters are used to specify specific behavior

    CallingConvention

    CharSet

    [DllImport("KERNEL32.DLL", EntryPoint="MoveFileW",SetLastError=true,

    CharSet=CharSet.Unicode, ExactSpelling=true,

    CallingConvention=CallingConvention.StdCall)]

    public static extern bool MoveFile(String src, Stringdst);

    EntryPoint

    ExactSpelling

  • 8/13/2019 2555A_05

    13/16

    How to Call Win32 APIs from a Windows FormsApplication

    Create a new project in Visual Studio .NET

    Import the System.Runtime.InteropServices namespace

    using System.Runtime.InteropServices;

    Define a function using DllImport

    Add code to call the Win32 API from a Windows Form

    bool result;System.IntPtr resourceHandle = System.IntPtr.Zero;result =

    Win32PlaySoundClass.PlaySound_DllImport(soundFileName,resourceHandle,0);

    Determine the details of the function you want to call

  • 8/13/2019 2555A_05

    14/16

    Practice: Calling Win32 APIs

    Begin reviewing the objectives

    for this practice activity15 min

    In this practice, you will call a Win32 API froma .NET-based application using the DllImport

    attribute

  • 8/13/2019 2555A_05

    15/16

    Review

    Using .NET and COM Components in a WindowsForms Application

    Calling Win32 APIs from Windows Forms Applications

  • 8/13/2019 2555A_05

    16/16

    Lab 5.1: Interoperating with COM and Calling Win32APIs

    Exercise 1: Using a COM Component in a.NET-Based Application

    Exercise 2:Calling Win32 APIs from a.NET-Based Application