reinaldo.gomes has asked for the wisdom of the Perl Monks concerning the following question:

I have a simple task: to make sure that a given dll is indeed registered in a Windows system.

The only thing I could think of is: to extract a CLSID from the DLL's TypeLib and check it against Windows' registry. This way I can be 100% sure that this exact file is registered, and not just any DLL with the same name.

The question is: how can I reliably extract TypeLib's CLSID from an unregistered DLL? I've been able to do it through hexdump/regex, but in the end I'll be just guessing where it is, and it might fail eventually.

Is there a way to make the DLL spit it out for me? Just the way it does to the registry when the registering function is called?

Replies are listed 'Best First'.
Re: Extract CLSID from unregistered DLL
by Corion (Patriarch) on Dec 24, 2016 at 07:37 UTC

    According to a quick look on Google, you have to use LoadTypeLibEx (with regkind set to NONE) to load the DLL and get its ITypeLib interface.

    If you just want to make sure that the DLL is registered, maybe just use regsvr.exe or the RegisterType call.

      Calling regsvr32 or anything else that actually registers the dll is not a safe option.

      I have a few hundreds servers. Each one have dozens of DLLs. Every server is updated somewhat weekly, and most DLLs are changed/updated.

      Given this scenario, I have to make sure every last one of them is actually registered and up to date. I can't simply look for them in the registry by name, because it only tells me whether or not there is a DLL registered with that given name, not necessarily that new file I just copied with the same name.

      The LoadTypeLibEx/ITypeLib method looks primising, since it's just checking the file, but I'm not sure how to use it.

        Note that LoadTypeLibEx will not be "just check" the file, it will also load that library into memory and execute some code from the library.

        Using it likely consists of using Win32::API to load the function call and then supplying the correct parameters to the imported function.

        I have not used LoadTypeLibEx, so your guess is as good as mine, but Microsoft provided some documentation on how to use the things.

Re: Extract CLSID from unregistered DLL
by reinaldo.gomes (Beadle) on Dec 25, 2016 at 13:50 UTC
    Well, I kind of gave in to the hexdump/pattern match method.

    After quite a few hours of hell on earth, I mean, struggling with Visual C++ basics, I figured out how to get the CLSID from a DLL with the above recommended libraries, only to realize that I would be able to run this code from a Windows machine only, due to its dependencies.

    After carefully scavenging the DLLs with different hex dumpers and disassemblers, I found what it seems to be a reliable pattern to get the CLSID next to its entry point. Maybe not the ideal solution, but the best I could come up with, without messing (too much) with Windows.