Example of Asp calling COM component compiled by C#
1 Create a new class library MyTestDLL
2 Right-click the project MyTestDLL-》Properties-》Generate-》Check Register for COM interop
3 Open the AssemblyInfo.cs file and modify [assembly: ComVisible(true)]
4 Open the command prompt tool of Visual Sutdio 2008 and enter guidgen.exe. Select DEFINE_GUID and click New GUID.
5 codes
1. Each class name corresponds to an interface name. The interface name is the class name plus a capital I.
2. The method declared in the interface must use the attribute [DispId(n)]
3. The class must have a parameterless constructor
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace MyTestDll
{
//The Guid here is generated in step 4.
[Guid(FFA4B191-FB5B-4dd5-B7B1-B2F32BF6F1FF)]
public interface IMyTestDll
{
[DispId(0)]
string GetAbout();
}
public class Test1:IMyTestDll
{
PRivate string summary;
publicTest1()
{
summary = This is my first test;
}
public string GetAbout()
{
return summary;
}
}
}
6 Generate 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 the COM component we developed in C# on other computers, you also need to register it with regasm.
The method is:
First copy the files in the bin/Debug directory to the target computer, then open the command prompt tool and enter:
regasm The directory/filename.dll you copied to /tlb f:/dll/filename.tlb /codebase
Run can be used on this computer