C # Write COM component, JS call control

Source: Internet
Author: User
Tags unique id

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.

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.