Bahasa

Marshal.GetActiveObject(String) Metode

Definisi

Mendapatkan instans yang sedang berjalan dari objek yang ditentukan dari tabel objek yang sedang berjalan (ROT).

public:
 static System::Object ^ GetActiveObject(System::String ^ progID);
public static object GetActiveObject(string progID);
[System.Security.SecurityCritical]
public static object GetActiveObject(string progID);
static member GetActiveObject : string -> obj
[<System.Security.SecurityCritical>]
static member GetActiveObject : string -> obj
Public Shared Function GetActiveObject (progID As String) As Object

Parameter

progID
String

Pengidentifikasi terprogram (ProgID) objek yang diminta.

Mengembalikan

Objek yang diminta; jika tidak null. Anda dapat melemparkan objek ini ke antarmuka COM apa pun yang didukungnya.

Atribut

Pengecualian

Objek tidak ditemukan.

Contoh

Contoh berikut dijalankan di komputer yang dikonfigurasi dengan instans Microsoft Word yang sedang berjalan. Tidak ada instans Microsoft Excel berjalan.

Contoh memanggil GetActiveObject dua kali. Panggilan pertama mencoba mengambil referensi ke instans Microsoft Word (instans objek Word.Application). Panggilan kedua mencoba mengambil referensi ke instans Microsoft Excel (instans objek Excel.Application).

Kode mengambil referensi ke instans Microsoft Word berhasil. Namun, karena Microsoft Excel tidak berjalan, upaya untuk mengambil objek kedua menimbulkan COMException.

using System;
using System.Runtime.InteropServices;

class MainFunction
{
    static void Main()
        {
        Console.WriteLine("\nSample: C# System.Runtime.InteropServices.Marshal.GetActiveObject.cs\n"); 

        GetObj(1, "Word.Application");
        GetObj(2, "Excel.Application");
        }

    static void GetObj(int i, String progID)
    {
        Object obj = null;

        Console.WriteLine("\n" +i+") Object obj = GetActiveObject(\"" + progID + "\")");
        try
           { obj = Marshal.GetActiveObject(progID); }
        catch (Exception e)
           {
           Write2Console("\n   Failure: obj did not get initialized\n" + 
                         "   Exception = " +e.ToString().Substring(0,43), 0); 
           }
 
        if (obj != null)
           { Write2Console("\n   Success: obj = " + obj.ToString(), 1 ); }
    }

    static void Write2Console(String s, int color)
        {
        Console.ForegroundColor = color == 1? ConsoleColor.Green : ConsoleColor.Red;
        Console.WriteLine(s); 
        Console.ForegroundColor = ConsoleColor.Gray;
    }
}

/*
Expected Output:

Sample: C# System.Runtime.InteropServices.Marshal.GetActiveObject.cs

1) Object obj = GetActiveObject("Word.Application")

   Success: obj = System.__ComObject

2) Object obj = GetActiveObject("Excel.Application")

   Failure: obj did not get initialized
   Exception = System.Runtime.InteropServices.COMException
*/

Imports System.Runtime.InteropServices

Module Module1

    Sub Main()
        Console.WriteLine(vbcrlf + "Sample: VB System.Runtime.InteropServices.Marshal.GetActiveObject.vb" + vbcrlf) 
        GetObj(1, "Word.Application")
        GetObj(2, "Excel.Application")
    End Sub


    Sub GetObj(ByVal i As Integer, ByVal progID As [String])
        Dim obj As [Object] = Nothing
        
        Console.WriteLine((vbLf & i & ") Object obj = GetActiveObject(""") + progID & """)")
        Try
            obj = Marshal.GetActiveObject(progID)
        Catch e As Exception
            Write2Console((vbLf & "   Failure: obj did not get initialized" & vbLf & "   Exception = ") + e.ToString().Substring(0, 43), 0)
        End Try
        
        If obj IsNot Nothing Then
            Write2Console(vbLf & "   Success: obj = " & obj.ToString(), 1)
        End If
    End Sub

    Sub Write2Console(ByVal s As [String], ByVal color As Integer)
        Console.ForegroundColor = If(color = 1, ConsoleColor.Green, ConsoleColor.Red)
        Console.WriteLine(s)
        Console.ForegroundColor = ConsoleColor.Gray
    End Sub

End Module

'Expected Output:
'
'Sample: VB System.Runtime.InteropServices.Marshal.GetActiveObject.vb
'
'1) Object obj = GetActiveObject("Word.Application")
'
'   Success: obj = System.__ComObject
'
'2) Object obj = GetActiveObject("Excel.Application")
'
'   Failure: obj did not get initialized
'   Exception = System.Runtime.InteropServices.COMException
'

Keterangan

GetActiveObject mengekspos fungsi COM GetActiveObject dari OLEAUT32.DLL; namun, yang terakhir mengharapkan pengidentifikasi kelas (CLSID) alih-alih pengidentifikasi terprogram (ProgID) yang diharapkan oleh metode ini. Untuk mendapatkan instans objek COM yang sedang berjalan tanpa terdaftar ProgID, gunakan Platform Invoke untuk mendefinisikan fungsi COM GetActiveObject. Untuk deskripsi pemanggilan platform, lihat Mengonsumsi Fungsi DLL Tidak Terkelola.

ProgID dan CLSID

Kunci dalam subtree HKEY_CLASSES_ROOT registri berisi berbagai jenis subkunci. Sebagian besar subkunci adalah ProgID, yang memetakan string ramah pengguna ke CLSID. Aplikasi sering menggunakan string yang dapat dibaca manusia ini alih-alih CLSID numerik. Seringkali, komponen memiliki ProgID independen versi yang dipetakan ke versi terbaru komponen yang diinstal pada sistem.

Aplikasi dan komponen terutama menggunakan ProgID untuk mengambil CLSID yang sesuai.

Berlaku untuk