Author Topic: Register and Unregister COM DLLs  (Read 5734 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3510
    • ScriptBasic Open Source Project
Register and Unregister COM DLLs
« on: October 31, 2015, 07:27:33 PM »
Microsoft Knowledge Base Article


Code: C++
  1. #include <tchar.h>
  2. #include <afxole.h>
  3. #include <stdlib.h>
  4.  
  5. #define FAIL_ARGS    1
  6. #define FAIL_OLE     2
  7. #define FAIL_LOAD    3
  8. #define FAIL_ENTRY   4
  9. #define FAIL_REG     5
  10.  
  11. static char szAppName[] = "Register";
  12. static char szUsage[] = "\n\nUsage: Register [/u] dllname";
  13. static char szDllRegSvr[] = "DllRegisterServer";
  14. static char szDllUnregSvr[] = "DllUnregisterServer";
  15.  
  16. int PASCAL WinMain(
  17.                    HINSTANCE hInstance,
  18.                    HINSTANCE hPrev,
  19.                    LPSTR pszCmdLine,
  20.                    int nCmdShow)
  21. {  
  22.         int iReturn = 0;      
  23.         HRESULT (FAR STDAPICALLTYPE * lpDllEntryPoint)(void);  
  24.         static TCHAR szMsgBuffer[_MAX_PATH*4];        
  25.         BOOL bVisualC = FALSE;
  26.         BOOL bSilent = FALSE;  
  27.         BOOL bUnregister = FALSE;      
  28.         LPSTR pszDllEntryPoint = szDllRegSvr;  
  29.         LPSTR pszDllName = NULL;
  30.         char szCmdLineCopy[_MAX_PATH];
  31.         strcpy(szCmdLineCopy, pszCmdLine);            
  32.         LPSTR pszTmp = szCmdLineCopy;  
  33.         LPSTR pszTok;          
  34.        
  35.         while ((pszTok = strtok(pszTmp, " \t")) != NULL)      
  36.         {                                
  37.                 pszTmp = NULL;
  38.                
  39.                 if ((pszTok[0] == '-') || (pszTok[0] == '/'))
  40.                 {      
  41.                         switch (pszTok[1])    
  42.                         {      
  43.                         case 'v':      
  44.                         case 'V':
  45.                                 bVisualC = TRUE;
  46.                                 break;        
  47.                         case 's':      
  48.                         case 'S':
  49.                                 bSilent = TRUE;
  50.                                 break;        
  51.                         case 'u':      
  52.                         case 'U':
  53.                                 bUnregister = TRUE;
  54.                                 pszDllEntryPoint = szDllUnregSvr;
  55.                                 break;        
  56.                         default:
  57.                                 wsprintf(szMsgBuffer,
  58.                                         "Unrecognized flag: %s%s",
  59.                                         pszTok,
  60.                                         (LPCSTR)szUsage);
  61.                                 if (!bSilent)  
  62.                                         MessageBox(NULL,
  63.                                            szMsgBuffer,
  64.                                            szAppName,
  65.                                            MB_TASKMODAL | MB_ICONEXCLAMATION);
  66.                                 return FAIL_ARGS;      
  67.                         }
  68.                 }
  69.                 else
  70.                 {      
  71.                         if (pszDllName == NULL)
  72.                                 pszDllName = pszTok;  
  73.                         else  
  74.                         {
  75.                                 wsprintf(szMsgBuffer,
  76.                                         "Extra argument on command line: %s%s",
  77.                                         pszTok,
  78.                                         (LPCSTR)szUsage);
  79.                                 if (!bSilent)  
  80.                                         MessageBox(NULL,
  81.                                            szMsgBuffer,
  82.                                            szAppName,
  83.                                            MB_TASKMODAL | MB_ICONEXCLAMATION);
  84.                                 return FAIL_ARGS;      
  85.                         }
  86.                 }      
  87.         }      
  88.        
  89.         if (pszDllName == NULL)        
  90.         {
  91.                 if (!bSilent)
  92.                 {      
  93.                         if (bVisualC)  
  94.                         {
  95.                                 MessageBox(NULL,
  96.                                 "This command is only valid when "
  97.                                 "an OLE Custom Control project is open.",
  98.                                 bUnregister ?
  99.                                 "Unregister Control" : "Register Control",
  100.                                 MB_TASKMODAL | MB_ICONEXCLAMATION);
  101.                         }
  102.                         else  
  103.                         {
  104.                                 wsprintf(szMsgBuffer,
  105.                                         _T("No DLL name specified%s"),
  106.                                         (LPCSTR)szUsage);
  107.                                 MessageBox(NULL,
  108.                                         szMsgBuffer,
  109.                                         szAppName,
  110.                                         MB_TASKMODAL | MB_ICONEXCLAMATION);
  111.                         }
  112.                 }
  113.                 return FAIL_ARGS;      
  114.         }
  115.        
  116.         if (FAILED(OleInitialize(NULL)))      
  117.         {
  118.                 if (!bSilent)  
  119.                         MessageBox(NULL,
  120.                                 "OleInitialize failed.",
  121.                                 szAppName,
  122.                                 MB_TASKMODAL | MB_ICONINFORMATION);
  123.                 return FAIL_OLE;      
  124.         }              
  125.        
  126.         HINSTANCE hLib = LoadLibrary(pszDllName);      
  127.        
  128.         if (hLib < (HINSTANCE)HINSTANCE_ERROR)
  129.         {
  130.                 wsprintf(szMsgBuffer,
  131.                         "LoadLibary(\"%s\") failed.",
  132.                         pszDllName);
  133.                 MessageBox(NULL,
  134.                         szMsgBuffer,
  135.                         szAppName,
  136.                         MB_TASKMODAL | MB_ICONEXCLAMATION);
  137.                 iReturn = FAIL_LOAD;
  138.                 goto CleanupOle;      
  139.         }
  140.        
  141.         (FARPROC&)lpDllEntryPoint = GetProcAddress(hLib, pszDllEntryPoint);
  142.        
  143.         if (lpDllEntryPoint == NULL)  
  144.         {
  145. #ifdef _WIN32
  146.                 int nLen = strlen(pszDllName);
  147.                 if ((nLen > 4) &&
  148.                         (stricmp(pszDllName + nLen - 4, ".dll") != 0)  &&
  149.                         (stricmp(pszDllName + nLen - 4, ".ocx") != 0))
  150.                 {      
  151.                         wsprintf(szMsgBuffer,
  152.                         "%s was loaded, but the %s entry point "
  153.                         "was not found. %s does not appear to be "
  154.                         "an .DLL or .OCX file.",
  155.                         pszDllName,
  156.                         pszDllEntryPoint,
  157.                         pszDllName);
  158.                 }
  159.                 else
  160.                 {      
  161.                         wsprintf(szMsgBuffer,
  162.                         "%s was loaded, but the %s entry point "
  163.                         "was not found. %s may not be exported, "
  164.                         "or a corrupt version may be in memory.  "
  165.                         "Consider using PView to detect and remove it.",
  166.                         pszDllName,
  167.                         pszDllEntryPoint,
  168.                         pszDllEntryPoint);
  169.                 }
  170. #else
  171.                 wsprintf(szMsgBuffer,
  172.                 "%s was loaded, but the %s entry point "
  173.                 "was not found. %s may not be exported, "
  174.                 "or a corrupt version may be in memory.  "
  175.                 "Consider using WPS to detect and remove it.",
  176.                 pszDllName,
  177.                 pszDllEntryPoint,
  178.                 pszDllEntryPoint);
  179. #endif
  180.                
  181.                 if (!bSilent)  
  182.                         MessageBox(NULL,
  183.                                 szMsgBuffer,
  184.                                 szAppName,
  185.                                 MB_TASKMODAL | MB_ICONEXCLAMATION);
  186.                 iReturn = FAIL_ENTRY;
  187.                
  188.                 goto CleanupLibrary;  
  189.         }
  190.        
  191.         if (FAILED((*lpDllEntryPoint)()))      
  192.         {
  193.                 wsprintf(szMsgBuffer,
  194.                         "%s in %s failed.",
  195.                         pszDllEntryPoint,
  196.                         pszDllName);
  197.                
  198.                 if (!bSilent)  
  199.                         MessageBox(NULL,
  200.                                 szMsgBuffer,
  201.                                 szAppName,
  202.                                 MB_TASKMODAL | MB_ICONEXCLAMATION);
  203.                 iReturn = FAIL_REG;
  204.                
  205.                 goto CleanupLibrary;  
  206.         }      
  207.        
  208.         wsprintf(szMsgBuffer,
  209.                 "%s in %s succeeded.",
  210.                 pszDllEntryPoint,
  211.                 pszDllName);    
  212.        
  213.         if (! bSilent)
  214.                 MessageBox(NULL,
  215.                         szMsgBuffer,
  216.                         szAppName,
  217.                         MB_TASKMODAL | MB_ICONINFORMATION);
  218. CleanupLibrary:    
  219.         FreeLibrary(hLib);
  220.        
  221. CleanupOle:
  222.         OleUninitialize();    
  223.        
  224.         return iReturn;
  225. }
  226.