. Net DLL is not a dynamic link library, but a Type Library. CCW is required to call it as COM. The following is an instance.
First, create the project comlib of A Class Library and write an interface. the object to be called by com implements this interface and a class to implement this interface, if you want to create an object using Createobject in VB or ASP, you need a constructor without parameters. The Code is as follows:
Using system; using system. windows. forms; namespace comlib {// defines the interface. This is very important (the methods in this interface are the same as those in the COM component) public interface iclass1 {void say (string word );} public class class1: iclass1 {public void say (string word) {MessageBox. show (Word );}}} |
Run the Visual Studio 2005. Net command prompt and run Sn-K comlib. SNK to generate an SNK signature file comlib. SNK. To make the type visible in COM, you need to set this [Assembly: comvisible (true)] In the assemblyinfo. CS file. Add a [Assembly: assemblykeyfile ("file path \ comlib. SNK ")] In Solution Explorer, select project> right-click and choose Properties> Generate tab> Register com InterOP, in this way, the COM component is automatically registered when the project is created. After the project is generated, you can call COM. Here, we use VB as an example to create an EXE project and add references to comlib. TLB. Some files are in the bin directory. Then use the following code to call
Option explicit private sub form_load () dim IC as iclass1 set Ic = new class1 IC. Say "Hello Jerry! "'Use this method in ASP dim a set a = Createobject (" comlib. class1 ") end sub |
A message box appears after the project is run. Indicates that the call is successful. The above com registration is automatic registration, but sometimes manual registration is required. For example, if you want to use the Type Library on another computer, there is no Visual Studio 2005 on that computer. To manually register an assembly, follow these steps: (1) Use the regasm tool to register the Assembly, run the Visual Studio 2003. Net command prompt, and run regasm comlib. dll/TLB: comlib. TLB.
(2) Use the gacutil tool to install the DLL assembly to the Global Assembly Cache. Run the Visual Studio 2003. Net command prompt and then run gacutil/I comlib. dll.
Attached source file download: clsin.rar