DCOM会话名字对象

时间:2013-12-19 02:38:58

标签: windows session process dcom opc

代码在我的OPC客户端中,我想激活DCOM OPC服务器。我的目的是在会话1中激活OPC服务器,但它仍然在会话0中工作,我的意思是OPC服务器进程仍然在会话0中运行。因为OPC服务器依赖于会话1中的某些其他进程,所以它可以在会话0中有效。任何人都可以帮助我吗?它困扰了我好几天。

IBindCtx* pBindCtx;
HRESULT hr = CreateBindCtx(NULL, &pBindCtx);

BIND_OPTS2 stBindOpt;
ZeroMemory(&stBindOpt,sizeof(BIND_OPTS2));
stBindOpt.cbStruct = sizeof(BIND_OPTS2);
hr = pBindCtx->GetBindOptions(&stBindOpt);
stBindOpt.dwClassContext = CLSCTX_REMOTE_SERVER;
stBindOpt.grfFlags = BIND_JUSTTESTEXISTENCE;
stBindOpt.pServerInfo = &tCoServerInfo;
hr  = pBindCtx->SetBindOptions(&stBindOpt);

if (FAILED(hr)) exit(0);  // Handle errors here.

WCHAR wszCLSID[64];
StringFromGUID2(clsid, wszCLSID, 64);
CString strCLSID = wszCLSID;
strCLSID.Replace(_T("{"), _T(""));
strCLSID.Replace(_T("}"), _T(""));

CString strMoniker;
strMoniker.Format(_T("Session:1!clsid:%s"), strCLSID);
CLogger::Instance().WriteLog(_T("Moniker String:") + strMoniker);

nSize = strMoniker.GetLength() * sizeof(WCHAR);
LPWSTR wstrMoniker = new WCHAR[nSize];
// Copy the machine name string into the server info structure:
#ifdef _UNICODE
lstrcpyn(wstrMoniker, strMoniker, nSize);
#else 
mbstowcs(wstrMoniker, strMoniker, nSize);
#endif//_UNICODE

ULONG ulParsed;
IMoniker* pMoniker;
hr = MkParseDisplayNameEx(pBindCtx,
wstrMoniker,
&ulParsed,
&pMoniker
);
if (SUCCEEDED(hr))
{
IUnknown* pSessionTestFactory;
hr = pMoniker->BindToObject(pBindCtx,
            NULL,
            IID_IUnknown,
            (void**)&pSessionTestFactory
            );
if (SUCCEEDED(hr))
{
    pMoniker->AddRef();
}

pSessionTestFactory->Release();
}

pMoniker->Release();
delete [] wstrMoniker;

0 个答案:

没有答案