home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap05 / selfreg / selfreg.cpp < prev    next >
C/C++ Source or Header  |  1995-05-03  |  4KB  |  178 lines

  1. /*
  2.  * SELFREG.CPP
  3.  * Server Self-Registrtation Utility, Chapter 5
  4.  *
  5.  * Copyright (c)1993-1995 Microsoft Corporation, All Rights Reserved
  6.  *
  7.  * Kraig Brockschmidt, Microsoft
  8.  * Internet  :  kraigb@microsoft.com
  9.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  10.  */
  11.  
  12.  
  13. #define INITGUIDS
  14. #include "selfreg.h"
  15.  
  16.  
  17. /*
  18.  * WinMain
  19.  *
  20.  * Purpose:
  21.  *  Main entry point of application.
  22.  */
  23.  
  24. int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hInstPrev
  25.     , LPSTR pszCmdLine, int nCmdShow)
  26.     {
  27.     BOOL        fEXE=FALSE;
  28.     BOOL        fUnreg=FALSE;
  29.     LPSTR       psz;
  30.     char        ch;
  31.  
  32.     /*
  33.      * Walk down the command line looking for /u and /e in
  34.      * any order.  The first non-whitespace character after
  35.      * whitespace that is not a "/" is assumed to be the
  36.      * beginning of the server filename.  This is all done
  37.      * in ANSI because pszCmdLine is in ANSI.
  38.      */
  39.  
  40.     psz=pszCmdLine;
  41.  
  42.     while (ch=*psz)
  43.         {
  44.         BOOL    fStop=FALSE;
  45.  
  46.         switch (ch)
  47.             {
  48.             case '\t':
  49.             case '\n':
  50.             case '\r':
  51.             case ' ':
  52.                 //Scan for next non-whitespace and continue
  53.                 psz=PszWhiteSpaceScan(psz, TRUE);
  54.                 continue;
  55.  
  56.             case '/':
  57.                 /*
  58.                  * Check what flag this is, then skip to next
  59.                  * whitespace.
  60.                  */
  61.                 ch=*(++psz);
  62.                 if ('u'==ch)
  63.                     fUnreg=TRUE;
  64.  
  65.                 if ('e'==ch)
  66.                     fEXE=TRUE;
  67.  
  68.                 psz=PszWhiteSpaceScan(psz, FALSE);
  69.                 continue;
  70.  
  71.             default:
  72.                 fStop=TRUE;
  73.                 break;
  74.             }
  75.  
  76.         if (fStop)
  77.             break;
  78.  
  79.         psz++;
  80.         }
  81.  
  82.  
  83.     if (0==ch)
  84.         return 0;   //Nothing to do
  85.  
  86.  
  87.     /*
  88.      * Now launch and EXE or load a DLL and tell them to do
  89.      * their registration or unregistartion.
  90.      */
  91.     if (fEXE)
  92.         {
  93.         char    szEXE[512];
  94.  
  95.         wsprintfA(szEXE, "%s %s", psz
  96.             , fUnreg ? "/UNREGSERVER" : "/REGSERVER");
  97.         WinExec(szEXE, SW_HIDE);
  98.  
  99.         MessageBoxA(NULL, fUnreg
  100.             ? "EXE unregistration started"
  101.             : "EXE registration started.", "SelfReg", MB_OK);
  102.         }
  103.     else
  104.         {
  105.         HINSTANCE       hMod;
  106.  
  107.         //Do this for the sake of DLLs.
  108.         if (FAILED(CoInitialize(NULL)))
  109.             return 0;
  110.  
  111.         hMod=LoadLibraryA(psz);
  112.  
  113.         if (hMod > (HINSTANCE)HINSTANCE_ERROR)
  114.             {
  115.             HRESULT (STDAPICALLTYPE *pfn)(void);
  116.             BOOL    fRes=FALSE;
  117.  
  118.             if (fUnreg)
  119.                 {
  120.                 (FARPROC&)pfn=GetProcAddress(hMod
  121.                     , "DllUnregisterServer");
  122.  
  123.                 if (NULL!=pfn)
  124.                     fRes=SUCCEEDED((*pfn)());
  125.  
  126.                 MessageBoxA(NULL, fRes
  127.                     ? "DLL unregistration succeeded."
  128.                     : "DLL unregistration failed.", "SelfReg", MB_OK);
  129.                 }
  130.             else
  131.                 {
  132.                 (FARPROC&)pfn=GetProcAddress(hMod
  133.                     , "DllRegisterServer");
  134.  
  135.                 if (NULL!=pfn)
  136.                     fRes=SUCCEEDED((*pfn)());
  137.  
  138.                 MessageBoxA(NULL , fRes
  139.                     ? "DLL registration succeeded."
  140.                     : "DLL registration failed.", "SelfReg", MB_OK);
  141.                 }
  142.  
  143.             CoFreeLibrary(hMod);
  144.             CoUninitialize();
  145.             }
  146.         else
  147.             {
  148.             MessageBoxA(NULL, "LoadLibrary failed.", "SelfReg"
  149.                 , MB_OK);
  150.             }
  151.         }
  152.  
  153.     return 1;
  154.     }
  155.  
  156.  
  157.  
  158.  
  159. LPSTR PszWhiteSpaceScan(LPSTR psz, BOOL fSkip)
  160.     {
  161.     char        ch;
  162.     BOOL        fWhite;
  163.  
  164.     while (ch=*psz)
  165.         {
  166.         fWhite=('\n'==ch || '\r'==ch
  167.             || '\t'==ch || ' '==ch);
  168.  
  169.         //Too bad C doesn't have a logical XOR (^^) operator.
  170.         if ((fSkip && !fWhite) || (!fSkip && fWhite))
  171.             break;
  172.  
  173.         psz++;
  174.         }
  175.  
  176.     return psz;
  177.     }
  178.