Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a DLL file is registered?

Tags:

c#

.net

People also ask

How do I verify a DLL file?

Type "sfc /scannow" at the command prompt, then press the "Enter" key. The System File Checker will run. It will check for all missing and corrupted DLL files and replace them.

Where are DLL registered?

Summary. Regsvr32 is a command-line utility to register and unregister OLE controls, such as DLLs and ActiveX controls in the Windows Registry. Regsvr32.exe is installed in the %systemroot%\System32 folder in Windows XP and later versions of Windows.

How can I tell if a .NET DLL is registered?

You could scan/search the registry if there is an entry for the classid of the COM component. Or some other key you might set on registration. Another method would be to try an instantiate the COM object and check if it succeeds or fails.

How do I check if a component is registered?

Just make a lookup in the registry. HKEY_CLASSES_ROOT\yourcom. component.


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.


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.


You can use this:

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

Where "gEncrypt.clsEncrypt" is ComName.ClassName.


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

  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

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

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