用户名不同的计算机之间的调用dcom

https://blog.csdn.net/embededvc/article/details/18863535?locationNum=1&fps=1

DCOM是依赖于RPC服务的,是一种分布式远程过程(进程)调用,调用代码在远程主机上执行,使用远程主机的系统资源,因此要有远程主机所属的访问权限,一般来说访问用户应是远程主机本地用户或主机所属的域用户,所以

  第一步是在远程主机或其域上注册(获得)一个有足够权限的用户,

  第二步是使用此用户令牌激活远程COM服务器,方法是使用CoInitializeSecurity & CoCreateInstanceEx函数(见文后例子),成功激活后就得到相关请求接口的代理,此时代理并没有对远程服务器访问的权限,

  第三步是为接口代理设置访问权限,方法是使用CoSetProxyBlanket函数,此后就可以使用代理指针进行方法调用了。见文后例子

  以上是客户端要做的处理,至于服务器端,只要使用CoInitializeSecurity函数设置一下访问许可权限就可以了(有时客户端也要进行这一处理,比如使用了连接点),对于即存的服务器程序使用dcomcfg工具在注册表里设置也可以。

  CoSetProxyBlanket函数调用例子:

  *******************************

  原例子:

  

  1.   IRecordServerLink* pRSL=(IRecordServerLink*)mq[0].pItf;
  2.    
  3.   hr = CoSetProxyBlanket( pRSL, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
  4.      
  5.   RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, &caid, EOAC_NONE);
  6.   pRSL->Methods();

 

  ----------------------------------

  修改后的例子:

  

  1.   hr = CoSetProxyBlanket( pAccount, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE, NULL,
  2.      
  3.   RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, &caid, EOAC_NONE);
  4.   pAccount->Deposit(x);

 

  ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※

  CoInitializeSecurity & CoCreateInstanceEx函数调用例子:

  *********************************************************

  例子1:

  

  1.   HRESULT hr;
  2.   hr = CoInitialize(NULL);
  3.   ASSERT(SUCCEEDED(hr));
  4.   MULTI_QI qi;
  5.   qi.pIID = &IID_ICRemoteTime;
  6.   qi.hr = NULL;
  7.   qi.pItf = NULL;
  8.   COAUTHIDENTITY authidentity;
  9.   authidentity.User = L"administrator";  你机子的帐号
  10.   authidentity.UserLength = wcslen(authidentity.User);
  11.   authidentity.Domain = NULL;
  12.   authidentity.DomainLength = 0;
  13.   authidentity.Password = L"";     密码
  14.   authidentity.PasswordLength = wcslen(authidentity.User);
  15.   authidentity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  16.   COAUTHINFO authinfo = {-1, 0, 0, RPC_C_AUTHN_LEVEL_DEFAULT,
  17.      
  18.   RPC_C_IMP_LEVEL_IMPERSONATE, &authidentity, 0};
  19.   COSERVERINFO servInf;
  20.   servInf.dwReserved1 = NULL;
  21.   servInf.dwReserved2 = NULL;
  22.   // servInf.pAuthInfo = NULL;
  23.   servInf.pAuthInfo = &authinfo;
  24.   USES_CONVERSION;
  25.   servInf.pwszName = L"127.0.0.1";
  26.   hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
  27.    
  28.   RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_ACCESS_CONTROL, NULL);
  29.   ASSERT(SUCCEEDED(hr));
  30.   hr = CoCreateInstanceEx(CLSID_CRemoteTime, NULL, CLSCTX_REMOTE_SERVER, &servInf, 1, &qi);
  31.   if (FAILED(hr))
  32.   {
  33.    TRACE(_T("CoCreateInstanceEx failed"));
  34.    return false;;
  35.   }
  36.   if (FAILED(qi.hr))
  37.   {
  38.    TRACE(_T("Failed to connect to server"));
  39.    return false;;
  40.   }
  41.   //通过IUnkonwn指针去查询接口指针,返回IAccount指针
  42.   hr = pUnknown->QueryInterface(IID_ICRemoteTime,(void**)&pIRetime)

 

  例子2:

  

  1.   HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
  2.    
  3.   RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
  4.    
  5.    COAUTHIDENTITY us;
  6.    
  7.    us.User      = m_strName.AllocSysString();
  8.    us.UserLength   = wcslen(us.User);
  9.    us.Password    = m_strPassword.AllocSysString();
  10.    us.PasswordLength = wcslen(us.Password);
  11.    us.Domain     = m_strDomain.AllocSysString();
  12.    us.DomainLength  = wcslen(us.Domain);
  13.    us.Flags     = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  14.    
  15.    COAUTHINFO auth;
  16.    
  17.    auth.dwAuthnSvc      = RPC_C_AUTHN_WINNT;
  18.    auth.dwAuthzSvc      = RPC_C_AUTHZ_NONE;
  19.    auth.pwszServerPrincName = NULL;
  20.    auth.dwAuthnLevel     = RPC_C_AUTHN_LEVEL_CONNECT;
  21.    auth.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
  22.    auth.dwCapabilities    = EOAC_NONE;
  23.    auth.pAuthIdentityData  = &us;
  24.    
  25.    COSERVERINFO si;
  26.    MULTI_QI   qi;
  27.    
  28.    CComBSTR bstr = strComputer;
  29.    LPWSTR name  = bstr.m_str;
  30.    
  31.    si.dwReserved1 = 0;
  32.    si.pwszName  = name;
  33.    si.pAuthInfo  = m_bAccess ? &auth : NULL;
  34.    si.dwReserved2 = 0;
  35.    
  36.    IID iid = __uuidof(m_pIRemoteControl);
  37.    qi.pIID = &iid;
  38.    qi.pItf = NULL;
  39.    
  40.    do
  41.    {
  42.    
  43.   hr = CoCreateInstanceEx(__uuidof(RemoteControl), NULL, CLSCTX_SERVER, &si, 1, &qi);
  44.    
  45.   if(FAILED(hr) || FAILED(qi.hr))
  46.    
  47.   break ;
  48.    
  49.    
  50.   m_pIRemoteControl = (IRemoteControl *)qi.pItf;
  51.    }while(0);

例子3:

  

  1.   HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
  2.    
  3.   RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
  4.    
  5.    COAUTHIDENTITY us;
  6.    
  7.    us.User      = m_strName.AllocSysString();
  8.    us.UserLength   = wcslen(us.User);
  9.    us.Password    = m_strPassword.AllocSysString();
  10.    us.PasswordLength = wcslen(us.Password);
  11.    us.Domain     = m_strDomain.AllocSysString();
  12.    us.DomainLength  = wcslen(us.Domain);
  13.    us.Flags     = SEC_WINNT_AUTH_IDENTITY_UNICODE;
  14.    
  15.    COAUTHINFO auth;
  16.    
  17.    auth.dwAuthnSvc      = RPC_C_AUTHN_WINNT;
  18.    auth.dwAuthzSvc      = RPC_C_AUTHZ_NONE;
  19.    auth.pwszServerPrincName = NULL;
  20.    auth.dwAuthnLevel     = RPC_C_AUTHN_LEVEL_CONNECT;
  21.    auth.dwImpersonationLevel = RPC_C_IMP_LEVEL_IMPERSONATE;
  22.    auth.dwCapabilities    = EOAC_NONE;
  23.    auth.pAuthIdentityData  = &us;
  24.    
  25.   MULTI_QI MultiQi={&IID_IUnknown,NULL,NOERROR};
  26.    COSERVERINFO si;
  27.   CComBSTR bstr = strComputer;
  28.    LPWSTR name  = bstr.m_str;
  29.    
  30.    si.dwReserved1 = 0;
  31.    si.pwszName  = name;
  32.    si.pAuthInfo  = m_bAccess ? &auth : NULL;
  33.    si.dwReserved2 = 0;
  34.    
  35.    
  36.   hr = CoCreateInstanceEx(__uuidof(RemoteControl), NULL, CLSCTX_SERVER, &si, 1, &MultiQi);
  37.    
  38.    if(FAILED(hr))
  39.    {
  40.    
  41.   MessageBox("创建对象实例失败!");
  42.    
  43.   return;
  44.    }
  45.    
  46.    
  47.   //通过IUnkonwn指针去查询接口指针,返回IAccount指针
  48.    pUnknown = (IUnknown *) MultiQi.pItf;
  49.   hr = pUnknown->QueryInterface(IID_IAccount,(void**)&pAccount);
  50.    if(FAILED(hr))
  51.    {
  52.    
  53.   MessageBox("没有查找的接口指针!");
  54.    
  55.   return false;
  56.    }
  57.    pUnknown->Release();
http://www.warting.com/program/201110/37053_2.html
上一篇:ubuntu 20.04 中文环境和英文环境切换


下一篇:linux下添加桌面图标