home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / wincom / dllreg / dllreg.c next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.7 KB  |  177 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #ifdef WIN32
  20. #include <objbase.h>
  21. #else
  22. #include <windows.h>
  23. #include <compobj.h>
  24. #include <stdlib.h>
  25. #endif
  26.  
  27. /*  This is mainly sample source to show how to auto[un]register a DLL.
  28.  *  This is equivalent to REGSVR32.EXE for 32 bits (VC4), and
  29.  *      REGSVR.EXE for 16 bits (MSVC CDK16 only).
  30.  *
  31.  *  Also, we can ship this, I am not sure if we can redistribute those
  32.  *      utilities.
  33.  */
  34.  
  35. typedef HRESULT (*DllServerFunction)(void);
  36.  
  37. #ifdef WIN32
  38. int main(int iArgc, char *paArgv[])
  39. #else
  40. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  41. #endif
  42. {
  43.     BOOL     bRegister = TRUE;
  44.     BOOL     bRetval = FALSE;
  45.     char    szPath[_MAX_PATH];
  46.     LPSTR    pLib = NULL;
  47.     LPSTR    pOldLib = NULL;
  48.  
  49. #ifdef WIN32
  50.     if(iArgc >= 2)  {
  51.         int iTraverse = 1;
  52.         while(iTraverse < iArgc)    {
  53.             if(!stricmp(paArgv[iTraverse], "/u"))    {
  54.                 bRegister = FALSE;
  55.             }
  56.             else    {
  57.                 pLib = paArgv[iTraverse];
  58.             }
  59.  
  60.             iTraverse++;
  61.         }
  62.     }
  63. #else
  64.     if(lpCmdLine && *lpCmdLine)  {
  65.         char *pTraverse = lpCmdLine;
  66.         while(*pTraverse)   {
  67.             if(!strnicmp(pTraverse, "/u", 2))  {
  68.                 bRegister = FALSE;
  69.  
  70.                 *pTraverse = ' ';
  71.                 pTraverse++;
  72.                 *pTraverse = ' ';
  73.             }
  74.  
  75.             pTraverse++;
  76.         }
  77.  
  78.         pTraverse = lpCmdLine;
  79.         while(*pTraverse && isspace(*pTraverse))  {
  80.             pTraverse++;
  81.         }
  82.  
  83.         pLib = pTraverse;
  84.     }
  85. #endif
  86.  
  87.     CoInitialize(NULL);
  88.  
  89.     if(pLib)    {
  90.         /*  Expand to the full path or LoadLibrary likes to fail
  91.          *      if it is too convoluted/long/relative.
  92.          */
  93.         pOldLib = pLib;
  94.         pLib = _fullpath(szPath, pLib, sizeof(szPath));
  95.     }
  96.  
  97.     if(pLib)  {
  98.         HINSTANCE hLibInstance = hLibInstance = LoadLibrary(pLib);
  99.  
  100. #ifdef WIN32
  101.         if(hLibInstance)
  102. #else
  103.         if(hLibInstance > (HINSTANCE)HINSTANCE_ERROR)
  104. #endif
  105.         {
  106.             DllServerFunction RegistryFunc = NULL;
  107.  
  108.             if(bRegister)  {
  109.                 (FARPROC)RegistryFunc = GetProcAddress(hLibInstance, "DllRegisterServer");
  110.             }
  111.             else    {
  112.                 (FARPROC)RegistryFunc = GetProcAddress(hLibInstance, "DllUnregisterServer");
  113.             }
  114.  
  115.             if(RegistryFunc)   {
  116.                 HRESULT hResult = (RegistryFunc)();
  117.  
  118.                 if(GetScode(hResult) == S_OK)   {
  119.                     bRetval = TRUE;
  120.                 }
  121.  
  122.                 RegistryFunc = NULL;
  123.             }
  124.             else    {
  125.                 /* If the DLL doesn't have those functions then it just doesn't support
  126.                  * self-registration. We don't consider that to be an error
  127.                  *
  128.                  * We should consider checking for the "OleSelfRegister" string in the
  129.                  * StringFileInfo section of the version information resource. If the DLL
  130.                  * has "OleSelfRegister" but doesn't have the self-registration functions
  131.                  * then that would be an error
  132.                  */
  133.                 bRetval = TRUE;
  134.             }
  135.  
  136.             FreeLibrary(hLibInstance);
  137.             hLibInstance = NULL;
  138.         }
  139.     }
  140.  
  141.     CoUninitialize();
  142.  
  143.     if(bRetval == FALSE) {
  144.         char *pMessage;
  145.  
  146.         if(bRegister == TRUE)   {
  147.             pMessage = "Registration did not succeed.";
  148.         }
  149.         else    {
  150.             pMessage = "Unregistration did not succeed.";
  151.         }
  152.  
  153.         if(pLib == NULL)    {
  154.             pLib = "Usage";
  155.             pMessage = "dllreg [/u] filename.dll";
  156.         }
  157.  
  158. #ifndef DEBUG_blythe
  159. #ifdef WIN32
  160.         printf("%s:\t%s\n", pLib, pMessage);
  161. #else
  162.         MessageBox(NULL, pMessage, pLib, MB_OK);
  163. #endif
  164. #endif
  165.     }
  166.  
  167.     if(pLib)    {
  168.         pLib = pOldLib;
  169.     }
  170.  
  171. #ifdef WIN32
  172.     return(bRetval == FALSE);
  173. #else
  174.     return(0);
  175. #endif
  176. }
  177.