RegSvr32.exe的/ n和/ i参数有什么不同?

为了注册一个COM服务器,我们运行一些类似于提升模式的东西:

regsvr32.exe com.dll 

要执行每个用户注册,请在用户帐户中执行:

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

regsvr32.exe支持这些参数:

 /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) 

在Delphi中创build一个COM服务器时,这些方法被导出:

 exports DllGetClassObject, DllCanUnloadNow, DllRegisterServer, DllUnregisterServer, DllInstall; 

我注意到这些会发生:

  1. “regsvr32.exe com.dll”调用DllRegisterServer。
  2. “regsvr32.exe / u com.dll”调用DllUnregisterServer。
  3. “regsvr32.exe / n /我:用户com.dll”调用DllInstall。
  4. “regsvr32.exe / u / n /我:用户com.dll”调用DllInstall。

我很迷惑参数/ n和/我以及DllUnregisterServer和DllInstall。 有什么不同吗?

另外,为什么“/ u / n / i:user”调用Dllinstall? 我注意到“HKEY_CURRENT_USER \ Software \ Classes”中相应的registry项被删除。

DllInstall()的文档解释了不同之处:

DllInstall仅用于应用程序安装和设置。 它不应该被应用程序调用。 它与DllRegisterserver或DllUnregisterserver类似。 与这些函数不同,DllInstall接受一个可用于指定各种不同操作的输入字符串。 这允许基于任何适当的标准以多种方式安装DLL。

要与regsvr32一起使用DllInstall,请添加一个“/ i”标志,后跟一个冒号(:)和一个字符串。 该字符串将作为pszCmdLine参数传递给DllInstall。 如果省略冒号和字符串,则pszCmdLine将被设置为NULL。 以下示例将用于安装DLL。

regsvr32 / i:“Install_1”dllname.dll

调用DllInstall时,将bInstall设置为TRUE,并将pszCmdLine设置为“Install_1”。 要卸载DLL,请使用以下命令:

regsvr32 / u / i:“Install_1”dllname.dll

与上面的两个例子,DllRegisterserver或DllUnregisterserver也将被调用。 要仅调用DllInstall,请添加“/ n”标志。

regsvr32 / n / i:“Install_1”dllname.dll

我的建议是跳过使用regsvr32.exe – 只是自己做这个工作是一件容易的事情:

 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; } 

这个特定的代码调用DllRegisterserver ,但它是微不足道的参数化它调用DllInstallDllUninstall等,如你所愿。 这消除了什么时候被调用的问题,等等。