3

为了注册一个 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 中创建 COM 服务器时,导出了以下方法:

exports
  DllGetClassObject,
  DllCanUnloadNow,
  DllRegisterServer,
  DllUnregisterServer,
  DllInstall;

我注意到这些会发生:

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

我对参数 /n 和 /i 以及 DllUnregisterServer 和 DllInstall 感到困惑。有什么不同吗?

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

4

2 回答 2

7

DllInstall() 的文档解释了差异:

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

要将 DllInstall 与 regsvr32 一起使用,请添加一个“/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

于 2012-06-12T04:48:46.723 回答
2

我的建议是完全跳过使用 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,但根据需要将其参数化为 call DllInstallDllUninstall等是微不足道的。这消除了关于何时调用什么等的任何问题。

于 2012-06-12T03:44:06.990 回答