1, the new project in c#2005, type is the class library, the project name is addcom OK.
Configuration: Right-click Addcom in Solution Explorer, select Properties, select Build, select Register for COM Interop (_P)
2. Open the AssemblyInfo.cs file, set [Assembly:comvisible (True)], if not gerze cannot be called by other programs
3, write COM components will use a GUID (Global unique ID), writing COM components must be used. C # will default to generate a AssemblyInfo.cs, such as no build, the next generation of software on the net, there are many.
4, C # in the source code as follows, according to the following method to write code after the project,/bin/release get AddCom.dll.
Using System;
Using System.Collections.Generic;
Using System.Text;
Using System.Runtime.InteropServices;
Namespace addcom
{
[Guid ("298d881c-e2a3-4638-b872-73eade25511c")]
public interface Addcominterface
{
[DispID (1)]
int Iadd (int a, int b);
}
[Guid ("2c5b7580-4038-4d90-babd-8b83fce5a467")]
[ClassInterface (ClassInterfaceType.None)]
public class Addcomservice:addcominterface
{
Public Addcomservice ()
{
}
public int Iadd (int a, int b)
{
int c = 0;
c = a + B;
return C;
}
}
}
5. Using the E:\regasm AddCom.dll command to register COM components under DOS can be understood as a Regsvr32 registered DLL component in a non-. NET platform. Prompt to register successfully.
Regasm.exe generally exist C:\Windows\Microsoft.NET\Framework\v2.0.50727, if the hint is not a command character
C:\Windows\Microsoft.NET\Framework\v2.0.50727 \regasm D:\test.dll (D:\test.dll is the directory where the DLL is located)
JS calls com
<HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><OBJECTClassID=CLSID:0045EA0A-2042-43F8-A2F4-B5CF54D45C10ID=comtestwidth=0Height=0></OBJECT><Scriptlanguage= "JavaScript">Alert ("----------------------Start-----------------------");varwmp;varIresult= 12345; alert (iresult); WMP=document.getElementById ("comtest"); alert (WMP);if(WMP== NULL{alert (WMP);}Else{Iresult=Wmp.iadd (4,9); Alert ("Test successful!");} Alert ("Iadd (4,9) =" +iresult); Alert ("----------------------Start-----------------------");</Script></Head></HTML>
If JS calls the new method, it needs to be defined in the interface and then implemented in the class.