DCOM depends on the RPC service and is a Distributed Remote Process (process) call. The call code is executed on the remote host and the system resources of the remote host are used, therefore, you must have the access permission to the remote host. Generally, the access user should be the local user of the remote host or the domain user of the host. Therefore, the first step is to register (obtain) the remote host or its domain) A user with sufficient permissions, step 2 is to use this User Token to activate the Remote COM server by using the coinitializesecurity & cocreateinstanceex function (see the following example ), after successful activation, the proxy of the relevant request interface is obtained. At this time, the proxy does not have the permission to access the remote server. The third step is to set the access permission for the interface proxy by using the cosetproxyblket function, then you can use the proxy pointer to call the method. See the following example.
The above is what the client needs to do. As for the server side, you only need to use the coinitializesecurity function to set the access permission (sometimes the client also needs to do this, such as using the connection point ), you can also set the dcomcfg tool in the Registry for a stored server program.
Example of cosetproxyblanket function call: ******************************** original example: irecordserverlink * prsl = (irecordserverlink *) MQ [0]. pitf; HR = cosetproxyblanket (prsl, rpc_c_authn_winnt, rpc_c_authz_none, null,
Rpc_c_authn_level_default, rpc_c_imp_level_impersonate, & Caid, eoac_none );
Prsl-> methods (); -------------------------------- modified example: Hr = cosetproxyblanket (paccount, rpc_c_authn_winnt, rpc_c_authz_none, null,
Rpc_c_authn_level_default, rpc_c_imp_level_impersonate, & Caid, eoac_none );
Paccount-> deposit (X ); ※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※ example of coinitializesecurity & cocreateinstanceex function call: **************************************** * ***************** Example 1: hresult hr;
HR = coinitialize (null );
Assert (succeeded (HR ));
Multi_qi qi;
Qi. piid = & iid_icremotetime;
Qi. HR = NULL;
Qi. pitf = NULL;
Coauthidentity authidentity;
Authidentity. User = l "Administrator"; // your account
Authidentity. userlength = wcslen (authidentity. User );
Authidentity. Domain = NULL;
Authidentity. domainlength = 0;
Authidentity. Password = l ""; // Password
Authidentity. passwordlength = wcslen (authidentity. User );
Authidentity. Flags = sec_winnt_auth_identity_unicode;
Coauthinfo authinfo = {-1, 0, 0, rpc_c_authn_level_default,
Rpc_c_imp_level_impersonate, & authidentity, 0 };
Coserverinfo servinf;
Servinf. dwreserved1 = NULL;
Servinf. dwreserved2 = NULL;
// Servinf. pauthinfo = NULL;
Servinf. pauthinfo = & authinfo;
Uses_conversion;
Servinf. pwszname = l "127.0.0.1 ";
HR = coinitializesecurity (null,-1, null, null,
Rpc_c_authn_level_none, rpc_c_imp_level_identify, null, eoac_access_control, null );
Assert (succeeded (HR ));
HR = cocreateinstanceex (clsid_cremotetime, null, clsctx_remote_server, & servinf, 1, & Qi );
If (failed (HR ))
{
Trace (_ T ("cocreateinstanceex failed "));
Return false ;;
}
If (failed (QI. hr ))
{
Trace (_ T ("failed to connect to server "));
Return false ;;
}
// Use the iunkonwn pointer to query the interface pointer and return the iaccount pointer hR = punknown-> QueryInterface (iid_icremotetime, (void **) & piretime) Example 2: hresult hR = coinitializesecurity (null, -1, null, null,
Rpc_c_authn_level_none, rpc_c_imp_level_identify, null, eoac_none, null );
Coauthidentity us;
Us. User = m_strname.allocsysstring ();
Us. userlength = wcslen (US. User );
Us. Password = m_strpassword.allocsysstring ();
Us. passwordlength = wcslen (US. Password );
Us. Domain = m_strdomain.allocsysstring ();
Us. domainlength = wcslen (US. domain );
Us. Flags = sec_winnt_auth_identity_unicode;
Coauthinfo auth;
Auth. dwauthnsvc = rpc_c_authn_winnt;
Auth. dwauthzsvc = rpc_c_authz_none;
Auth. pwszserverprincname = NULL;
Auth. dwauthnlevel = rpc_c_authn_level_connect;
Auth. dwimpersonationlevel = rpc_c_imp_level_impersonate;
Auth. dwcapabilities = eoac_none;
Auth. pauthidentitydata = & US; coserverinfo Si;
Multi_qi qi;
Ccombstr BSTR = strcomputer;
Lpwstr name = BSTR. m_str;
Si. dwreserved1 = 0;
Si. pwszname = Name;
Si. pauthinfo = m_baccess? & Auth: NULL;
Si. dwreserved2 = 0;
IID = _ uuidof (m_piremotecontrol );
Qi. piid = & IID;
Qi. pitf = NULL;
Do
{
HR = cocreateinstanceex (_ uuidof (remotecontrol), null, clsctx_server, & Si, 1, & Qi );
If (failed (HR) | failed (QI. hr ))
Break;
M_piremotecontrol = (iremotecontrol *) QI. pitf;
} While (0); --------------------------------- Example 3: hresult hR = coinitializesecurity (null,-1, null, null,
Rpc_c_authn_level_none, rpc_c_imp_level_identify, null, eoac_none, null );
Coauthidentity us;
Us. User = m_strname.allocsysstring ();
Us. userlength = wcslen (US. User );
Us. Password = m_strpassword.allocsysstring ();
Us. passwordlength = wcslen (US. Password );
Us. Domain = m_strdomain.allocsysstring ();
Us. domainlength = wcslen (US. domain );
Us. Flags = sec_winnt_auth_identity_unicode;
Coauthinfo auth;
Auth. dwauthnsvc = rpc_c_authn_winnt;
Auth. dwauthzsvc = rpc_c_authz_none;
Auth. pwszserverprincname = NULL;
Auth. dwauthnlevel = rpc_c_authn_level_connect;
Auth. dwimpersonationlevel = rpc_c_imp_level_impersonate;
Auth. dwcapabilities = eoac_none;
Auth. pauthidentitydata = & US; multi_qi multiqi = {& iid_iunknown, null, noerror}; coserverinfo Si; ccombstr BSTR = strcomputer;
Lpwstr name = BSTR. m_str;
Si. dwreserved1 = 0;
Si. pwszname = Name;
Si. pauthinfo = m_baccess? & Auth: NULL;
Si. dwreserved2 = 0;
HR = cocreateinstanceex (_ uuidof (remotecontrol), null, clsctx_server, & Si, 1, & multiqi );
If (failed (HR ))
{
MessageBox ("An error occurred while creating the object instance! ");
Return;
}
// Use the iunkonwn pointer to query the interface pointer and return the iaccount pointer.
Punknown = (iunknown *) multiqi. pitf;
HR = punknown-> QueryInterface (iid_iaccount, (void **) & paccount );
If (failed (HR ))
{
MessageBox ("no interface pointer found! ");
Return false;
} Punknown-> release ();