C# 如何检查DLL文件是否已注册?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/689072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 13:34:35  来源:igfitidea点击:

How to check if a DLL file is registered?

c#.net

提问by

How do I find whether a DLL file written in C# is registered or not programmatically?

如何以编程方式查找用 C# 编写的 DLL 文件是否已注册?

I already tried this code, but it doesn't come off.

我已经尝试过这段代码,但它没有脱落。

If I register a DLL file and check using this code it returns. If I unregister it and run this same piece of code, it returns true again. I'm giving the fullpath of the DLL file as argument.

如果我注册一个 DLL 文件并使用此代码进行检查,它将返回。如果我取消注册并运行同一段代码,它会再次返回 true。我将 DLL 文件的完整路径作为参数。

We developed a simple DLL file in Visual C++. After that we registered it. Now we want to confirm wheteher it is registered.

我们用Visual C++开发了一个简单的 DLL 文件。之后我们注册了它。现在我们要确认它是否已注册。

Bob, will you replace the piece of code on your own, it is still difficult for me?

鲍勃,你自己替换那段代码,对我来说还是很难吗?

If I register a DLL file, is there an entry present in the registry? Shall I find those entries and judge wheteher the DLL file is registered or not?

如果我注册一个 DLL 文件,注册表中是否有条目?我要找出那些条目并判断DLL文件是否已注册?

The last answer is working with some modifications. I looked in typelib instead of clsid.

最后一个答案是进行一些修改。我查看了 typelib 而不是 clsid。

earwicker der: Anyway, I have done it with a slight modification. It's working now.

Earwicker der:不管怎样,我做了一点修改。它现在正在工作。

采纳答案by Daniel Earwicker

You need to find out the GUID of a COM object defined in the DLL. Then look at this registry key:

您需要找出 DLL 中定义的 COM 对象的 GUID。然后看看这个注册表项:

HKEY_CLASSES_ROOT\CLSID\{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}\InprocServer32

Replace the x's with the GUID.

用 GUID 替换 x。

It should have a default value that contains the full path to the DLL.

它应该有一个包含 DLL 完整路径的默认值。

回答by BobbyShaftoe

[DllImport("kernel32")]    
public extern static bool FreeLibrary(int hLibModule);

[DllImport("kernel32")]    
public extern static int LoadLibrary(string lpLibFileName);



public bool IsDllRegistered(string DllName)    
{

      int libId = LoadLibrary(DllName);    
      if (libId>0) FreeLibrary(libId);    
      return (libId>0);    
}

回答by Vinay

  1. Declare a pointer to Interface
  2. Call CoCreateInstance on the CLSID and IID
  3. If return value is not S_OK then class is not registered
  1. 声明一个指向接口的指针
  2. 在 CLSID 和 IID 上调用 CoCreateInstance
  3. 如果返回值不是 S_OK 则类未注册

回答by abatishchev

If you mean registered in GAC, here is my consideration: to be registered in GAC, an assembly must be signed with a strong name (have a public key tokenin it's name).

如果您的意思是在 GAC 中注册,那么我的考虑是:要在 GAC 中注册,程序集必须使用强名称签名(名称中包含公钥令牌)。

So you can try load it using Assembly.Load(string), if you got FileNotFoundException- assembly was not registered in GAC.

所以你可以尝试使用加载它Assembly.Load(string),如果你有FileNotFoundException- 程序集未在 GAC 中注册。

If got no error, but result Assembly.GetName().GetPublicKeyToken()is null or empty -- this mean you found assembly in application directory, not in GAC.

如果没有错误,但结果Assembly.GetName().GetPublicKeyToken()为空或空——这意味着您在应用程序目录中找到了程序集,而不是在 GAC 中。

回答by Francois

class TestDll
{
    //Import your tested DLL here
    [DllImport("kernel32")]
    public extern static int LoadLibrary(string lpLibFileName);
}

try
{
    TestDll test = new TestDll();
}
catch(DllNotFoundException ex)
{
    return false;
}

回答by javier

You can use this:

你可以使用这个:

My.Computer.Registry.ClassesRoot.GetSubKeyNames.Contains("gEncrypt.clsEncrypt")

Where "gEncrypt.clsEncrypt" is ComName.ClassName.

其中“gEncrypt.clsEncrypt”是 ComName.ClassName。

回答by C. Augusto Proiete

If you know the CLSID of the COM dll, you can just check if there's a key with that CLSID on HKEY_CLASSES_ROOT\CLSID\{CLSID-of-your-COM-component}or HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CLSID-of-your-COM-component}(Wow6432Node => 32-bit COM registered on a 64-bit machine)

如果您知道 COM dll 的 CLSID,您可以检查是否有带有该 CLSID 的密钥HKEY_CLASSES_ROOT\CLSID\{CLSID-of-your-COM-component}HKEY_CLASSES_ROOT\Wow6432Node\CLSID\{CLSID-of-your-COM-component}(Wow6432Node => 32 位 COM 在 64 位机器上注册)

Here is an example:

下面是一个例子:

private bool IsAlreadyRegistered()
{
    using (var classesRootKey = Microsoft.Win32.RegistryKey.OpenBaseKey(
           Microsoft.Win32.RegistryHive.ClassesRoot, Microsoft.Win32.RegistryView.Default))
    {
        const string clsid = "{12345678-9012-3456-7890-123456789012}";

        var clsIdKey = classesRootKey.OpenSubKey(@"Wow6432Node\CLSID\" + clsid) ??
                        classesRootKey.OpenSubKey(@"CLSID\" + clsid);

        if (clsIdKey != null)
        {
            clsIdKey.Dispose();
            return true;
        }

        return false;
    }
}