C # generate COM components for ASP to call

Source: Internet
Author: User

I. vs2005-create a project-C # class library

The source code of the class library is as follows (including interfaces, classes, and event interfaces ):

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;
Using system. Data. sqlclient;
Using system. runtime. interopservices;

Namespace entryclass
{

// Generation of guid value: Program filesmicrosoft Visual Studio 8common7toolsguidgen.exe, run it, select single choice 4, click "New guid", click "copy", enter the class library, and paste it.
// Class Interface
[GUID ("F2257E71-ECC9-47ef-A065-F5B530F24B07")]
Public interface ini_mng_interface
{
[Dispid (1)] // fixed statement, index number starting from 1
String ini_dimethyl (string entryuser, string entrypass, string agentname, string agentpass, string constr );
[Dispid (2)]
String ini_svr (string entryuser, string entrypass, string agentname, string agentpass, string constr );
}

// Event Interface
[GUID ("45875ee5-5c8d-4016-897a-fcc7dd5a6834"), // fixed Writing Method
Interfacetype (cominterfacetype. interfaceisidispatch)]
Public interface ini_mng_events
{
}

// Class
[GUID ("8edfa9ea-9589-4157-aeef-ab4861efe5d6"), // fixed Writing Method
Classinterface (classinterfacetype. None ),
Comsourceinterfaces (typeof (hst_odr_events)]
Public class ini_mng: ini_mng_interface // This class inherits the above interface and implements the abstract Method
{
Public String ini_dimethyl (string entryuser, string entrypass, string agentname, string agentpass, string constr)
{
Return "0 ";
}

Public String ini_svr (string entryuser, string entrypass, string agentname, string agentpass, string constr)
{
Return "0 ";
}

}
}

 

Ii. Generate COM components using the class library

Complete Step 1 (coding), and then
1. Start-vs2005-Vs tools-Vs command prompt, enter "Sn-K a1.snk", Press enter, then enter "Sn-P a1.snk a2.snk", and press Enter, enter "Sn-T a2.snk", Press enter, and close.
2. vs2005 class library properties-generate-output-select the "register for com..." option
3. vs2005 class library attributes-signing-signature file-browse to the a1.snk file (Program filesmicrosoft Visual Studio 8vc)
4. VS2005-Assembly.cs-comVisible is set to true
5. Generate a project-generate a project under Bin

 

Iii. asp calls COM components

1. Add <! -- Metadata type = "typelib" file = "absolute path of TLD file" -->

2. In ASP, you can use X to call methods in the COM component class:
Set x = server. Createobject ("component name. Class Name ")

 

Http://www.cnblogs.com/rentj1/archive/2009/02/23/1396187.html

 

1. Create a class library mytestdll.

2. Right-click the project "mytestdll"-"properties-" generate-"and select" register for com"

3 open the assemblyinfo. CS file and modify [Assembly: comvisible (true)]

4. Open the command line of visual sudio 2008 and input guidgen.exe to select

Select define_guid and click "New guid"

5 code

1. Each class name corresponds to an interface name. The interface name is preceded by a capitalized I

2. Attribute [dispid (n)] should be used for the method declared in the interface

3. The class must have a non-argument constructor.

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. runtime. interopservices;
Namespace mytestdll
{

// Here, The GUID is generated in step 1.
[GUID ("FFA4B191-FB5B-4dd5-B7B1-B2F32BF6F1FF")]
Public interface imytestdll
{
[Dispid (0)]
String getabout ();
}
Public class test1: imytestdll
{
Private string summary;
Public test1 ()
{
Summary = "this is my first test ";
}
Public String getabout ()
{
Return summary;
}
}
}

 

6. Generate a project

ASP test code

<%
Dim o
Set o = server. Createobject ("mytestdll. test1 ")
Response. Write O. getabout ()
Set o = nothing

%>

Tip: If you want to use this COM component developed by C # on another computer, you still need to use regasm to register it.

Method:

First, copy the files in the bin \ DEBUG directory to the target computer, and then open the command prompt line tool and enter:
Regasm the directory/file name you copied to. dll/tlb f:/dll/file name. TLB/codebase

Running can be used on this computer.

References:

 

Http://topic.csdn.net/u/20080625/13/0294fe91-200c-4939-b36b-c9a2c6781354.html

Http://topic.csdn.net/t/20060314/15/4613620.html

Http://cplus.e800.com.cn/articles/2009/211/1234338268521_3.html

Http://topic.csdn.net/t/20020712/10/868557.html

Http://www.itzhe.cn/news/20071123/21768.html

Http://www.cnblogs.com/illele/archive/2007/10/25/937050.html

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.