C # compile COM components

Source: Internet
Author: User

1. Create a class library project
2. Change class1.cs to the desired name and ask if you want to rename the class at the same time.
3. Modify the assemblyinfo. CS comvisible attribute under the properties directory to true.
4. Go to the "generate" tab of the "Project menu"> "mylib property" and click "register for com InterOP ".
5. Go on to the "signature" tab and click "sign for assembly". In the following drop-down box, select "<new...>"
6. In the pop-up dialog box, enter mylib .. You can also remove the password-protected file option by name.
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, select registry format for "create guid", and then copy
Note that you need to remove the curly brackets before and after the guid in [GUID ("...").
9. compile it at the command prompt. Go to the dll Directory and use gacutil/I mylib. DLL to add the DLL to the global cache. Then use regasm mylib. DLL to register the 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.