C # Two Methods for automatically registering DLL controls (or OCX)

Source: Internet
Author: User

 

Although MS provides us with a variety. net framework library, our program C # development brings great convenience, but sometimes, some control libraries with specific functions still need to be provided by a third party or written by themselves. When Dll references are required, we usually add them to the project by adding references, and then we can use them as conveniently as we use our own classes. However, some Dll library (OCX) files must be registered to the Windows registry before they can be added and used properly. This article introduces two methods for Automatic Registration of Dll Library (OCX) for your reference.

First, everyone knows how to manually register the Dll Control (OCX) by inputting regsvr32.exe path in the Windows runtime environment. This method is inconvenient for the automatic deployment of programs, today we will focus on how to use C # for automatic registration.

 

Method 1: Call the Regsvr32 method.

If you can enter the “regsvr32.exe path in the runtime column to register, you must use the same method in the C # program to call Regsvr32 to Achieve Registration:

Process p = new Process ();
P. StartInfo. FileName = "Regsvr32.exe ";
P. StartInfo. Arguments = "/s C: \ DllTest. dll"; // The path cannot contain spaces.
P. Start ();

To use this method, you must add a reference to the namespace System. Diagnostics:

using System.Diagnostics;

In addition, the registration is done by the regsvr32.exe program. It is not convenient to know the registration result in the system, nor to customize and control the dialog box popped up during the registration process. The parameter description of Regsvr32 is provided here: (Thank you for your reminder)

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

Regsvr32 [/u] [/S] [/n] [/I [: using line] DLL file name

The command can be written in the "Start> Run" text box or in the bat batch processing document in advance. If no parameters are provided, the DLL file registration function is provided. Other parameters correspond to the following functions:

/U: registers DLL files;

/S: run the command in silent mode, that is, the result prompt box is not displayed when the DLL file is successfully registered or unregistered.

/C: control port;

/I: Call dllinstall when using/u anti-registration;

/N: do not call the dllregisterserver. It must be used with/I.

 

Method 2: Call the DllRegisterServer Function

Since method 1 is not practical, let's look for a practical method to achieve our goal. You can call this method to complete DLL registration. You can call dllregisterserver () to complete the registration process.

First, you must introduce external methods:

[Dllimport ("dlltest. dll")]
Public static extern int dllregisterserver (); // used for registration
[Dllimport ("dlltest. dll")]
Public static extern int dllunregisterserver (); // used to cancel registration
Next, it is not difficult:
Int I = dllregisterserver ();
If (I> = 0)
{
// Registration successful!
}
Else
{
// Registration failed
}

The process of canceling registration should not be followed by code.

The two methods have been introduced, but what are the disadvantages? By the way, you can determine whether the Dll has been registered. Generally, we can put the registration process of the Dll Control in the system startup process. However, we cannot register the Dll control every time it is started? This is obviously unreasonable. Then, let's determine whether the current Dll has been registered. If yes, skip the registration process.

The registration of each Dll records information about it in the registry, such as the registration path and unique ID. 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)
{
// The Dll is not registered. Call DllRegisterServer () here.
}

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

using Microsoft.Win32;

Among them, "{7713F78A-44DE-42BA-A1F6-3FB0BD6CA63B}" is the unique ID of the Dll, and each Dll file will be different. However, the problem arises again. How do you know its unique ID? In fact, it is very simple, that is, "reverse thinking ". We can first register this Dll file, and then go to the "Search" Dll name or path under the "HKEY_CLASSES_ROOT \ CLSID" branch of the Registry to see this ID. I won't say much about simplicity.

 

Source: http://www.cnblogs.com/luckeryin/archive/2010/03/17/1687999.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.