VB6 Self Register OCXsAuthor: 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: ---------------------------------------------------
'This will handle the installation of the OCX control if it is not already found on the system 'this runs from a module before the main form is loaded because the main form depends on the OCX. 'I did not want to use a batch file because there is a strong chance that newer versions of Windows 'which are 64-bit would try to execute it with a 64-bit cmd.exe which would then screw up regsvr32 call. Sub Main() Dim ocx As String Dim src As String On Error Resume Next ocx = "c:windowssystem32mscomm32.ocx" src = App.path & "mscomm32.ocx" If Not FileExists(ocx) Then If Not FileExists(src) Then MsgBox "serial port OCX not found and not installed?" End Else FileCopy src, ocx Shell "regsvr32 " & ocx If Err.Number <> 0 Then MsgBox "failed to register OCX control? you may have to run with administrator privileges", vbInformation End If End If End If Form1.Visible = True End Sub example auto elevate permissions so you can write to HKLM for sure: Function ElevateCmdIfReq(exe As String, args As String, Optional taskID As Long) If IsVistaPlus() Then If Not IsUserAnAdministrator() Then MsgBox "Must be an admin user to install these settings can not elevate.", vbInformation Else If IsProcessElevated() Then If taskID = 14 Then args = Replace(args, "/treg", Empty) args = trim(Replace(args, """", Empty)) RegisterTlbFile args End If Else RunElevated exe, essSW_HIDE, , args End If End If Else If taskID = 14 Then args = Replace(args, "/treg", Empty) args = trim(Replace(args, """", Empty)) RegisterTlbFile args Else Shell exe & " " & args End If End If End Function * 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:
On 11.03.19 - 4:01pm Shareef wrote:
On 11.03.19 - 6:37pm Dave wrote:
|
About Me More Blogs Main Site |
|||