C# automatically realizes two methods of Dll (OCX) control registration

Although MS provides us with a wealth of .net framework libraries, our program C# development brings great convenience, but sometimes, some specific function control libraries still need to be provided by third parties or written by themselves. When we need to use Dll references, we usually incorporate them into the project by "adding references", and then we can use them as easily as our own classes. However, some Dll library (OCX) files need to be registered in the Windows registry before they can be added and used normally. This article introduces two methods for automatic registration of Dll library (OCX) for your reference.

First of all, we all know that in the "Run" of Windows, enter the "Regsvr32.exe path" method to manually register the Dll control (OCX), which shows that this method brings great inconvenience to the automatic deployment of the program, so , Today we focus on how to use C# to achieve automatic registration.

Method 1: Call the Regsvr32 method

Since you can enter the "Regsvr32.exe path" method in the run bar to register, then you can use the same method to call Regsvr32 in the C# program to achieve registration:

Process p = new Process();
p.StartInfo.FileName = "Regsvr32.exe";
p.StartInfo.Arguments = " /s C:\\DllTest.dll " ; // No spaces in the path 
p.Start();

Taking this approach, be careful to add a reference to the namespace System.Diagnostics:

using System.Diagnostics;

In addition, this method has a disadvantage, that is, the registration work is done by the Regsvr32.exe program outside this program, it is not convenient for the system to know the result of the registration, and it is not convenient to carry out the dialog box popped up during the registration process. Customize and control. Here is the parameter description of Regsvr32:

regsvr32.exe is a DLL registration and anti-registration tool used in 32-bit systems. It must be used through the command line. The format is:

  regsvr32 [/u] [/s] [/n] [/i[:cmdline]] DLL filename

  The command can be in the "Start → Run" text box, or you can write the command in the bat batch document in advance. Without any parameters, it is the function of registering the DLL file, and the corresponding functions of other parameters are as follows:

  /u: Unregister DLL files;

  /s: Execute the command in silent mode, that is, the result prompt box will not be displayed on the premise of successfully registering/unregistering the DLL file.

  /c: control port;

  /i: call DllInstall when using /u to deregister;

  /n: Do not call DllRegisterServer, must be used in conjunction with /i.

Method 2: Call the DllRegisterServer function method

Since the first method is not very practical, let's find a really practical method to achieve our purpose. Studying Regsvr32.exe and Dll files, we will find that, in fact, each file that needs to be registered includes a DllRegisterServer() method, and Regsvr32.exe completes the registration of Dll by calling this method. Oh, knowing this, we can call DllRegisterServer() ourselves to complete the registration process.

First, we have to introduce external methods:

    using System.Runtime.InteropServices;

    namespace AlarmList
    {
       public partial class Form1 : Form
       {
          [DllImport( " DllTest.dll " )]
           public  static  extern  int DllRegisterServer(); // Use when registering 

          [DllImport( " DllTest.dll " )]
           public  static  extern  int DllUnregisterServer(); // Use when unregistering

          public Form1()
          {
             int i = DllRegisterServer();
             if (i >= 0)
             {
                // Registration succeeded!

             }
             else
             {
                 // Registration failed 
             }
          }
       }
    }

The registration process should no longer post code.

The two methods have been introduced, but what seems to be missing? By the way, that is to judge whether the Dll has been registered. Under normal circumstances, we can complete the registration process of the Dll control in the process of system startup, but we can't register it every time we start it, right? This is clearly unreasonable. Then, let's judge whether the current Dll has been registered. If it has been registered, skip the registration process.

Each Dll registration will record information about itself in the registry, such as registration path, unique ID and so on. Here we use the unique ID number it leaves to judge:

RegistryKey rkTest = Registry.ClassesRoot.OpenSubKey("CLSID\\{7713F78A-44DE-42BA-A1F6-3FB0BD6CA63B}\\"); 
if (rkTest == null)
{
// Dll is not registered, call DllRegisterServer() here 
}

Note to add a reference to the namespace Microsoft.Win32:

using Microsoft.Win32;

"{7713F78A-44DE-42BA-A1F6-3FB0BD6CA63B}" is the unique ID of the Dll, and each Dll file will be different. However, the question comes again, how to know its unique ID? In fact, it is very simple, that is, "reverse thinking". We can first register the Dll file, and then go to the "HKEY_CLASSES_ROOT\CLSID" branch of the registry to "find" the name or path of the Dll, and you can see the ID.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324812953&siteId=291194637