We will introduce several methods for compiling COM components in C #, and give a detailed introduction to C # compiling COM components in. net. This includes generating. Net assembly, registering and creating a library, and so on.
Encrypt (string ptoencrypt, string skey) // encrypt decrypt (string ptodecrypt, string skey) // decrypt and write the code as follows:
Using system; using system. runtime. interopservices; namespace {// first create an interface. This is the [GUID ("canonical")] public interface iencrypt {string encrypt (string ptoencrypt, string skey) that must be used by COM ); string decrypt (string ptodecrypt, string skey);} // implementation of the write interface [GUID ("CB52E990-185E-4448-A7E8-C88ECAD563AB")] public class name: iencrypt {Public String encrypt (string ptoencrypt, string skey) {// copy the FAQ encryption code} Public String decrypt (string ptodecrypt, string skey) {// copy the FAQ decryption code} and package it in vs.net. DLL class library file, assuming the name is myencrypt. DLL
Use the following Tool
Regasm myencrypt. dll/TLB: myencrypt. TLB
This. TLB file is a type library and can be referenced by VB6 and VC ++ 6.
Note that the guid above is generated using the guid creation tool in the vs.net tool menu.
Note that assemblyinfo is automatically generated by vs.net. A strong name and version number must be added to the CS file. Because the COM component requires a version number, do not change the version number or use vs.net's automatic version number. * It is best to use such a version number.
A fixed version such as 1.1.1.1 must be added at a time and cannot be added multiple times.
Procedure
1. Net Assembly generation:
First you put http://expert.csdn.net/Expert/FAQ/FAQ_Index.asp? Copy the Code with ID = 35935.
Then you copy the code
Using system; using system. io; using system. text; using system. runtime. interopservices; using system. security. cryptography; namespace CSIC {// first establish the interface. This is the [GUID ("plaintext")] public interface iencrypt {string encrypt (string ptoencrypt, string skey) that must be used by COM ); string decrypt (string ptodecrypt, string skey);} // implementation of the write interface [GUID ("CB52E990-185E-4448-A7E8-C88ECAD563AB")] public class crypt: iencrypt {Public String encrypt (string ptoencrypt, string skey) {// copy the FAQ encryption code} Public String decrypt (string ptodecrypt, string skey) {// copy the FAQ decryption code} and save the above file as a class library project of the vs.net project.
Then use vs.net's "vsitual Studio. NET tool" --> vistual Studio. NET Command Prompt
In the command line
Cd c:/Sn-K mykey. SNK generates a strong name file named mykey. SNK in the root directory of the C drive, and closes the prompt window.
In the assemblyinfo. CS file automatically generated by the class library project in vs.net
[Assembly: assemblykeyfile ("")] changed
[Assembly: assemblykeyfile (@ "C:/mykey. SNK")] [Assembly: assemblyversion ("1. 0. *")]
Change
[Assembly: assemblyversion ("1.0.0.0")] // note: in this case, your COM component version is 1.0.0.0, and then press SHIFT + Ctrl + B to generate the dll library (in the release mode ), assume it is CSIC. crypt. DLL.
At this time, the Assembly is successfully created.
2. register the Assembly and create a Type Library
Still use the vistual Studio. NET command prompt in the Start Menu
Enter your project directory, for example, D:/myproject/bin/release.
In the dialog box, enter
D: CD myproject/bin/release and enter the Dir command to view the CSIC. crypt. dll file.
Enter regasm/tlb csic. crypt. tlb csic. crypt. dll
Then the CSIC. crypt. TLB library file is generated in this directory. Do not close this prompt window.
At this time, the. NET assembly of this. dll will become a standard COM component, but it cannot be used yet. It must be changed to a global COM component.
This regasm utility will create a Type Library and register it in the Windows registry, so that the classes in physserver2.dll can be used on the COM Client.
3. Add the assembly to the Global Assembly Cache
Enter the prompt window and enter
Gacutil/I CSIC. crypt. dll at this time, your dll will be copied to the Global Assembly Cache. That is to say, you can use this DLL component on any hard disk on this computer.
IV. C # How to compile COM components
ASP usage
Set OBJ = server. createobject ("CSIC. crypt ") dim str1 str1 = obj. encrypt ("content to be encrypted", "password") // encrypt dim str2 str2 = obj. decrypt ("content to be decrypted", "password") // decrypt