Create a COM object with C #

xiaoxiao2021-03-06  74

In this article, we will discuss the following questions: • Create a simple COM object using COM (using COM's interoP features). · Access COM from VC client software. Client software uses TypeLibrary (.tlb file). For the sake of simplicity and convenience of developers, we use the Northwind database in the default installation of the SQL Server database software. · Modify the name of SQLServer in the COM object and connect to SQLServer. · We have created connection databases for the username and password of Scott, Tiger, and we can use it or other existing usernames and passwords. The first part: Creating a simple COM object with COM object is a ClassLibrary class, which generates a DLL file. To create a simple COM object in the VS development environment, we can select "File" -> "New Create" -> "Project" -> "Visualc # Project" -> "Class Library" -> "Class Library" - Engineering of Database_Comobject. It should be noted that the following conditions need to be called in the COM: • Class must be the PUBLIC property. · Features, methods and events must be in the PUBLIC property. · Features and methods must be defined in class interfaces. • The event must be defined in an event interface. A member of the PUBLIC property defined in these interfaces cannot be accessed by COM, but they can be accessed by other .NET Framework objects. To allow COM to access features and methods, we must define them in class interfaces to have a Dispid property and implement these features and methods in the class. The order in these members definitions is the order in which they are in COM. To let the COM access, you must define these events in an event interface and give them the Dispid property. The event interface should not be completed by the class, the class only the class interface (it can implement an interface, but the first interface is the default interface), and the method and characteristics, methods, methods, methods that need to make COM access should be implemented in the default interface. Features must be identified as public properties and conform to definitions in class interfaces. Events that need to be accessible are also completed in the default class interface, and they must also be identified as public properties and meet the definitions in the event interface. Before the interface name, each interface requires a GUID feature.

To generate a unique GUID, you need to run the GUIDgen.exe tool software and select "Registry Format" below is a class interface: [GUID ("694c1820-04b6-4988-928f-fd858b95c880")] public interface dbcom_interface { DispId (1)] void Init (string userid, string password); [DispId (2)] bool ExecuteSelectCommand (string selCommand); [DispId (3)] bool NextRow (); [DispId (4)] void ExecuteNonSelectCommand (string insCommand ); [DispId (5)] string GetColumnData (int pos);} COM event interface: // event interface Database_COMObjectEvents [Guid ( "47C976E0-C208-4740-AC42-41212D3C34F0"), InterfaceType (ComInterfaceType.InterfaceIsIDispatch)] public interface DBCOM_Events {} the following is the actual class definition: [Guid ( "9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E"), ClassInterface (ClassInterfaceType.None), ComSourceInterfaces (typeof (DBCOM_Events))] public class DBCOM_Class: DBCOM_Interface {Note Yes, in front of the class, you need to set the following characteristics: ClassInterface (ClassInterFactype.none), CosourceInterfaces (TypeOf (dbcom_events))] ClassInterFaceType.none means that there is no class interface for this class, if the interface is not explicitly implemented, the class can only Provide later binding access by iDispatch. The user hopes that the external object can access the class's function by explicitly enabling the interface, which is also the setting of the recommended ClassInterFacettribute. COMSourceInterfaces (TypeOf (dbcom_events))] Determines many interfaces provided to the external object as a COM event. In the examples of this article, we don't open any events for external objects.

Here is the complete source code COM object: using System; using System.Runtime.InteropServices; using System.IO; using System.Text; using System.Data.SqlClient; using System.Windows.Forms; namespace Database_COMObject {[Guid ( " 694C1820-04B6-4988-928F-FD858B95C880 ")] public interface DBCOM_Interface {[DispId (1)] void Init (string userid, string password); [DispId (2)] bool ExecuteSelectCommand (string selCommand); [DispId (3) ] bool NextRow (); [DispId (4)] void ExecuteNonSelectCommand (string insCommand); [DispId (5)] string GetColumnData (int pos);} // event interface Database_COMObjectEvents [Guid ( "47C976E0-C208-4740-AC42- 41212D3C34F0 "), InterfaceType (ComInterfaceType.InterfaceIsIDispatch)] public interface DBCOM_Events {} [Guid (" 9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E "), ClassInterface (ClassInterfaceType.None), ComSourceInterfaces (typeof (DBCOM_Events))] public class DBCOM_Class : Dbcom_interface {private sqlConnection myconnection = null; sqldataareader myreader = null; public dbcom_class () {} public void init (String UserID, string password) {try {string myConnectString = "user id =" userid "; password =" password "; Database = NorthWind; Server = SKYWALKER; Connect Timeout = 30"; myConnection = new SqlConnection (myConnectString); myConnection.Open (); MessageBox.Show ( "CONNECTED");} catch (Exception e) {MessageBox.Show (e.Message);}} public bool ExecuteSelectCommand (string selCommand) {if (! myReader = null) myReader.Close () SQLCommand mycommand = new sqlcommand; mycommand.connection = myconnection; mycommand.executenonQuery (); myreader = mycommand.executeReader (); returntrue;

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

New Post(0)