Share via


使用會話 Moniker

會話對會話啟用可讓用戶端進程在指定的會話上啟用本機伺服器進程。 您可以使用系統提供的會話 Moniker,以每個會話為基礎執行這項操作。 如需建立會話 Moniker 的詳細資訊,請參閱 會話 Moniker 的會話對會話啟用

下列範例示範如何在會話識別碼為 3 的會話上啟用類別識別碼為 「100000013-0000-0000-0000000000001」 的本機伺服器進程。

首先,此範例會呼叫 CoInitialize 函式來初始化 COM 程式庫。 然後範例會呼叫 CreateBindCtx ,以擷取 IBindCtx 介面實作的指標。 此物件會儲存 Moniker 系結作業的相關資訊;需要指標才能呼叫 IMoniker 介面的方法。 接下來,範例會呼叫 MkParseDisplayNameEx 函式來建立複合會話 Moniker,然後使用新建立的會話 Moniker,啟動用戶端與伺服器進程之間的連線 IMoniker::BindToObject 方法。 此時,您可以使用 介面指標在 物件上執行所需的作業。 最後,範例會釋放系結內容,並呼叫 CoUninitialize 函式

// Initialize COM.

HRESULT hr = CoInitialize(NULL);
if (FAILED(hr)) exit(0);  // Handle errors here.

// Get interface pBindCtx.

IBindCtx* pBindCtx;
hr = CreateBindCtx(NULL, &pBindCtx);
if (FAILED(hr)) exit(0);  // Handle errors here.

// Get moniker pMoniker.

OLECHAR string[] =
    L"Session:3!clsid:10000013-0000-0000-0000-000000000001";
ULONG ulParsed;
IMoniker* pMoniker;
hr = MkParseDisplayNameEx( pBindCtx,
                           string,
                           &ulParsed,
                           &pMoniker
                          );
if (FAILED(hr)) exit(0);  // Handle errors here.

// Get object factory pSessionTestFactory.

IUnknown* pSessionTestFactory;
hr = pMoniker->BindToObject( pBindCtx,
                             NULL,
                             IID_IUnknown,
                             (void**)&pSessionTestFactory
                            );
if (FAILED(hr)) exit(0);  // Handle errors here.

//
// Make, use, and destroy object here.
//
pSessionTestFactory->Release();
pSessionTestFactory = NULL;

pMoniker->Release();  // Release moniker.

pBindCtx->Release();  // Release interface.

CoUninitialize();  // Release COM.

因為 「{class id of the class moniker}」 也是命名類別 Moniker 的方法,所以您可以使用下列字串來命名複合 Moniker, (以類別 Moniker 撰寫的會話 Moniker) ,而不是上述範例所示的方式。

OLECHAR string[] = 
    L"Session:3!{0000031A-0000-0000-C000-000000000046}:
    10000013-0000-0000-0000-000000000001";

注意

如果在跨會話啟用期間登入每個會話,您可以成功啟動設定為在 RunAs 互動式使用者啟用模式中執行的任何伺服器進程。 如果不同的使用者登入每個會話,伺服器必須呼叫 CoInitializeSecurity 函式,才能在用戶端與伺服器之間成功啟用和連線之前設定適當的使用者權限。 若要達成此目的,其中一個方法是讓伺服器實作自訂 IAccessControl 介面,並將實作傳遞至 CoInitializeSecurity。 在任何情況下,用戶端使用者都必須具有伺服器上執行之應用程式所指定的適當 啟動存取權限 。 如需詳細資訊,請參閱 COM 中的安全性

 

如需系統提供 Moniker 和 Moniker 和啟用模式的詳細資訊,請參閱 Platform Software Development Kit (SDK) 的 COM 檔中的 MonikersIMoniker 介面和 AppId 金鑰