Although Ms provides us with a rich. NET Framework library, our program C # Development brings great convenience, but sometimes some specific features of the control library need to be provided by a third party or written by itself. When you need to use DLL references, we usually include them in the project in the form of "add references," and then we can use them as easily as if we were using our own classes. However, some DLL library (OCX) files are required to be registered with the Windows registry before they can be added and used properly. This paper introduces two methods of automatic registration for DLL library (OCX), which provides reference for everyone.
First of all, we all know that in Windows "Run", enter the "Regsvr32.exe path" method to manually register the DLL control (OCX), showing that this method for the automated deployment of the program, such as a great inconvenience, so today we focus on how to implement automatic registration in C #.
Method One: Call the Regsvr32 method
Since you can enter the "Regsvr32.exe path" method in the run bar to register, you can certainly use the same method in your C # program to invoke Regsvr32 for registration:
Process P = new process ();
p.StartInfo.FileName = "Regsvr32.exe";
P.startinfo.arguments = "/s C:\\dlltest.dll";//cannot have spaces in the path
P.start ();
In this way, note that you want 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 outside the program by the Regsvr32.exe program, the system is not easy to know the results of registration, it is not convenient for the registration process Popup dialog box to customize and control. Here is a description of the parameters attached to Regsvr32:
Regsvr32.exe is a DLL registration and anti-registration tool used under a 32-bit system, which must be used in a command line format:
regsvr32 [/u] [/s] [/n] [/i[:cmdline]] DLL file name
Commands can be in the start → run text box, or you can write commands in the bat batch document beforehand. Without any parameters is registered DLL file function, other parameters corresponding functions are as follows:
/u: Anti-registration DLL file;
/s: Quiet mode (Silent) executes the command, which does not display the result prompt box if the DLL file is successfully registered/unregistered.
/C: Control port;
/I: Call DllInstall when using the/U anti-registration;
/N: Do not call DllRegisterServer and must be in conjunction with/I.
Method Two: Call DllRegisterServer function method
Since the method is not practical, then we are looking for a really practical way to achieve our goal. Studying Regsvr32.exe and DLL files, we will find that each file that needs to be registered includes a DllRegisterServer () method, Regsvr32.exe is to complete the DLL registration by calling the method. Hehe, knowing this, we can call DllRegisterServer () to complete the registration process.
First, you have to introduce an external method:
[DllImport ("DllTest.dll")]
public static extern int DllRegisterServer ();//Register with
[DllImport ("DllTest.dll")]
public static extern int DllUnregisterServer ();//Cancel registration with
The next step is not difficult:
int i = DllRegisterServer ();
if (i >= 0)
{
Registration Successful!
}
Else
{
Registration Failed}
}
The process of canceling the registration should not be affixed to the code.
Two methods are finished, but it seems that the shortcomings of what? By the way, that is the judgment of whether the DLL has been registered. In general, we can put the registration process of the DLL control in the system startup process to complete, but can not always be registered every time it started? It is obviously unreasonable to do so. So, let's judge whether the current DLL has been registered, and if it has already been registered, skip the registration process.
The registration of each DLL will be recorded in the registry about itself, such as the registration path, unique ID, etc. Here we use the unique ID number left by it 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;
Where the "{7713f78a-44de-42ba-a1f6-3fb0bd6ca63b}" is the unique ID of the DLL, 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 see this ID by registering the DLL file and then going to the name or path of the "find" DLL under the "HKEY_CLASSES_ROOT\CLSID" branch of the registry.
C # Implementation of DLL (OCX) control two methods of automatic registration on the Internet, and then I tried it, and I could use it.