home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / controls / regsvr / regsvr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.1 KB  |  240 lines

  1. // regsvr.cpp : Program to invoke OLE self-registration on a DLL.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include <windows.h>
  14. #include <ole2.h>
  15. #include <tchar.h>
  16. #include <stdio.h>
  17. #include "resource.h"
  18.  
  19. #define FAIL_ARGS   1
  20. #define FAIL_OLE    2
  21. #define FAIL_LOAD   3
  22. #define FAIL_ENTRY  4
  23. #define FAIL_REG    5
  24.  
  25. const TCHAR _szAppName[] = _T("RegSvr32");
  26. const char _szDllRegSvr[] = "DllRegisterServer";
  27. const char _szDllUnregSvr[] = "DllUnregisterServer";
  28. HINSTANCE _hInstance;
  29.  
  30. BOOL _bSilent;
  31. BOOL _bConsole;
  32.  
  33. #define SafePutts(string) ((stdout >= 0) ? _putts(string) : 0)
  34.  
  35. void
  36. FormatString2(
  37.     LPTSTR lpszOut,
  38.     LPCTSTR lpszFormat,
  39.     LPCTSTR lpsz1,
  40.     LPCTSTR lpsz2
  41.     )
  42. {
  43.     LPCTSTR pchSrc = lpszFormat;
  44.     LPTSTR pchDest = lpszOut;
  45.     while (*pchSrc != '\0') {
  46.         if (pchSrc[0] == '%' && (pchSrc[1] >= '1' && pchSrc[1] <= '2')) {
  47.             lstrcpy(pchDest, (LPCTSTR)(pchSrc[1] == '1' ? lpsz1 : lpsz2));
  48.             pchDest += lstrlen(pchDest);
  49.             pchSrc += 2;
  50.         } else {
  51.             if (_istlead(*pchSrc))
  52.                 *pchDest++ = *pchSrc++; // copy first of 2 bytes
  53.             *pchDest++ = *pchSrc++;
  54.         }
  55.     }
  56.     *pchDest = '\0';
  57. }
  58.  
  59. #define MAX_STRING 1024
  60.  
  61. void
  62. DisplayMessage(
  63.     UINT ids,
  64.     LPCTSTR pszArg1 = NULL,
  65.     LPCTSTR pszArg2 = NULL,
  66.     BOOL bUsage = FALSE,
  67.     BOOL bInfo = FALSE
  68.     )
  69. {
  70.     if (_bSilent && !_bConsole)
  71.         return;
  72.  
  73.     TCHAR szFmt[MAX_STRING];
  74.     LoadString(_hInstance, ids, szFmt, MAX_STRING);
  75.  
  76.     TCHAR szText[MAX_STRING];
  77.     FormatString2(szText, szFmt, pszArg1, pszArg2);
  78.     if (bUsage) {
  79.         int cch = _tcslen(szText);
  80.         LoadString(_hInstance, IDS_USAGE, szText + cch, MAX_STRING - cch);
  81.     }
  82.  
  83.     if (! _bSilent)
  84.         MessageBox(NULL, szText, _szAppName,
  85.             MB_TASKMODAL | (bInfo ? MB_ICONINFORMATION : MB_ICONEXCLAMATION));
  86.  
  87.     if (_bConsole) {
  88.         TCHAR szMessage[MAX_STRING];
  89.         FormatString2(szMessage, _T("%1: %2\n"), _szAppName, szText);
  90.         SafePutts(szMessage);
  91.     }
  92. }
  93.  
  94. inline void
  95. Usage(
  96.     UINT ids,
  97.     LPCTSTR pszArg1 = NULL,
  98.     LPCTSTR pszArg2 = NULL
  99.     )
  100. {
  101.     DisplayMessage(ids, pszArg1, pszArg2, TRUE);
  102. }
  103.  
  104. inline void
  105. Info(
  106.     UINT ids,
  107.     LPCTSTR pszArg1 = NULL,
  108.     LPCTSTR pszArg2 = NULL
  109.     )
  110. {
  111.     DisplayMessage(ids, pszArg1, pszArg2, FALSE, TRUE);
  112. }
  113.  
  114. int PASCAL
  115. WinMain(
  116.     HINSTANCE hInstance,
  117.     HINSTANCE,
  118.     LPSTR,
  119.     int
  120.     )
  121. {
  122.     int iReturn = 0;
  123.     HRESULT (STDAPICALLTYPE * lpDllEntryPoint)(void);
  124.  
  125.     BOOL bVisualC = FALSE;
  126.     BOOL bUnregister = FALSE;
  127.     LPCSTR pszDllEntryPoint = _szDllRegSvr;
  128.     LPCSTR pszDllName = NULL;
  129.     LPCSTR pszTok;
  130.  
  131.     _hInstance = hInstance;
  132.  
  133.     // Parse command line arguments.
  134.     int iTok;
  135.     for (iTok = 1; iTok < __argc; iTok++) {
  136.         pszTok = __argv[iTok];
  137.  
  138.         if ((pszTok[0] == '-') || (pszTok[0] == '/')) {
  139.             switch (pszTok[1]) {
  140.                 case 'v':
  141.                 case 'V':
  142.                     bVisualC = TRUE;
  143.                     break;
  144.  
  145.                 case 's':
  146.                 case 'S':
  147.                     _bSilent = TRUE;
  148.                     break;
  149.  
  150.                 case 'u':
  151.                 case 'U':
  152.                     bUnregister = TRUE;
  153.                     pszDllEntryPoint = _szDllUnregSvr;
  154.                     break;
  155.  
  156.                 case 'c':
  157.                 case 'C':
  158.                     _bConsole = TRUE;
  159.                     break;
  160.  
  161.                 default:
  162.                     Usage(IDS_UNRECOGNIZEDFLAG, pszTok);
  163.                     return FAIL_ARGS;
  164.             }
  165.         } else {
  166.             if (pszDllName == NULL) {
  167.                 pszDllName = pszTok;
  168.                 break;
  169.             } else {
  170.                 Usage(IDS_EXTRAARGUMENT, pszTok);
  171.                 return FAIL_ARGS;
  172.             }
  173.         }
  174.     }
  175.  
  176.     if (pszDllName == NULL) {
  177.         if (bVisualC)
  178.             DisplayMessage(IDS_NOPROJECT);
  179.         else
  180.             Usage(IDS_NODLLNAME);
  181.  
  182.         return FAIL_ARGS;
  183.     }
  184.  
  185.     // Initialize OLE.
  186.     if (FAILED(OleInitialize(NULL))) {
  187.         DisplayMessage(IDS_OLEINITFAILED);
  188.         return FAIL_OLE;
  189.     }
  190.  
  191.     SetErrorMode(SEM_FAILCRITICALERRORS);       // Make sure LoadLib fails.
  192.     for (; iTok < __argc; iTok++) {
  193.         pszDllName = __argv[iTok];
  194.  
  195.         // Load the library.
  196.         HINSTANCE hLib = LoadLibraryEx(pszDllName, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  197.  
  198.         if (hLib < (HINSTANCE)HINSTANCE_ERROR) {
  199.             TCHAR szError[12];
  200.             wsprintf(szError, _T("0x%08lx"), GetLastError());
  201.             DisplayMessage(IDS_LOADLIBFAILED, pszDllName, szError);
  202.             iReturn = FAIL_LOAD;
  203.             goto CleanupOle;
  204.         }
  205.  
  206.         // Find the entry point.
  207.         (FARPROC&)lpDllEntryPoint = GetProcAddress(hLib, pszDllEntryPoint);
  208.  
  209.         if (lpDllEntryPoint == NULL) {
  210.             TCHAR szExt[_MAX_EXT];
  211.             _tsplitpath(pszDllName, NULL, NULL, NULL, szExt);
  212.  
  213.             if ((_stricmp(szExt, ".dll") != 0) && (_stricmp(szExt, ".ocx") != 0))
  214.                 DisplayMessage(IDS_NOTDLLOROCX, pszDllName, pszDllEntryPoint);
  215.             else
  216.                 DisplayMessage(IDS_NOENTRYPOINT, pszDllName, pszDllEntryPoint);
  217.  
  218.             iReturn = FAIL_ENTRY;
  219.             goto CleanupLibrary;
  220.         }
  221.  
  222.         // Call the entry point.
  223.         if (FAILED((*lpDllEntryPoint)())) {
  224.             DisplayMessage(IDS_CALLFAILED, pszDllEntryPoint, pszDllName);
  225.             iReturn = FAIL_REG;
  226.             goto CleanupLibrary;
  227.         }
  228.  
  229.         Info(IDS_CALLSUCCEEDED, pszDllEntryPoint, pszDllName);
  230.  
  231. CleanupLibrary:
  232.         FreeLibrary(hLib);
  233.     }
  234.  
  235. CleanupOle:
  236.     OleUninitialize();
  237.  
  238.     return iReturn;
  239. }
  240.