Use C # to create a COM object (from Computer World)
Source: Internet
Author: User
In this article, we will discuss the following issues:
· Use C # to create a simple COM object (using the Interop feature of COM ).
· Access COM from the VC ++ client. The client software uses the TypeLibrary (. TLB file ).
To make it simple and convenient for developers to use and test, we use the Northwind database in the default installation of SQL Server database software.
· Modify the SQLServer name in the COM object and connect to SQLServer.
· We have created the usernames and passwords used to connect to the database for scott and tiger respectively. We can use them or other existing usernames and passwords.
Part 1: Use C # to create a simple COM object
The COM object is a ClassLibrary class that generates DLL files. To create a simple COM object in the VS development environment, select "file"> "new"> "project"> "VisualC # Project"> "class library", and create a project named Database_COMObject.
Note that calling the VC # object in COM requires the following conditions:
· The class must be public.
· Features, methods, and events must be public.
· Features and methods must be defined in class interfaces.
· Events must be defined in the event interface.
Not the public class members 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 the DispId attribute and implement these features and methods in the class. The order in which these members are defined is the order in COM. To enable events in the COM domains class, you must define these events in the event interface and assign them the DispId attribute. The event interface should not be completed by the class, and the class only implements the class interface (it can implement more than one interface, but the first interface is the default interface ), methods and features that need to be accessed by COM should be implemented in the default interface. Methods and features must be identified as public and comply with the definition in the class interface. Events that need to be accessed by COM are also completed in the default class interface. They must also be identified as public and comply with the definitions in the event interface.
Each interface requires a GUID before the interface name. To generate a unique guid, run the guidgen.exe tool and select "Registry Format"
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.