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:
Name:
Email: (not shown)
Message: (Required)
Math Question: 64 + 29 = ? followed by the letter: B 



About Me
More Blogs
Main Site
Posts: (year)
2024 (3)
     VB6 JSON
     ffmpeg voodoo
     RegJump Vb
2023 (9)
     VB6 Virtual Files
     File handles across dlls
     python abort / script timeout
     py4vb
     VB6 Python embed w/debugger
     python embedding
     VB6 IDE Enhancements
     No Sleep
     A2W no ATL
2022 (4)
     More VB6 - C data passing
     Vb6 Asm listing
     Byte Array C to VB6
     Planet Source Code DVDs
2021 (2)
     Obscure VB
     VB6 IDE SP6
2020 (4)
     NTFileSize
     BSTR from C Dll to VB
     Cpp Memory Manipulation
     ActiveX Binary Compatability
2019 (5)
     Console tricks
     FireFox temp dir
     OCX License
     Extract substring
     VB6 Console Apps
2018 (6)
     VB6 UDTs
     VB6 Debugger View As Hex tooltips
     VB6 - C Share registry data
     VB6 Addin Missing Menus
     VB6 Class Init Params
     VB6 isIn function
2017 (6)
     Python and VB6
     Python pros and cons
     download web Dir
     vc rand in python
     VB6 Language Enhancement
     Register .NET as COM
2016 (22)
     VB6 CDECL
     UDT Tricks pt2
     Remote Data Extraction
     Collection Extender
     VB6 FindResource
     CDO.Message
     DirList Single Click
     Reset CheckPoint VPN Policy
     VB6 BSTR Oddities Explained
     SafeArrays in C
     BSTR and Variant in C++
     Property let optional args
     Misc Libs
     Enum Named Pipes
     Vb6 Collection in C++
     VB6 Overloaded Methods
     EXPORT FUNCDNAME Warning
     VB6 Syncronous Socket
     Simple IPC
     VB6 Auto Resize Form Elements
     Mach3 Automation
     Exit For in While
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)
     Query Last 12 Mos
     Progid from Interface ID
     VB6 to C Array Examples
     Human Readable Variant Type
     ScriptBasic COM Integration
     CodeView Addin
     ScriptBasic - Part 2
     Script Env
     MSCOMCTL Win7 Error
     printf override
     History Combo
     Disable IE
     API Hooking in VB6
     Addin Hook Events
     FastBuild Addin
     VB6 MemoryWindow
     Link C Obj Files into VB6
     Vb6 Standard Dlls
     CStr for Pascal
     Lazarus Review
     asprintf for VS
     VB6 GlobalMultiUse
     Scintilla in VB6
     Dynamic Highlight
     WinVerifyTrust, CryptMsgGetParam VB6
2013 (4)
     MS GLEE Graphing
     printf for VB6
     C# App Config
     Tero DES C# Test
2012 (10)
     VC 2008 Bit Fields
     Speed trap
     C# Db Class Generator
     VB6 vrs .NET (again)
     FireFox Whois Extension
     git and vb6
     Code Additions
     Compiled date to string
     C# ListView Sorter
     VB6 Wish List
2011 (7)
     C# Process Injection
     CAPTCHA Bots
     C# PE Offset Calculator
     VB6 Async Download
     Show Desktop
     coding philosophy
     Code release
2010 (11)
     Dll Not Found in IDE
     Advanced MSScript Control
     random tip
     Clipart / Vector Art
     VB6 Callback from C#
     Binary data from VB6 to C#
     CSharp and MsScriptControl
     HexDumper functions
     Js Beautify From VB6 or C#
     vb6 FormPos
     Inline Asm w VB6
2009 (3)
     The .NET Fiasco
     One rub on computers
     Universal extractor