C# self register ocx


Author: Dave
Date: 12.12.15 - 4:42pm



In a previous post I covered how you can have your VB6 apps detect if a necessary ActiveX control (dll or ocx) is registered at startup and compensate if not.

The same trick also applies to c# apps. I have a tool which requires my hexeditor ocx control. C# must follow the same suit as vb6 and not require the ocx until that specific form is loaded. This gives us an opportunity to detect it in Program.Main() and register the ocx if need be.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    static class Program
    {

        static void RegisterOcx(string ocxPath)
        {
            Process p = new Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.FileName = "regsvr32.exe";
            p.StartInfo.Arguments = ocxPath;
            p.Start();
            p.WaitForExit();
        }

        static string FindExternal(string baseName)
        {
            string f = Application.StartupPath;

            for (int i = 0; i < 6; i++)
            {
                if (!File.Exists(f + "\\" + baseName)) f = Path.GetDirectoryName(f);
            }

            if (!File.Exists(f + "\\" + baseName))
            {
                MessageBox.Show("Could not locate " + baseName + " in: " + f);
                return "";
            }

            f += "\\" + baseName;
            return f;
        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            var clsIdKey = Registry.ClassesRoot.OpenSubKey("rhexed.HexEd");
            if (clsIdKey == null) 
            {
                string ocx = FindExternal("hexed.ocx");
                if (ocx.Length != 0) RegisterOcx(ocx);
            }
             
            Application.Run(new Form1());
        }
}





Comments: (0)

 
Leave Comment:
Name:
Email: (not shown)
Message: (Required)
Math Question: 54 + 1 = ? followed by the letter: O 



About Me
More Blogs
Main Site
Posts: (All)
2020 ( 3 )
2019 ( 5 )
2018 ( 6 )
2017 ( 6 )
2016 ( 22 )
2015 (16)
     C# self register ocx
     VB6 Class Method Pointers
     JS Debugger
     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 )