To create a COM object in C #

Source: Internet
Author: User
Tags definition bool class definition

In this article, we will discuss the following questions:

• Create a simple COM object using C # (using COM's interop attributes).

• Access COM from VC + + client software. The client software uses the TypeLibrary (. tlb file).

For simplicity and convenience for developers to use and test, we used the Northwind database in the default installation of SQL Server database software.

• Modify the name of SQL Server in a COM object and connect to SQL Server.

• We have created the user name and password for the connection database for Scott, Tiger, and we can use it or other existing username and password.

Part I: Creating a simple COM object with C #

A COM object is a classlibrary class that generates a DLL file. To create a simple COM object in the VS development environment, we can select "File"->, "newly created"->, "project"->, "visualc# project"->, "Class Library", and then create a project with the name Database_comobject.

Note that the following conditions are required to invoke the Vc# object in COM:

• The class must be public in nature.

• Features, methods, and events must be public in nature.

• Attributes and methods must be defined in the class interface.

• Events must be defined in the event interface.

Class members that are not of the public nature defined in these interfaces cannot be accessed by COM, but they can be accessed by other. NET Framework objects. To enable COM to access features and methods, we must define them in the class interface so that they have DISPID attributes and implement them in the class. The order in which these members are defined is the order in which they are in COM. To allow COM access to events in a class, you must define these events in the event interface and give them DISPID properties. The event interface should not be completed by the class, class implements only the class interface (it can implement more than one interface, but the first interface is the default interface), the methods and attributes that need to be made available for COM access should be implemented in the default interface, and methods and attributes must be identified as public and conform to the definition in the class interface. Events that require COM access are also completed in the default class interface, which must also be identified as public and conform to the definition in the event interface.

Before the interface name, each interface requires a GUID attribute. To generate a unique GUID, you need to run the Guidgen.exe tool software and select "Sign-up form" 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:

// 事件接口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 that in front of the class, you need to set the following attributes:

ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(DBCOM_Events))]

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.