When DCOM/COM + is called on the local machine, the problem is often relatively simple. If remote calls are required, many problems may occur, such as 80070005 (acess is denied) and 80040155 (interface not registered). Because cross-Machine Access is involved, permissions are involved, registry configuration and so on.At the same time, when different types of clients (C ++ type and other compilation customers, or VBScript and other interpretation customers) remotely call DCOM, the client requires different configurations.. This article describes the example of a remote call to DCOM for a compiled client.
First, when the client calls a remote call, you need to configure the following options on the client:
Select 1. PassNmake-F <dcomservice>. mkGenerate the corresponding proxy DLL, then deploy the proxy DLL (generally <dcomservice> ps. dll) in the customer segment, andRegsvr32 <dcomservice> ps. dllTo achieve remote calls on the client.
Select 2. Set<Dcomservice>. TLBPublish the file to the client, and then runRegtlib (regtlibv12) <dcomservice>. TLBAnd can be remotely called on the client.
The client call is given belowCode(Vc6.0 ):
Note: If you call the cocreateinstanceex API, you must enable a _ win32_dcom switch. You can add it to stdafx. h.
# DEFINE _ win32_dcom
Or add this value to preprocess as follows:
// Dcomserviceclient. cpp: defines the entry point for the console application.
//
# Include " Stdafx. h "
# Include " .. \ Dcomdemov1.h "
# Include " .. \ Dcomdemovpolici.c "
# Include < Atlbase. h >
# Include < Comdef. h >
# Include < Comutil. h >
int main ( int argc, char * argv [])
{< br> /* createinstance locally via smartpointer;
ccomptr spsimpleclass;
coinitialize (null);
hresult hr;
hR = spsimpleclass. cocreateinstance (_ uuidof (simpleclass);
If (failed (HR) {printf ("error code: % x", HR); Return hr ;}
BSTR result;
HR = spsimpleclass-> helloworld (& result );
If (failed (HR) {printf ("error code: % x", HR); Return hr ;}
Char * P =: _ com_util: convertbstrtostring (result );
Printf ("% s \ n", P );
: sysfreestring (result);
couninitialize ();
return 0;
*/
// =============================================== ========================
/* createinstance locally via primitive pointer;
isimpleclass * psimpleclass;
coinitialize (null);
hresult hr;
hR = cocreateinstance (clsid_simpleclass, 0, clsctx_local_server, iid_isimpleclass, (void **) & psimpleclass );
If (failed (HR) return hr;
BSTR result;
HR = psimpleclass-> helloworld (& result );
Char * P =: _ com_util: convertbstrtostring (result );
Printf ("% s \ n", P );
Psimpleclass-> release ();
Couninitialize ();
Return 0;
*/
//Call remotely
Hresult hr;
Isimpleclass*Pi=NULL;
Coserverinfo sin,*Sinptr;
Multi_qi mqi;
Mqi. piid= &Iid_isimpleclass;
Mqi. hr=0;
Mqi. pitf=0;
Coauthinfo authinfo;
Authinfo. dwauthnsvc = Rpc_c_authn_winnt; // Rpc_c_authn_none;
Authinfo. dwauthzsvc = Rpc_c_authz_none;
Authinfo. pwszserverprincname = NULL;
Authinfo. dwauthnlevel = Rpc_c_authn_level_pkt; // Rpc_c_authn_level_none;
Authinfo. dwimpersonationlevel = Rpc_c_imp_level_impersonate; // Rpc_c_imp_level_anonymous;
Authinfo. pauthidentitydata = NULL;
Authinfo. dwcapabilities = NULL;
Sin. dwreserved1 = 0 ;
Sin. dwreserved2 = 0 ;
Sin. pwszname = L " Azalea-desk " ; // Define the remote server name here
Sin. pauthinfo = & Authinfo;
Sinptr = & Sin;
HR = Coinitialize ( 0 );
If (Succeeded (HR ))
{
HR = Cocreateinstanceex (clsid_simpleclass,
Null,
Clsctx_remote_server,
Sinptr,
1 ,
& Mqi
);
If (Succeeded (HR ))
{
Pi = (Isimpleclass * ) Mqi. pitf;
Printf ( " DCOM server connect \ n " );
BSTR bsreturnvalue;
Pi -> Helloworld ( & Bsreturnvalue );
Pi -> Release ();
Char * Pvalue = _ Com_util: convertbstrtostring (bsreturnvalue );
Printf ( " % S \ n " , Pvalue );
Delete pvalue;
}
Else
{
Printf ( " Createinstance error! Error code: % x \ n " , HR );
}
}
Else
{
Printf ( " Coinitialize error! Error code: % x \ n " , HR );
}
Couninitialize ();
Return 0;
}
This DCOM Service only defines a method for returning the string helloworld (). The screenshot after the client call is successful is as follows:
Click here to download the source code