注册客户端提供程序程序集

备注

本文档适用于想要使用 System.Windows.Automation 命名空间中定义的托管 UI 自动化类的 .NET Framework 开发人员。 有关 UI 自动化的最新信息,请参阅 Windows 自动化 API:UI 自动化

本主题演示如何注册包含客户端 UI 自动化提供程序的 DLL。

示例

下面的示例演示如何注册包含控制台窗口提供程序的程序集。

using System;
using System.Windows.Automation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.IO;

namespace CSClient
{
    class CSClientProgram
    {
        [DllImport("kernel32.dll")]
        static extern IntPtr GetConsoleWindow();

        static void Main(string[] args)
        {
            // TODO  Change the path to the appropriate one for your CSProviderDLL.
            string fileloc = @"C:\SampleDependencies\CSProviderDLL.dll";
            Assembly a = null;
            try
            {
                a = Assembly.LoadFile(fileloc);
            }
            catch (FileNotFoundException e1)
            {
                Console.WriteLine(e1.Message);
            }
            if (a != null)
            {
                try
                {
                    ClientSettings.RegisterClientSideProviderAssembly(a.GetName());
                }
                catch (ProxyAssemblyNotLoadedException e)
                {
                    Console.WriteLine(e.Message);
                }

                IntPtr hwnd = GetConsoleWindow();

                // Get an AutomationElement that represents the window.
                AutomationElement elementWindow = AutomationElement.FromHandle(hwnd);
                Console.WriteLine("Found UI Automation client-side provider for:");

                // The name property is furnished by the client-side provider.
                Console.WriteLine(elementWindow.Current.Name);
                Console.WriteLine();
            }
            Console.WriteLine("Press any key to exit.");
            Console.ReadLine();
        }
    }
}

Imports System.Windows.Automation
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.IO


Namespace CSClient
    Friend Class CSClientProgram
        <DllImport("kernel32.dll")>
        Shared Function GetConsoleWindow() As IntPtr
        End Function

        Shared Sub Main(ByVal args() As String)
            ' TODO  Change the path to the appropriate one for your CSProviderDLL.
            Dim fileloc As String = "C:\SampleDependencies\CSProviderDLL.dll"
            Dim a As System.Reflection.Assembly = Nothing
            Try
                a = System.Reflection.Assembly.LoadFile(fileloc)
            Catch e1 As FileNotFoundException
                Console.WriteLine(e1.Message)

            End Try
            If a IsNot Nothing Then
                Try
                    ClientSettings.RegisterClientSideProviderAssembly(a.GetName())
                Catch e As ProxyAssemblyNotLoadedException
                    Console.WriteLine(e.Message)
                End Try

                Dim hwnd As IntPtr = GetConsoleWindow()

                ' Get an AutomationElement that represents the window. 
                Dim elementWindow As AutomationElement = AutomationElement.FromHandle(hwnd)
                Console.WriteLine("Found UI Automation client-side provider for:")

                ' The name property is furnished by the client-side provider.
                Console.WriteLine(elementWindow.Current.Name)
                Console.WriteLine()
            End If
            Console.WriteLine("Press any key to exit.")
            Console.ReadLine()
        End Sub
    End Class
End Namespace

请参阅