Search code examples
c#.netvb.netwinapiocx

How to get location and file name of OCX in vb.net


I can load up the control fine (pseudo code):

Set o = CreateObject("DevsoftCustom.CommControl")

Now, I need to find the file and directory for that ocx (so I can read the version stamp on the ocx, and read from a config file in that directory).

How is this done in C# and vb.net? (This project is in vb.net)


Solution

  • Now, I need to find the file and directory for that ocx

    Assuming you are not using private COM registration, such information is in the registry.

    HKCR\CLSID\<SOME-GUID>\LocalServer="<path>"
    

    ...or:

    HKCR\<PROG-ID>\CLSID=<GUID>  // use this GUID in the example above
    

    Or if you don't want to use the registry you can make use of the native function CLSIDFromProgID(). Tell me more...

    Here's an easy representation of Microsoft Outlook Date Control in OLE/COM Object Viewer.

    You can use the OLE/COM Object Viewer to view a control's interfaces.

    enter image description here

    So in your case, look up the class ID in the registry based on the prog ID "DevsoftCustom.CommControl"

    i.e.

    1. Under HKCR, look for the key DevsoftCustom.CommControl
    2. There will be a child key called CLSID, take note of the default value
    3. Open HKCR\CLSID\ < your-key-from-step-2 >
    4. The path will be in a subkey called LocalServer32

    Using the Office example:

    enter image description here

    Once you know how COM works, its easy to implement the above in c#.

    Isn't there a more direct way to ask the OS based on the object/control name?

    Not that I'm aware of and as Damien mentioned, the point of COM (including OCX) is that you don't care where something is, you just want an instance.

    The closest parallel is having a .NET assembly in the GAC where you want to create an instance of an object but don't want to have to worry about deploying the same assembly over and over on the same computer. Of course, the GAC serves far more purposes than that so that's where the comparison ends.