tags:

views:

2417

answers:

5

hi, how to find whether a dll(c#) is registered or not programmatically.

i already tried this code.but it doesnt come off.

if i register a dll and check using this code it returns,if i unregister it and run this same piece of code ,it returns true again,im giving the fullpath of the dll as argument

we developed a simple dll in vc++ ,after that we registered it .now we want to conform wheteher it is registered.

bob will u replace the piece of code on your own,it is still difficult for me

if i register a dll is there any entry present in the registry.shall i found those entries and judge wheteher the dll is registered or not.

the last answer is working with some modifications,i lookd in typelib instead of clsid

earwicker der anyway i done it with slight modification,its working now

+1  A: 
[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
you can not use >0 because of C++ autocast int>0 to bool=true. So, if(libID) .. return libID;
abatishchev
This just discovers if the DLL can be loaded using a filename. Nothing to do with being registered or not.
Daniel Earwicker
@abatishchev, no, not ture. LoadLibrary here returns an int.
BobbyShaftoe
+1  A: 

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
+1  A: 
  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
+1  A: 

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
+3  A: 

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
How does one get that GUID?
Neil Barnwell
@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