Call the. net dll generated by C # In VB and ASP

Source: Internet
Author: User

Http://blog.csdn.net/KimmKing/archive/2008/12/04/3445233.aspx

 

Abstract: This article describes how to call the. net dll generated by C # In VB and ASP.
 
1. Use C # To create. Net DLL
1.1 create a project
Create a new project in. Net: testcom.

1.2 implement C # class
Add an interface and a class in the class1.cs file: using system;
Using system. Collections. Generic;
Using system. text;
Using system. runtime. interopservices;

Namespace testcom
{
[Comvisible (true)]
Public interface iclass1
{
String test ();
}

[Comvisible (true)]
[Classinterface (classinterfacetype. autodual)]
Public class class1: iclass1
{
Public String test ()
{
Return "OK ";
}

}
}

Note that attributes on interfaces and classes are visible to and generated for com.
1.3 Add a strong name
Select the vistual studio command prompt under vistual Studio Tools under the vistual studio directory in the Start Menu. Use Sn-k c:/mykey. SNK to generate the signature file. Right-click the project, click Properties, select Signature, select the Assembly signature, and select the mykey. SNK file.

1.4 generate a solution
You can find testcom. dll in/testcom/bin/debug of the project directory. So far, the. net dll generated by C # has been completed.
2 generate and register a Type Library
2.1 generate a TLB Library
In the Visual Studio command prompt, switch to this directory. Enter tlbexp testcom. dll/out: testcom. TLB. A message is displayed, indicating that the TLB library file is exported successfully.
 

2.2 register a Type Library
Enter regasm testcom. dll/TLB: testcom. TLB/codebase to import the Type Library to the Registry. A message indicating that the type is successfully registered indicates that the operation is successful. In this case, testcom. dll can be used as a COM.

2.3 add DLL to GAC
Enter gacutil/I testcom. dll and set this. netProgramSet to GAC.
3 VB call Test
3.1 create a VB Project
Create a standard EXE project in VB. Select reference from the project menu, and find testcom in the pop-up reference COM Object List.
Drag and Drop a button on the form, double-click the button, and enterCode:
Dim A as new testcom. class1
Msgbox A. Test
 

3.2 run the program
Start the project and click the button to bring up the dialog box "OK", indicating that the call is successful.
 
 
4 ASP call Test
4.1 create an ASP file
Create a text file in the root directory of IIS and change it to 1.asp. enter the following content:
<%
Set S = Createobject ("testcom. class1 ")
Response. Write (S. Test ())
%>

4.2 run the program
Access this file in IE, and the page output is OK, indicating that the call is successful.
 

Related Article

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.