Develop COM + components using C #

xiaoxiao2021-03-06  17

.NET technology is the next-generation platform technology that Microsoft vigorously promotes, since the official release of the .NET technology architecture beta2 version, this technology is gradually maturing and stabilizing. According to Microsoft's platform system, we are not difficult to imagine, in the next two years. NET technology must be trending the mainstream technical platform, and a new technical platform is the most important premise of rapid development is : He will not completely abandon the previous technology, this point is COM / COM technology for .NET technology.

In general, in the IT technology circle and hardware industry, the upgrade speed of technology is very amazing, and the practice is all new technologies to follow the principle of downward compatibility, but .NET technology not only did this, .NET or even Implementation of each call between each other, this is very difficult to be valuable. That is, not only we can call COM components in the .NET component, but also call the .NET component in the COM component. The benefits of this point are obvious, on the one hand, we can maintain existing technical resources, on the other hand, in the existing resources, can take advantage of the various new technologies brought by .NET.

Generate a GUID with the SN command in the E / CLASSLIBRARY1 / directory, as shown below:

Sn -k keyfile.snk

Add the following statement in the project's assemblyinfo.cs file:

[assembly: assemblykeyKeyfile (@ "e: /classlibrary1/keyfile.snk")]]

The role is to make the DLL file created by this project file with GUID (global unique identifier), which is a strong name, which is COM requirements.

The detailed code of the CS is as follows:

using System; using System.Runtime.InteropServices; // call to GUIDusing System.EnterpriseServices; // reference to a class using System.Web.Services COM ; namespace ClassLibrary1 {// If no guid, randomly generated every time CLSID [Guid ( "539448DE-9F3B-4781-A1F6-F3C852091FC9")] public interface ISharpObject2 {string Name // Property: Name, Get / Set {get; set;} void Test ();} public class Class1: ServicedComponent, ISharpObject2 {public class1 () {// // TODO: Add constructor logic here //} private string m_strName; public string Name // Property: Name, Get / Set {get {return m_strName;} set {m_strName = value;}} public void Test () {}}} After generating a DLL file, there can be three ways. Create a COM application in the COM management server, where the method used here is to use the command under DOS operation.

Regsvcs / c class1.dll mycom "Mycom is the COM app name to generate"

转载请注明原文地址:https://www.9cbs.com/read-44202.html

New Post(0)