33

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

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 full-path of the DLL file as argument.

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

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 whether the DLL file is registered or not?

shA.t
  • 16,580
  • 5
  • 54
  • 111
  • What exactly do you mean by registered in the context of a .NET DLL? This is more of a COM DLL concept, and does not apply to .NET DLLs. Can you clarify please? – David M Mar 27 '09 at 09:40

7 Answers7

22

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

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

Replace the x's with the GUID.

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

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • @Neil Barnwell - if it's for a DLL you don't have any source or documentation for, the easiest thing to do is to register the DLL it with `regsvr32` and then search the registry for the full path to the DLL, and you'll find one or more `InprocServer32` keys for the objects exposed by the DLL. – Daniel Earwicker Mar 29 '10 at 12:43
  • 6
    Don't forget to check HKEY_CLASSES_ROOT\Wow6432Node\CLSID\ – DanM7 May 09 '13 at 15:42
  • I know this was asked a long time ago. However, if the DLL you developed is in Visual C++, and it uses ATL.Then, it will most likely be registered in Computer\HKEY_CLASSES_ROOT\TypeLib\ Hope this helps some late arrival as it did with me. – Mo Aboulmagd Nov 26 '20 at 00:52
  • [Comments from OP:] answer is working with some modifications. I looked in typelib instead of clsid. Anyway, I have done it with a slight modification. It's working now. – shA.t Dec 29 '21 at 12:35
5

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 token in it's name).

So you can try load it using Assembly.Load(string), if you got FileNotFoundException - assembly was not registered in 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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
3

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)

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;
    }
}
C. Augusto Proiete
  • 24,684
  • 2
  • 63
  • 91
3

You can use this:

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

Where "gEncrypt.clsEncrypt" is ComName.ClassName.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
javier
  • 31
  • 1
2
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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Francois
  • 21
  • 1
2
  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
Vinay
  • 4,743
  • 7
  • 33
  • 43
1
[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);    
}
BobbyShaftoe
  • 28,337
  • 7
  • 52
  • 74
  • you can not use >0 because of C++ autocast int>0 to bool=true. So, if(libID) .. return libID; – abatishchev Mar 27 '09 at 09:53
  • 1
    This just discovers if the DLL can be loaded using a filename. Nothing to do with being registered or not. – Daniel Earwicker Mar 27 '09 at 10:06
  • 1
    @abatishchev, no, not ture. LoadLibrary here returns an int. – BobbyShaftoe Mar 27 '09 at 19:18
  • From http://stackoverflow.com/questions/4966508/how-to-check-com-dll-is-registered-or-not-with-c If the DLL loads, it's because the DLL was found on the path. If you drop the DLL into the current directory or into C:\Windows\System32 or anywhere else it can be found, then LoadLibrary will work. At that point, so will DllGetClassObject, but this tells us nothing. – AshesToAshes Mar 26 '12 at 07:51