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 3

Listing 3: register.c — Code to register/unregister DLL

// Server and Proxy/Stub registration
// Author : Fran Heeran
#include <windows.h>
#include "marsamp.h"

char    s_szCLSID[]   = "{BAEA9B70-28E9-11d1-ADD0-006097731D51}";
char    s_szIFaceID[] = "{005763A0-2850-11d1-ADCD-006097731D51}";

// Creates a new registry key and optionally sets a default value
BOOL SetRegKeyValue(LPTSTR pszKey,LPTSTR pszSubkey, LPTSTR pszValue)
{
   LONG  lRes;
   HKEY  hKey;
   TCHAR szKey[256];

  lstrcpy(szKey, pszKey);

  if (NULL != pszSubkey) {
    lstrcat(szKey, TEXT("\\"));
    lstrcat(szKey, pszSubkey);
  }

  if (ERROR_SUCCESS != RegCreateKeyEx(HKEY_CLASSES_ROOT,szKey,0,
      NULL,REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS,NULL,&hKey,NULL))
      return FALSE;

   if (pszValue)
      lRes = RegSetValueEx(hKey,NULL,0,REG_SZ,(BYTE *)pszValue,
            (lstrlen(pszValue)+1)*sizeof(TCHAR));

   RegCloseKey(hKey);

   return (lRes == ERROR_SUCCESS);
}

// Deletes a registry key
void DeleteRegKey(LPTSTR pszKey,LPTSTR pszSubkey)
{
   TCHAR szKey[256];

   lstrcpy(szKey, pszKey);

   if (pszSubkey) {
      lstrcat(szKey, TEXT("\\"));
      lstrcat(szKey, pszSubkey);
   }

   RegDeleteKey(HKEY_CLASSES_ROOT, szKey);
}

// Called to register the Server and Proxy/Stub DLL.The servers file
// name is passed in and the Proxy/Stub DLL name derived from that
BOOL RegisterServer(LPSTR pszExePath)
{
   TCHAR szCLSID[129];
   TCHAR szIFace[129];
   char  szFile[MAX_PATH];

   lstrcpy(szCLSID, TEXT("CLSID\\"));
   lstrcat(szCLSID, s_szCLSID);

   strcpy(szFile, pszExePath);
   strcat(szFile, "\\server.exe");

  // Create entries under CLSID.
   SetRegKeyValue(szCLSID, NULL,
            TEXT("ISimpleObject - WDJ wire_marshall example"));
   SetRegKeyValue(szCLSID, TEXT("LaunchPermission"), TEXT("Y"));
   SetRegKeyValue(szCLSID, TEXT("LocalServer32"), szFile);

   // Register the Proxy/Stub DLL
   strcpy(szFile, pszExePath);
   strcat(szFile, "\\sobj.dll");

   lstrcpy(szIFace, TEXT("Interface\\"));
   lstrcat(szIFace, s_szIFaceID);

   // Create the HKEY_CLASSES_ROOT\Interface entries.
   SetRegKeyValue(szIFace, NULL, TEXT("ISimpleObject"));
   SetRegKeyValue(szIFace, TEXT("ProxyStubClsid32"), s_szIFaceID);
   SetRegKeyValue(szIFace, TEXT("NumMethods"), TEXT("4"));

   // Create the HKEY_CLASSES_ROOT\CLSID entries.
   lstrcpy(szCLSID, TEXT("CLSID\\"));
   lstrcat(szCLSID, s_szIFaceID);
   SetRegKeyValue(szCLSID, NULL,
                  TEXT("ISimpleObject Proxy/Stub Factory"));
   SetRegKeyValue(szCLSID, TEXT("InprocServer32"), szFile);

   return TRUE;
}

// Called to unregister the Server and Proxy/Stub DLL
void UnregisterServer()
{
   TCHAR szCLSID[129];
   TCHAR szIFace[129];

   // Delete server registry entries
   lstrcpy(szCLSID, TEXT("CLSID\\"));
   lstrcat(szCLSID, s_szCLSID);

   DeleteRegKey(szCLSID, TEXT("LaunchPermission"));
   DeleteRegKey(szCLSID, TEXT("LocalServer32"));
   DeleteRegKey(szCLSID, NULL);

   // Delete Proxy/Stub Entries
   lstrcpy(szIFace, TEXT("Interface\\"));
   lstrcat(szIFace, s_szIFaceID);

   DeleteRegKey(szIFace, TEXT("ProxyStubClsid32"));
   DeleteRegKey(szIFace, TEXT("NumMethods"));
   DeleteRegKey(szIFace, NULL);

   lstrcpy(szCLSID, TEXT("CLSID\\"));
   lstrcat(szCLSID, s_szIFaceID);
   DeleteRegKey(szCLSID, TEXT("InprocServer32"));
   DeleteRegKey(szCLSID, NULL);
}
//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.