3

To register a COM server, we run something like in elevated mode:

regsvr32.exe com.dll

To perform per-user registration, execute in user account:

regsvr32.exe /n /i:user com.dll

regsvr32.exe support these parameters:

/u - Unregister server 
/i - Call DllInstall passing it an optional [cmdline]; when used with /u calls dll uninstall 
/n - do not call DllRegisterServer; this option must be used with /i 
/s – Silent; display no message boxes (added with Windows XP and Windows Vista)

When create a COM server in Delphi, these methods were exported:

exports
  DllGetClassObject,
  DllCanUnloadNow,
  DllRegisterServer,
  DllUnregisterServer,
  DllInstall;

I notice these will happen:

  1. "regsvr32.exe com.dll" invoke DllRegisterServer.
  2. "regsvr32.exe /u com.dll" invoke DllUnregisterServer.
  3. "regsvr32.exe /n /i:user com.dll" invoke DllInstall.
  4. "regsvr32.exe /u /n /i:user com.dll" invoke DllInstall.

I am confuse with parameters /n and /i as well as DllUnregisterServer and DllInstall. Is there any different?

Also, why "/u /n /i:user" invoke Dllinstall? I notice the corresponding registry entry in "HKEY_CURRENT_USER\Software\Classes" was removed.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Chau Chee Yang
  • 18,422
  • 16
  • 68
  • 132
  • /n can *only* be used with /i so it's not "between". –  Jun 12 '12 at 03:42
  • Any different using "/n /i" and without paramters (DllRegisterServer)? When to use "/n /i" and when not using any parameters? – Chau Chee Yang Jun 12 '12 at 03:56
  • So then "When to use DllRegisterServer instead of (just) DllInstall"? –  Jun 12 '12 at 03:59
  • Or in short, when to use DllRegisterServer and when to use DllInstall? Is there any different? – Chau Chee Yang Jun 12 '12 at 04:18
  • 1
    Yes, there is a difference. DllRegisterServer() is most commonly used, but DllInstall() is more flexible because you can send parameters to it. – Remy Lebeau Jun 12 '12 at 04:50
  • The source code for regsvr32 is available. It's one of the VS sample projects. Get it here: http://msdn.microsoft.com/en-us/library/ms177531(v=vs.90).aspx – David Heffernan Jun 12 '12 at 09:52
  • Essentially it comes down to "do what the DLL author tells you to do". If it's framework provided functionality, then you'll need to check the documentation. – Deanna Jun 12 '12 at 13:55

2 Answers2

7

The documentation for DllInstall() explains the difference:

DllInstall is used only for application installation and setup. It should not be called by an application. It is similar in purpose to DllRegisterServer or DllUnregisterServer. Unlike these functions, DllInstall takes an input string which can be used to specify a variety of different actions. This allows a DLL to be installed in more than one way, based on any criteria that is appropriate.

To use DllInstall with regsvr32, add a "/i" flag followed by a colon (:) and a string. The string will be passed to DllInstall as the pszCmdLine parameter. If you omit the colon and string, pszCmdLine will be set to NULL. The following example would be used to install a DLL.

regsvr32 /i:"Install_1" dllname.dll

DllInstall is invoked with bInstall set to TRUE and pszCmdLine set to "Install_1". To uninstall a DLL, use the following:

regsvr32 /u /i:"Install_1" dllname.dll

With both of the above examples, DllRegisterServer or DllUnregisterServer will also be called. To call DllInstall only, add a "/n" flag.

regsvr32 /n /i:"Install_1" dllname.dll

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
2

My advice would be to just skip using regsvr32.exe at all -- it's about as easy to just do the job yourself:

int register(char const *DllName) { 
        HMODULE library = LoadLibrary(DllName); 
        if (NULL == library) { 
                // unable to load DLL 
                // use GetLastError() to find out why. 
                return -1;      // or a value based on GetLastError() 
        } 
        STDAPI (*DllRegisterServer)(void); 
        DllRegisterServer = GetProcAddress(library, "DllRegisterServer"); 
        if (NULL == DllRegisterServer) { 
                // DLL probably isn't a control -- it doesn't contain a 
                // DllRegisterServer function. At this point, you might 
                // want to look for a DllInstall function instead. This is 
                //  what RegSvr32 calls when invoked with '/i' 
                return -2; 
        } 
        int error; 
        if (NOERROR == (error=DllRegisterServer())) { 
                // It thinks it registered successfully. 
                return 0; 
        } 
        else 
                return error; 
} 

This particular code calls DllRegisterServer, but it's trivial to parameterize it to call DllInstall, DllUninstall, etc., as you wish. This removes any question about what gets called when, etc.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • 2
    -1 doesn't answer the question that was asked, a question that can be answered. – David Heffernan Jun 12 '12 at 07:11
  • 1
    @DavidHeffernan: You're undoubtedly right -- it would have been better to answer the question he asked than to try to actually help him instead. I should probably just be kicked off permanently for doing such a horrible thing. – Jerry Coffin Jun 12 '12 at 07:39
  • 1
    @JerryCoffin: Your answer do help me too although not direct answer to the question. I learn from your answer too by register the dll using programming technique instead of using command line. – Chau Chee Yang Jun 12 '12 at 07:51
  • Thanks, it is really helpful in my case. – Feng Deng Nov 16 '17 at 13:35