Click here to Skip to main content
Licence 
First Posted 8 Feb 2005
Views 68,377
Bookmarked 28 times

Client/Server connection using DCOM, with anonymous access

By | 8 Feb 2005 | Article
Quick implementation of client/server connection using DCOM.

Introduction

This article will explain implementing a quick client/server connection using DCOM.

Using the code

This article consists of two parts:

  • Server side implementation
  • Client side implementation

Let's start with server side implementation:

  1. Start 'New ATL COM Wizard project' with Visual Studio.
  2. Name it as TestServer.
  3. Select server type: Service.
  4. Insert new ATL object: Category-Objects, Objects-Simple Objects.
  5. Name the interface as IHello.
  6. Add method Beep().

Now, we need to turn off the authentication of DCOM server. Open TestServer.rgs from Workspace window and add the following lines just before 'TestServer.EXE':

{
    val AccessPermission = b '01000480300000004c000000000000001
            400000002001c000100000000021400010000000101000000000001000000000
            105000000000005150000007ceb240dfa4f0c2ff89fb474f2030000010500000
            0000005150000007ceb240dfa4f0c2ff89fb47401020000'
    val LaunchPermission = b '01000480300000004c000000000000001
            400000002001c000100000000021400010000000101000000000001000000000
            105000000000005150000007ceb240dfa4f0c2ff89fb474f2030000010500000
            0000005150000007ceb240dfa4f0c2ff89fb47401020000'
}

Open testMonitor.cpp, locate Run() function, and make it look like this:

void CServiceModule::Run()
{
    _Module.dwThreadID = GetCurrentThreadId();

    //This means that calls come in on a random RPC thread
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);

    // This provides a NULL DACL which will allow access to everyone.
    hr = CoInitializeSecurity( 0, -1, 0, 0, 
         RPC_C_AUTHN_LEVEL_NONE, 
         RPC_C_IMP_LEVEL_IDENTIFY, 0, 0, 0 );
    hr = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER | 
         CLSCTX_REMOTE_SERVER, REGCLS_MULTIPLEUSE);
    ..............

Finally, add the following line to Beep() method and the server side is ready!!!

MessageBeep(MB_ICONEXCLAMATION);

Now, let's implement the client:

  1. Start 'New MFC App Wizard, dialog based, name it TestClient.
  2. Insert Resource, Custom, and name it exactly TYPELIB.
  3. ID of TYPELIB resource must be 1.
  4. Open property page for TYPELIB resource and specify path to file as TestServer.tlb.
  5. Add #import "..//TestServer/testserver.tlb" no_namespace line to stdafx.h.
  6. Add includes for Afxctl.h, objbase.h and initguid.h to stdafx.h.
  7. Add _WIN32_DCOM to preprocessor definitions.

Now, we need to add the actual connection code. Add the following Connect() function to your app class:

IIHello* CTestClientApp::Connect(CString& strIPAddress, CString& strError)
{
    // register type library
    CLSID libid;

    CLSIDFromString (L"{55259785-1985-4DCB-B4BA-FCB7F968DFA4}", &libid);

    if (!AfxOleRegisterTypeLib(AfxGetInstanceHandle(), libid))
    {
        AfxMessageBox("Failed to register server's type library");
        return NULL;
    }

    CLSID clsid;
    const IID IID_IHELLO = {0xFB57F08C,0x0F91,0x4046, 
              {0x8E,0xF1,0x05,0xD8,0x90,0x4C,0x66,0xF0}};
    MULTI_QI mqi = { &IID_IHELLO, 0, 0 };

    CLSIDFromString (L"{C7B7C24F-5B72-49D0-962B-FFD650BFFA2B}", &clsid);
    CoInitialize(0);
    COAUTHINFO cai = {  RPC_C_AUTHN_NONE,
                        RPC_C_AUTHZ_NONE,
                        0,
                        RPC_C_AUTHN_LEVEL_NONE,
                        RPC_C_IMP_LEVEL_IMPERSONATE,
                        0,
                        EOAC_NONE};

    COSERVERINFO csi = { 0, strIPAddress.AllocSysString(), &cai, 0 };

    HRESULT hr = CoCreateInstanceEx(clsid, NULL, 
                 CLSCTX_REMOTE_SERVER, &csi, 1, &mqi);

    if (FAILED(hr))
    {

        TCHAR szError[1024];
        FormatMessage(  FORMAT_MESSAGE_FROM_SYSTEM,
                        NULL,
                        hr,
                        MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
                        szError,
                        1024,
                        NULL);
        strError = szError;
        return FALSE;
    }

    return (IIHello*)(mqi.pItf);
}

From the code provided above, it can be seen that three GUIDs are needed: LIBID, CLASSID, and ITERFACEID. All of them must be taken from testserver.idl. Our connection code example is a bit hard-coded, but it allows to run the client on any machine without any registration. Server has to be registered before use. Since this is a DCOM server, it can be registered as a service or as a local server. To register the server as a service, type:

testserver /Service

To register the server as a local server, type:

testserver /RegServer

And finally, let's execute Beep() method on the remote server. To do that, let's just add a handler for the Beep button.

void CTestClientDlg::OnButtonBeep()
{
    ((CTestClientApp*)AfxGetApp())->m_pHello->Beep();
}

Hope this wasn't boring, but useful :-)

History

  • 06/02/2005 - Initial document creation.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vigen Dolbakyan

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAccess is denied PinmemberFulpagar Manik23:53 9 Jun '08  
GeneralAccess Denied or class not register Pinmembersanjayrvyas1:41 27 Sep '07  
QuestionDCOM vb Client Pinmembersivaganesh_gt5:10 18 Dec '06  
GeneralGenerate & Build Proxy dll for DCOM PinmemberAshwin kumar Gurujala22:34 13 Dec '05  
GeneralRe: Generate & Build Proxy dll for DCOM PinmemberAshwin kumar Gurujala22:35 13 Dec '05  
GeneralAccess is denied PinsussFalcon eyed23:52 17 May '05  
GeneralRe: Access is denied PinmemberLayla554:00 22 Jun '05  
GeneralRe: Access is denied Pinmemberkevinlzj22:00 8 May '06  
GeneralRe: Access is denied Pinmembershibu george19:18 20 Jul '06  
GeneralRPC-Server PinmemberNightShark0:13 15 Feb '05  
GeneralRe: RPC-Server PinmemberVigen Dolbakyan5:43 15 Feb '05  
GeneralRe: RPC-Server PinmemberNightShark11:32 15 Feb '05  
GeneralRe: RPC-Server Pinmemberskchow16:01 14 Mar '05  
GeneralRe: RPC-Server PinsussAnonymous3:58 14 Apr '05  
GeneralRe: RPC-Server PinmemberGrol23:30 14 Apr '10  
GeneralVS.NET 2003 Pinmemberbriviere25:59 9 Feb '05  
GeneralRe: VS.NET 2003 Pinmemberbriviere26:01 9 Feb '05  
GeneralRe: VS.NET 2003 PinmemberVigen Dolbakyan6:02 9 Feb '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 9 Feb 2005
Article Copyright 2005 by Vigen Dolbakyan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid