Detailed process of writing COM components using C #

Source: Internet
Author: User
1. Create a class library project

2. Change class1.cs to the name we want.

Ask if you want to rename the class at the same time, OK

3. Modify assemblyinfo. Cs in the properties directory.
Set the comvisible attribute to true.

4. Project menu-> mylib attributes
Find the generate Tab

Next, find "register for com InterOP" and check

5. Continue and find the "signature" tab.
CheckProgramSet signature"
In the drop-down box below, select "<new...>"

6. In the pop-up dialog box, enter mylib .. Or just give a name.
Remove the password-protected file option

7. Start encoding. Any public class must have an I-enabled interface definition.

C # code
   Using  System;  Using  System. Collections. Generic;  Using System. text;  Using  System. runtime. interopservices;  Namespace  Mylib {[comvisible (  True  )] [GUID (  "  2cbd3d76-35f1-4f9d-9c1b-9dbfee412f76  "  )]  Public     Interface Imyclass {  Void  Initialize ();  Void  Dispose ();  Int  Add (  Int  X,  Int  Y);} [comvisible (  True  )] [GUID ( "  EA2F140A-108F-47ae-BBD5-83EEE646CC0D  "  )] [Progid (  "  Mylib. myclass  "  )]  Public     Class  Myclass: imyclass {  Public     Void Initialize (){  //  Nothing todo    }  Public     Void  Dispose (){  //  Nothing todo   }  Public     Int  Add (  Int  X,  Int  Y ){  Return  X  +  Y ;}}} 

8. For the string in the guid attribute, under the "tool" menu, "Create guid"
Select registry format, and then copy

Note that you need to remove the curly brackets before and after the guid in [GUID ("...").

9. compile it
Enter the directory where the DLL is located at the command prompt.
Use gacutil/I mylib. DLL to add this DLL to the global cache.
Then use regasm mylib. DLL to register this DLL.

10. Try it In VBScript...

HTML code
   <  Script  Language  = "VBScript" >
DimO:SetO=Createobject("Mylib. myclass")
O. initialize Msgbox " 1 + 2 = " & O. Add ( 1 , 2 ) O. Dispose Set O = Nothing </ Script >
1. Create a class library project

2. Change class1.cs to the name we want.

Ask if you want to rename the class at the same time, OK

3. Modify assemblyinfo. Cs in the properties directory.
Set the comvisible attribute to true.

4. Project menu-> mylib attributes
Find the generate Tab

Next, find "register for com InterOP" and check

5. Continue and find the "signature" tab.
Check "sign the assembly"
In the drop-down box below, select "<new...>"

6. In the pop-up dialog box, enter mylib .. Or just give a name.
Remove the password-protected file option

7. Start encoding. Any public class must have an I-enabled interface definition.

C # code
   Using  System;  Using  System. Collections. Generic;  Using  System. text;  Using  System. runtime. interopservices; Namespace  Mylib {[comvisible (  True  )] [GUID (  "  2cbd3d76-35f1-4f9d-9c1b-9dbfee412f76  "  )]  Public     Interface  Imyclass {  Void Initialize ();  Void  Dispose ();  Int  Add (  Int  X,  Int  Y);} [comvisible (  True  )] [GUID (  "  EA2F140A-108F-47ae-BBD5-83EEE646CC0D  " )] [Progid (  "  Mylib. myclass  "  )]  Public     Class  Myclass: imyclass {  Public     Void  Initialize (){ //  Nothing todo    }  Public     Void  Dispose (){  //  Nothing todo    } Public     Int  Add (  Int  X,  Int  Y ){  Return  X  +  Y ;}}} 

8. For the string in the guid attribute, under the "tool" menu, "Create guid"
Select registry format, and then copy

Note that you need to remove the curly brackets before and after the guid in [GUID ("...").

9. compile it
Enter the directory where the DLL is located at the command prompt.
Use gacutil/I mylib. DLL to add this DLL to the global cache.
Then use regasm mylib. DLL to register this DLL.

10. Try it In VBScript...

HTML code
   <  Script  Language  = "VBScript" >
DimO:SetO=Createobject("Mylib. myclass")
O. initialize Msgbox " 1 + 2 = " & O. Add ( 1 , 2 ) O. Dispose Set O = Nothing </ Script >

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.