VB6 Self Register OCXs

Author: Dave
Date: 02.04.15 - 4:22am

One of the tricks with Visual Basic six is that you often depend on using ActiveX DLLs or OCX controls which must be registered on the system before use.

Typically I would just include a batch file to copy them to the system 32 directory and then register them so user clicks run me.bat and you're all set small note in the readme.

With the advent of 64-bit operating systems this is no longer sufficient because there is a 64-bit version of cmd.exe as well as regsvr32.exe. If user just double clicks on a batch file on a 64-bit version of Windows it automatically runs the 64-bit versions Of these programs. The problem is is that it registers the OCX control as if it was a 64-bit control which it is not. I looked a little bit on how to get cute and detect this from a batch file and it was not worth the time.

With just a small bit of thought I realized that my Visual Basic six application could register its own OCX controls that it required when it started up as long as it started from a module and sub main versus using a form as it startup object which would try to load the dependencies immediately. this nuance of VB 6 Really works in our favor here!

I wish I had thought of this earlier, and realized that the VB6 applications can register their own dependencies at runtime in this manner.

Another neat trick, lets say your ocx uses a standard windows dll. In my example I am working with my scintinilla wrapper scivb.ocx and the scilexer.dll. So the ocx wont be loaded until the form hosting it is first shown. I had a case where I wanted to load a custom scilexer dll just for this one specific application. This can be done. Use loadlibrary to load the specific dll before the form shows, then thats the copy that will be used by the ocx even in the real one is sitting in the same directory as the ocx. Pretty handy little trick!

here are the highlights broken down in logic order:
  1. an unregistered ocx will cause your application to crash
  2. the ocx is loaded on demand the first time a form that uses it is shown *
  3. sub main runs before any forms are shown
  4. in sub main, we can check if the ocx is registered, if not we can register it ourselves at runtime
  5. program continues as normal without requiring an explicit installer application.
  6. since vb6 is only 32 bit, we are sure the call to regsvr32 is calling the 32bit version
  7. you could also check for admin privileges and auto elevate if required
example register ocx at runtime

example auto elevate permissions so you can write to HKLM for sure:

* assuming no classes from it are used as global variables in your code

**you could register itself manually without regsvr32, its actually the activex dll/ocx itself which does it through the DllRegisterServer export vb6 automatically builds in, but since regsvr32 is a system component and always guaranteed to be there its easier just to let it do it.

Comments: (3)

On 10.15.15 - 2:42am Daniel wrote:
You mean you are able to register ocx libraries at run time without the internal regsvr32.exe ? Sorry, but I cannot catch the trick with a sub main inserted in a module. Could you add a bit more details about this approach ? Thank you.

On 11.03.19 - 4:01pm Shareef wrote:
if i created the module, it will load before anything else can you explain that to me

On 11.03.19 - 6:37pm Dave wrote:
You must create sub main() in a module and then set that as the startup procedure in the prophecy properties that is how it will run first before the main form and gives chance to register dependencies which are loaded on demand when form loads. After registering dependencies then sub main() loads main form

Leave Comment:
Email: (not shown)
Message: (Required)
Math Question: 30 + 19 = ? followed by the letter: A 

About Me
More Blogs
Main Site
Posts: (All)
2024 ( 1 )
2023 ( 9 )
2022 ( 4 )
2021 ( 2 )
2020 ( 4 )
2019 ( 5 )
2018 ( 6 )
2017 ( 6 )
2016 ( 22 )
2015 (15)
     C# self register ocx
     VB6 Class Method Pointers
     Duktape Debug Protocol
     QtScript 4 VB
     Vb6 Named Args
     vb6 Addin Part 2
     VB6 Addin vrs Toolbars
     OpenFile Dialog MultiSelect
     Duktape Example
     DukTape JS
     VB6 Unsigned
     .Net version
     TitleBar Height
     .NET again
     VB6 Self Register OCXs
2014 ( 25 )
2013 ( 4 )
2012 ( 10 )
2011 ( 7 )
2010 ( 11 )
2009 ( 3 )