. Net using unmanaged programs

Source: Internet
Author: User

In the development process, we need to use more or less unmanaged components, such as Common ActiveX (generally. ocx component written in VB) or COM components (usually written in C ++ ). We all know. netProgramYesCodeYou can use either of the following methods:The method of dllimport calls the unmanaged code directly. The second method is converted to "InterOP" with the suffix ". DLL package component (you can add reference through vs [Vs will automatically generate the corresponding component to put it in the OBJ folder] or use
Tlbimp.exe).

When the first method is mainly used for simple calls and does not want to deploy activx or COM components, the difficulty is to find the corresponding hosting type based on the unmanaged type (but many methods are introduced online, including some tools to help us solve this problem ). For the second method, the advantage of using the packaging class is that it is easy to use, but it needs to register the COM component (using the regsvr32 tool ).

Let's look at an example for the second method (I will not give an example for the first method, so let's take a look at it later ):

The following code checks whether a USB key is inserted. The ukey component itself is an OCX control. You can add it to generate an InterOP in OBJ using ukeyotplib. ukeyotplib. DLL to complete the control packaging, and then in the code, just like using a hosted component.

 

Code

 1   Using  System;
2   Using System. Collections. Generic;
3 Using System. text;
4 Using Ukeyotplib;
5 Namespace Invokeunmanagedcode
6 {
7 Class Program
8 {
9 Static Void Main ( String [] ARGs)
10 {
11 Otpukeyclass ukey = New Otpukeyclass ();
12 String Devicepath = Ukey. findport ( 0 );
13 If (Ukey. lasterr ! = 0 ) // The ukey is not found.
14 {
15 Console. writeline ( " Ukey is not inserted! " );
16 }
17 Else
18 {
19 Console. writeline ( " Ukey inserted! " );
20 }
21 Console. readkey ();
22 }
23 }
24 }
25

 

 

First, I will try not to register the component. If the result shows that the component is not found.

Then I register the component without inserting the ukey.

Insert ukey

Note: The registration component generally uses the regsvr32 tool, such as ukey, which usually has an installer and will be automatically registered during the installation process.

The above are commonly used methods for calling unmanaged programs. Are there other methods besides these two methods? This is achieved through "loading unmanaged Dynamic Link Libraries. This individual does not use too much, so it will not go into details.

Supplement: Sometimes we do not know the components we have registered, or we have registered them repeatedly (note that repeated registration may cause problems for you due to different versions ). In this case, how do I know the registered component information? The answer is to use the window optimization master. Run: System cleanup-ActiveX cleanup-analysis to view all registered com or ActiveX components. For example, the information of the USB key component I registered.

Here is a tip: If you see the registered information that you want to unregister and are afraid of the trouble of using the command line, you can first follow the component location provided by the window optimization master, place it elsewhere, and then use the Windows optimization master to fix it (because this is the specified location and there is no corresponding component, the optimization master considers this information as junk information) to delete the registration information.

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.