Click here to Skip to main content
6,634,665 members and growing! (16,519 online)
Email Password   helpLost your password?
Desktop Development » Shell and IE programming » General     Intermediate

DLL Registration / Unregistration using a Shell Extension

By Sardaukar

Why are we still using command prompt to register a DLL and not using the Windows Explorer?
VC6, Dev
Posted:30 Nov 1999
Views:108,568
Bookmarked:40 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
23 votes for this article.
Popularity: 6.13 Rating: 4.50 out of 5

1

2

3
1 vote, 50.0%
4
1 vote, 50.0%
5
  • Download demo project - 22 Kb
  • Download source files - 13 Kb

    Sample Image

    This time is about shell extensions. Because I am tired to open a console every time when I want to register the Debug dll as shell extension, I was thinking that's a good idea to right-click the dll to register/unregister.

    So here we are! (as stupid Dexters's sister, DeeDee, said once). You will find here few things you cannot find in MSDN or in other articles.

    1. GUID. You can obtain this sinister number running one of the uuidgen or guidgen tools. This is CLSID_DLLRegisterer.

    2. The main file, where the DLL's entry point DllMain resides, is dllregshex.cpp. I prefer to load resources at start and free them at end, especially when are in little number. So, the routines _LoadResources and _UnloadResources do this. The other two interesting routines are DllRegisterServer and DllUnregisterServer. If you use one, be polite to the user and use the twin (I have seen may Reg, but few Unreg). That's when you did not choose another way (as a .reg file, for example, or separate install/uninstall programs).

    So, what's happening registering a dll as shell context extension? You have to create the entry in CLSID key and to create under this the 'samename' key and the thread model key (I read somewhere that only 'Apartment' is accepted for context extensions - is really true?).

    STDAPI 
    DllRegisterServer(void)
    {
      HINSTANCE hInst = g_hmodThisDll;
    
      int      i;
      HKEY     hKey;
      LRESULT  lResult;
      DWORD    dwDisp;
      TCHAR    szSubKey[MAX_PATH];
      TCHAR    szCLSID[MAX_PATH];
      TCHAR    szModule[MAX_PATH];
      LPWSTR   pwszShellExt;
    
      StringFromIID(CLSID_DLLRegisterer, &pwszShellExt);
    
      if (pwszShellExt) 
      {
        WideCharToLocal(szCLSID, pwszShellExt, ARRAYSIZE(szCLSID));
    
        LPMALLOC pMalloc;
        CoGetMalloc(1, &pMalloc);
        if(pMalloc)
        {
          pMalloc->Free(pwszShellExt);
          pMalloc->Release();
        }
      }                                                
    
      GetModuleFileName(hInst, szModule, ARRAYSIZE(szModule));
      REGSTRUCT ShExClsidEntries[] = 
      {
        HKEY_CLASSES_ROOT, TEXT("CLSID\\%s"), NULL, TEXT(DLLREGUNREGNAME),
        HKEY_CLASSES_ROOT, TEXT("CLSID\\%s\\InProcServer32"), NULL, TEXT("%s"),
        HKEY_CLASSES_ROOT, TEXT("CLSID\\%s\\InProcServer32"),
    	  TEXT("ThreadingModel"), TEXT("Apartment"), NULL, NULL, NULL, NULL
      };
    
      for(i = 0; ShExClsidEntries[i].hRootKey; i++) 
      {
        wsprintf(szSubKey, ShExClsidEntries[i].lpszSubKey, szCLSID);
        lResult = RegCreateKeyEx(ShExClsidEntries[i].hRootKey, szSubKey, 0, NULL,
          REG_OPTION_NON_VOLATILE,  KEY_WRITE, NULL,
          &hKey, &dwDisp);
    
        if(NOERROR == lResult) 
        {
          TCHAR szData[MAX_PATH];
          wsprintf(szData, ShExClsidEntries[i].lpszData, szModule);
          lResult = RegSetValueEx(hKey, ShExClsidEntries[i].lpszValueName, 0,
            REG_SZ, 
            (LPBYTE)szData, lstrlen(szData) + 1);
          RegCloseKey(hKey);
        } 
        else
          return SELFREG_E_CLASS;
        }
    The other entries bind the desired extension (in our case, the DLLs) to our CLSID:
      REGSTRUCT OtherShExEntries[] = 
      {
        HKEY_LOCAL_MACHINE, TEXT("software\\classes\\clsid\\"DLLREGUNREGNAME),
        NULL,   TEXT("%s"),
        HKEY_CLASSES_ROOT,
    	TEXT("dllfile\\shellex\\ContextMenuHandlers\\"DLLREGUNREGNAME),
    	NULL, TEXT("%s"), NULL, NULL, NULL, NULL
      };
    
      for (i = 0; OtherShExEntries[i].hRootKey; i++) 
      {
        wsprintf(szSubKey, OtherShExEntries[i].lpszSubKey, szCLSID);
        lResult = RegCreateKeyEx(OtherShExEntries[i].hRootKey, szSubKey, 0, NULL, 
          REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, &dwDisp);
    
        if(NOERROR == lResult) 
        {
          TCHAR szData[MAX_PATH];
          wsprintf(szData, OtherShExEntries[i].lpszData, szCLSID);
          lResult = RegSetValueEx(hKey, OtherShExEntries[i].lpszValueName, 0,
    	  REG_SZ,
            (LPBYTE)szData, lstrlen(szData) + 1);
          RegCloseKey(hKey);
        } 
        else
          return SELFREG_E_CLASS;
      }
    A special mention when Windows is NT: register the extension as 'Approved':
      OSVERSIONINFO  osvi;
      osvi.dwOSVersionInfoSize = sizeof(osvi);
      GetVersionEx(&osvi);
      if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId)  
      {
        lstrcpy( szSubKey,
        TEXT(
          "Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved"
    	));
        lResult = RegCreateKeyEx(HKEY_LOCAL_MACHINE, szSubKey, 0, NULL,
          REG_OPTION_NON_VOLATILE, KEY_WRITE,
          NULL, &hKey, &dwDisp);
        if(NOERROR == lResult) 
        {
          TCHAR szData[MAX_PATH];
          lstrcpy(szData, DLLREGUNREGNAME);
          lResult = RegSetValueEx(hKey, szCLSID, 0, REG_SZ, (LPBYTE)szData,
    	  lstrlen(szData) + 1);
          RegCloseKey(hKey);
        } 
        else
          return SELFREG_E_CLASS;
      }
    
      return S_OK;
    }
    The unregister routine performs the inverse way. The rest is history: AddRef, Release, QueryInterface.
    3. The utils file (dllreg_util.cpp) contains conversion routines. This UNICODE job really kills me, I simply copied one of the 1000 other ways to deal with it.
    4. The menu file (dllreg_ctxm.cpp) contains 3 standard routines: QueryContextMenu, which builds the added popup menu and his options, InvokeCommand and GetCommandString - this will show you a simple text on Explorer' status bar. Nothing unusual.
    #include "dllreg_xhdr.h"
    
    #include "dllregshex.h"
    
    #include "dllreg_util.h"
    
    #include "resource.h"
    
    
    extern UINT      g_cRefThisDll;
    extern HINSTANCE   g_hmodThisDll;
    
    extern HBITMAP      hBmp_Install;
    extern HBITMAP      hBmp_Uninstall;
    extern HBITMAP      hBmp_About;
    extern HMENU      hSubMenu;
    
    // IContextMenu
    
    STDMETHODIMP 
    CShellExt::QueryContextMenu(HMENU hMenu, UINT indexMenu, UINT idCmdFirst,
      UINT idCmdLast, UINT uFlags)
    {
      _UNUSED_PARAMETER(idCmdLast);
      UINT idCmd = idCmdFirst;
    
      char *szMenuText_Popup   = "Shell e&xtension";
      char *szMenuText_Install   = "&Install";
      char *szMenuText_Uninstall   = "&Uninstall";
      char *szMenuText_About   = "&About...";
    
      BOOL bAppendItems = TRUE;
    
      if((uFlags & 0x000F) == CMF_NORMAL)
        bAppendItems = TRUE;
      else if (uFlags & CMF_VERBSONLY)
        bAppendItems = TRUE;
      else if (uFlags & CMF_EXPLORE)
        bAppendItems = TRUE;
      else
        bAppendItems = FALSE;
    
      if(bAppendItems)
      {
        InsertMenu(hMenu, indexMenu++, MF_SEPARATOR | MF_BYPOSITION, 0, NULL);  
    
        HMENU hSubMenu = CreateMenu();
        if(hSubMenu)
        {
          InsertMenu(hSubMenu, 0, MF_STRING    | MF_BYPOSITION, idCmd++,
    	    szMenuText_Install);
          SetMenuItemBitmaps(hSubMenu, 0, MF_BYPOSITION, hBmp_Install,
    	    hBmp_Install);
          InsertMenu(hSubMenu, 1, MF_STRING | MF_BYPOSITION, idCmd++,
    	    szMenuText_Uninstall);
          SetMenuItemBitmaps(hSubMenu, 1, MF_BYPOSITION, hBmp_Uninstall,
    	    hBmp_Uninstall);
    
          InsertMenu(hSubMenu, 2, MF_SEPARATOR | MF_BYPOSITION, 0, NULL);
    
          InsertMenu(hSubMenu, 3, MF_STRING    | MF_BYPOSITION, idCmd++,
    	    szMenuText_About);
          SetMenuItemBitmaps(hSubMenu, 3, MF_BYPOSITION, hBmp_About, hBmp_About);
        }
         
        InsertMenu(hMenu, indexMenu++, MF_STRING | MF_POPUP | MF_BYPOSITION, 
          (UINT_PTR)hSubMenu, szMenuText_Popup);
        InsertMenu(hMenu, indexMenu++, MF_SEPARATOR | MF_BYPOSITION, 0, NULL);
    
        return ResultFromShort(idCmd - idCmdFirst);
      }
      
      return NOERROR;
    }
    
    STDMETHODIMP 
    CShellExt::InvokeCommand(LPCMINVOKECOMMANDINFO lpcmi)
    {
      HRESULT hr = E_INVALIDARG;
      if (!HIWORD(lpcmi->lpVerb))
      {
        UINT idCmd = LOWORD(lpcmi->lpVerb);
        switch (idCmd)
        {
          case 0:
            hr = DoInstall(lpcmi->hwnd, lpcmi->lpDirectory,
    		  lpcmi->lpVerb, lpcmi->lpParameters, lpcmi->nShow);
            break;
          case 1:
            hr = DoUninstall(lpcmi->hwnd, lpcmi->lpDirectory,
    		  lpcmi->lpVerb, lpcmi->lpParameters, lpcmi->nShow);
            break;
          case 2:
            hr = DoAbout(lpcmi->hwnd, lpcmi->lpDirectory,
    		  lpcmi->lpVerb, lpcmi->lpParameters, lpcmi->nShow);
            break;
          default:
            break;
        }
      }
      return hr;
    }
    
    STDMETHODIMP 
    CShellExt::GetCommandString(UINT idCmd, UINT uFlags, UINT FAR *reserved,
      LPSTR pszName, UINT cchMax)
    {
      _UNUSED_PARAMETER(reserved);
      _UNUSED_PARAMETER(uFlags);
    
      *pszName = 0;
      cchMax   = 40;
      char psz[40];
    
      switch (idCmd)
      {
        case 0:
          LoadString(g_hmodThisDll, IDCMD_INSTALL, psz, cchMax);
          break;
        case 1:
          LoadString(g_hmodThisDll, IDCMD_UNINSTALL, psz, cchMax);
          break;
        case 2:
          LoadString(g_hmodThisDll, IDCMD_ABOUT, psz, cchMax);
          break;
        default:
          break;
      }
      wcscpy((unsigned short *)pszName, _WCSTR(psz));
      return NOERROR;
    }
    5. The most interesting stuff is contained in file dllreg_job.cpp, and this is the _GetFullFileName function. The others are handled in one routine, which simply format a command line using the regsvr32.exe tool (_DoRegisterJob)
    STDMETHODIMP 
    CShellExt::_GetFullFileName()
    {
      HRESULT hr = S_FALSE;
    
      //
    
      // IEnumFORMATETC. Needed for format enumeration.
    
      //
    
      IEnumFORMATETC *pefEtc = 0;
      hr = m_pDataObj->EnumFormatEtc(DATADIR_GET, &pefEtc);
      if(SUCCEEDED(hr))
      {
        hr = pefEtc->Reset();   //   Reset enumeration.
    
    
        if(SUCCEEDED(hr))
        {
          //
    
          //   FORMATETC. Needed for get data about object.
    
          //
    
          FORMATETC fEtc;
          ULONG ulFetched = 0L;
    
          while(TRUE)
          {
            hr = pefEtc->Next(1, &fEtc, &ulFetched);
            if(FAILED(hr) || (ulFetched <= 0))
              break;
    
            //
    
            //   'Arm' format and 'launch' to obtain STGMEDIUM...
    
            //
    
            fEtc.cfFormat   = CF_HDROP;
            fEtc.dwAspect   = DVASPECT_CONTENT;
            fEtc.lindex      = -1;
            fEtc.ptd      = NULL;
            fEtc.tymed      = TYMED_HGLOBAL;
            //
    
            //   IDataObject : GetData. Returned as TYMED_HGLOBAL.
    
            //
    
            STGMEDIUM stgM;
            hr = m_pDataObj->GetData(&fEtc, &stgM);
            if(SUCCEEDED(hr))
            {
              if(stgM.tymed == TYMED_HGLOBAL)
              {
                m_ppszFileUserClickedOn[0] = '\0';
    
                UINT nFileCount = DragQueryFile((HDROP)stgM.hGlobal,
                  (UINT)INVALID_HANDLE_VALUE, NULL, 0);
                if(nFileCount >= 1)
                {
                  if(m_ppszFileUserClickedOn)
                  {
                    DeleteFileData();
                  }
    
                  m_ppszFileUserClickedOn = new LPTSTR[nFileCount];
    
                  for(register UINT x = 0; x < nFileCount; x++)
                  {
                    m_ppszFileUserClickedOn[x] = new TCHAR[_MAX_PATH + 1];
                    // one file only; eliminate this and improve code to allow
    
                    //  multiple-DLL file (un)registering
    
                    DragQueryFile((HDROP)stgM.hGlobal, x,
                      m_ppszFileUserClickedOn[x], _MAX_PATH + 1);
                  }
    
                  m_xFileCount = nFileCount;
                }
              }
            }
          }
        }
      }
    
      if(pefEtc)
        pefEtc->Release();
      return hr;
    }
  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Sardaukar


    Member

    Occupation: Web Developer
    Location: Romania Romania

    Other popular Shell and IE programming articles:

    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 18 of 18 (Total in Forum: 18) (Refresh)FirstPrevNext
    Generalmore simple... Pinmembermayti3:13 14 Sep '05  
    GeneralGood Job PinmemberMark Philip Henri14:09 19 Jul '03  
    GeneralRe: Good Job PinmemberSardaukar23:17 19 Jul '03  
    GeneralRe: Good Job PineditorHeath Stewart4:23 26 Jan '04  
    GeneralOLE2! Command Failed PinmemberAnonymous23:03 10 Mar '02  
    GeneralEasier Method PinmemberTodd Smith17:45 4 Mar '02  
    GeneralRe: Easier Method PinmemberCristian Amarie21:22 4 Mar '02  
    GeneralI don't understand Pinmemberlinghuchong3:10 15 Jan '01  
    GeneralCan you tell me? Pinmemberlinghuchong20:57 12 Jan '01  
    GeneralThis would be cool if it worked. PinmemberJim A. Johnson8:57 9 Jan '01  
    GeneralRe: This would be cool if it worked. PinmemberAnonymous3:06 2 Jul '01  
    GeneralRe: This would be cool if it worked. PinmemberVolki15:39 12 Mar '02  
    GeneralHow to register my own DLL! PinsussWilliam12:44 21 Aug '00  
    GeneralRe: How to register my own DLL! PinsussSardaukar21:02 21 Aug '00  
    GeneralDoes this work for Solidworks DLL? PinsussHappy19:20 31 Jul '00  
    GeneralRe: Does this work for Solidworks DLL? PinsussSardaukar1:27 6 Aug '00  
    GeneralVery nice! PinsussJonathan Gilligan9:31 10 Mar '00  
    GeneralRe: Very nice! PinsussSardaukar0:42 12 Mar '00  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 30 Nov 1999
    Editor: Heath Stewart
    Copyright 1999 by Sardaukar
    Everything else Copyright © CodeProject, 1999-2009
    Web20 | Advertise on the Code Project