Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Control How COM Marshals Your Data


March 1998/Control How COM Marshals Your Data/Listing 6

Listing 6: server.cpp — Simple console-mode server app

// COM Marshalling Sample Server
// Author : Fran Heeran
#define  INC_OLE2
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <initguid.h>
#include "marsamp.h"

extern "C" BOOL RegisterServer(LPSTR pszExeName);
extern "C" void UnregisterServer();
HANDLE   hevtDone;

// Standard Class Factory
class CClassFactory : public IClassFactory
{
protected:
   ULONG m_cRef;
public:
   CClassFactory() { m_cRef = 1; }

   STDMETHODIMP QueryInterface (REFIID riid, void** ppv);
   STDMETHODIMP_(ULONG) AddRef(void)  { return ++m_cRef; }
   STDMETHODIMP_(ULONG) Release(void);
   STDMETHODIMP CreateInstance(LPUNKNOWN, REFIID, LPVOID FAR*);
   STDMETHODIMP LockServer(BOOL) { return E_FAIL; }
};

// ISimpleObject Interface
class CSimpleObject : public IUnknown {
   private:
      LONG m_cRef;
   public:
      CSimpleObject() { m_cRef = 1; }
      ~CSimpleObject() { SetEvent(hevtDone); }
      IUNKNOWN_METHODS(IMPL);
      ISIMPLEOBJECT_METHODS(IMPL);
};

CClassFactory  g_ClassFactory;

int __cdecl main(int argc, char *argv[], char *envp[])
{
   HRESULT hr;
   DWORD   dwRegister;

   // Handle register/unregister commands
   if (argc > 1) {
      if (!strcmp(argv[1], "-register")) {
         if (argc != 3) {
            printf("Useage is server -register [path to server \
& proxy/stub]");
            return 0;
         }
         if (!RegisterServer(argv[2]))
            printf("Failed to register Server & Proxy/Stub");
         else
            printf("Successfully registered Server & Proxy/Stub");
         return 0;
      }
      else if (!strcmp(argv[1], "-unregister")) {
         UnregisterServer();
         printf("Unregistered Server & Proxy/Stub");
         return 0;
      }
   }

   // Create the thread which is signaled when the
   // instance is deleted
   hevtDone = CreateEvent(NULL, FALSE, FALSE, NULL);
   if (!hevtDone)
      return 1;

   // Initialize COM
   hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
   RETURNONFAILURE(hr, "CoInitializeEx() failed");

   // Register class-object
   hr = CoRegisterClassObject(CLSID_SimpleObject, &g_ClassFactory,
                  CLSCTX_SERVER, REGCLS_SINGLEUSE, &dwRegister);
   RETURNONFAILURE(hr,"Failed to register class object");

   printf("Waiting...\n");

   // Wait until we have been called to display the array
   WaitForSingleObject(hevtDone, INFINITE);
   CloseHandle(hevtDone);
   CoUninitialize();

   // Wait for a key so the user can view the output
   printf("\nPress Any Key to Exit\n");
   char ch = _getch();

   return 0;
}

STDMETHODIMP CClassFactory::QueryInterface(REFIID riid,
                                           LPVOID FAR *ppvObject)
{
   *ppvObject = NULL;

        if(IsEqualIID(riid, IID_IUnknown))
                *ppvObject = (LPUNKNOWN) (LPCLASSFACTORY) this;
        else if(IsEqualIID(riid, IID_IClassFactory))
                *ppvObject = (LPCLASSFACTORY) this;
        else
                return E_NOINTERFACE;

        ((LPUNKNOWN)*ppvObject)->AddRef();

        return NOERROR;
}

STDMETHODIMP_(ULONG) CClassFactory::Release()
{
   if (--m_cRef)
      return m_cRef;
   delete this;
   return 0;
}

STDMETHODIMP CClassFactory::CreateInstance(LPUNKNOWN  pUnkOuter,
                                           REFIID     riid,
                                           LPVOID FAR *ppvObject)
{
   HRESULT        hr;
   CSimpleObject  *pObj;

   *ppvObject = NULL;
   hr = ResultFromScode(E_OUTOFMEMORY);

   // Verify that a controlling unknown asks for IUnknown
   if (NULL != pUnkOuter)
      return ResultFromScode(CLASS_E_NOAGGREGATION);

   // Create the object telling us to notify us when it's gone.
   pObj = new CSimpleObject();
   if (NULL == pObj)
      return hr;
   hr = pObj->QueryInterface(riid, ppvObject);
   if (FAILED(hr))
      delete pObj;

   pObj->Release();

   return hr;
}

STDMETHODIMP_(ULONG) CSimpleObject::Release()
{
   if (!InterlockedDecrement(&m_cRef)) {
      delete this;
      return 0;
   }
   return m_cRef;
}


STDMETHODIMP_(ULONG) CSimpleObject::AddRef()
{
   return InterlockedIncrement(&m_cRef);
}

STDMETHODIMP CSimpleObject::QueryInterface(REFIID riid, void** ppv)
{
   if (ppv == NULL)
      return E_INVALIDARG;
   if (riid == IID_IUnknown || riid == IID_ISimpleObject) {
      *ppv = (IUnknown *) this;
      AddRef();
      return S_OK;
   }

   *ppv = NULL;
   return E_NOINTERFACE;
}

STDMETHODIMP CSimpleObject::DisplayArray(BOOL *pArr)
{
   printf("Array Length = %d Bytes\r\nCompressed Length = %d \
Bytes\r\n", FLAG_ARRAY_SIZE * sizeof(BOOL), FLAG_ARRAY_SIZE / 8);

   for (int i = 0;i < FLAG_ARRAY_SIZE;i++)
      printf("%d ", *(pArr + i));

   return S_OK;
}
//End of File

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.