Delphi writing COM + component knowledge and examples

Source: Internet
Author: User

first, the basic knowledge of COM components introduction:

1, what is COM

COM is a Microsoft set up a component of the specification, the purpose is to achieve the reuse of components, whether you are in C, DELPHI, VB what language to write, as long as the observance of this specification is COM components, can be invoked between each other. What is this specification, the simplest is to use the interface according to the rules, Microsoft has set a IUnkown interface, as long as the implementation of the interface of the class, can be called a COM component. On this basis, you can add your own interface to achieve a specific function, which becomes a useful COM component.

2, COM, DCOM, COM +

COM is a binary based standard. For example, we use Delphi to achieve an object, under normal circumstances, we can only in Delphi to generate this object instance and call, and if we use Delphi to achieve a COM object, we can use VC, VB or any other language that supports COM objects to generate instances and invocations. Conversely, we can use a variety of COM objects in Delphi, without mind what language it is written in. COM provides a mechanism for distributed COM objects, which graphically says that you can invoke COM objects from another machine. COM + is an upgrade of MTS, which provides further transaction processing and many other pool technologies on a COM basis.

2. How to create a COM component in Delphi (two steps)

(1) Creating an ActiveX Library, which is a DLL; ActiveX tab

(2), create a COM object or Automation object, both of which are COM components, but of course there is a difference, the difference in the method of invocation, this is followed. "ActiveX tab";

(An ActiveX Library, named Mylib, was created.) A automation Object, named MyObj. and compile the registration. )

Note: COM object can only be invoked through an interface, automation object to call an interface or name.

3, Delphi How to invoke the COM component (two method calls),

The first method (invoked with an interface)
To support the interface of the language, such as C, DELPHI;VB, the advantage is fast, because you do not have to search by name, as well as the development of the compiler can check, the disadvantage is to import the type library.

The following actions are required:

Add the Mylib_tlb type library to the use (if you did not develop a TLB that you can import from the DLL), the code is var myobj:imyobj;myobj=comyobj.create (); If you are interested in looking at the type library code, Comyobj.create calls Createcomobject, which returns the IUnkown interface and then uses the as operation to convert to the Imyobj interface you need.

The second method (called by name)
For all languages, the advantage is not to import the type, the disadvantage is that the speed of the call is slow, and the development of the compiler does not check.

The second method does not require a type library, the code is var myobj:variant;myobj=createoleobject (' Mylib.myobj '), then you can use it, this is convenient. In fact, in order to achieve the call with the name, need to add IDispatch interface, but the Delphi is automatically added to us, do not use the tube directly.

1>, IDispatch is COM object interface, in Delphi usually refers to a oleobject.

2>, Olevariant is a COM object-compatible variant that can be common to any OLE Automation type and is compatible with IDispatch created by Createoleobject.

(COM component writing is similar to COM + component writing, so write only COM + components)

Second, the specific examples prepared by COM +:

1, the preparation of COM +:

1>, File---->new---->other .... Transactional Object under---->activex Library label

2>, then fill in: Coclss Name: Class names, such as: complusthreading Modal: Threading mode: Apartment option: Supports transactions

When establishing COM +, the transaction mode selected is requires a Transaction, COM + will set up the corresponding transaction according to the customer's request, not only the database, but also the system resources. Successful SetComplete. Rollback SetAbort. Selecting requires a transaction means that when the user invokes the COM + component, the COM + environment creates a new transaction context for the component, which is not the same as the transaction of the database. If an error occurs when your COM + component submits data, you should tell the transaction context, as long as you invoke the SetAbort method of the COM + component. As a result, all COM + components that are in the same transaction context will be rollback. If the data submission succeeds, you should call SetComplete, or you can do so, because by default, the transaction state of the COM + component is set to Enablecommite. The transaction context will actually submit data to the database when all COM + component objects that are in the context of the same transaction call SetComplete.

3> and then add the method in the View--->library dialog box Note: If the parameter is output, the type is pointer type, for example: Long *, and then the following parameters are modified In:out,ret

4>, finally improve the method of the increase is OK

2, the client calls to write:

1>, the interface type of COM + is dumped first. Project--->import type Library----> select the type of COM + you wrote, and then select: Create unit

3. There are two ways to install COM + components.

1>, first (recommended): If you are in an IDE environment, click on "run->install COM + Objects" To install an active library project that is open in the COM + environment, note: If the Open project is A common application project that cannot be installed in a COM + environment. Check the COM + that will be installed, then there are two options in application: Install to existing application: Indicates which package of COM server your COM + is installed in, install to New Application: Represents the installation of the current COM + component into a new component package.

2>, the second way: Open Control Panel-> management Tools-> Component Services-> computer-> My Computer->com+ application, right-click on the COM + application's tree item, select "New-> Application"-> Create an empty application and name the application, then click Next until the end. After an empty COM + application is established, the next step is to install the COM DLL into a COM + application. Create a new component in the tree item of the empty application you just created, to install a new component, select the DLL file to be installed in the COM + environment in the Open File dialog box, and then follow the wizard, and you can simply repeat the steps for installing multiple COM DLLs into the same COM + application package.

4. Export Client Component Packages

Exporting a client component package is the export of a registered component to an. msi-formatted file that, after the client is installed, will only register the component on the client without installing the redundant files. If the component is not registered with the client, it is not possible to invoke components located on the server (this refers to servers and clients distributed across different machines).

5, debugging COM + program---OK

1>, open Component Management in Windows, find the component package to debug, right-click, select Attributes, select Debug options in the Advanced page, tick; Then locate the/processid:{xxxxxxxxxxxxxxxxxxxxxxxxxxxxx in the debug path below and copy it

2>, run in Dephi | Parameters ... HOST application fill in {System path}\system32\dllhost.exe PARAMETERS sticky Towel/processid:{xxxxxxxxxxxxxxxxxxxxxxxxxxxxx}

3>, a key point: Component program: project|option|linker| Include TD32 debug info and include Remote debug symbols check

4>, start Delphi, run the COM + program you want to debug, set breakpoints, and then run the client program to enter COM + breakpoints.

5>, after debugging, remember to be in the Windows Component management in the Advanced this page debugging options check out yo.

6, COM + need to note the place:

1>, client run will report interface not supported error. General Reason: COM + 's permissions depend on the permissions configuration of Windows, and the client's username and password are required on the server. If not, reinstall COM + on the server, and then export again.

COM + cannot be included in the engineering group when the 2> is established. (My Practice)

3>, COM + does not support Oracle. Error while using transaction: Using Oracle with Microsoft Transaction Server and COM +

7. Add a remote Data module to COM +

1>, File---->new---->other .... Transactional Data Module under---->multitier label

2>, and then add the method in the View--->library dialog box.

8. Passing Arrays in COM +

1>, defining an array first:

Type Tdatarecord = Record

A:byte;

B:longword;

C:word;

D:longword;

End

2>, server-side:

function getdata:olevariant;

Var

P:pointer;

Begin

Varclear (result);/Do you know D5?

Try

Result: = Vararraycreate ([0, SizeOf (tdatatype)], varbyte);

P: = Vararraylock (Result); Data:tdatatype for the record type you want to pass

Move (Data, p^, SizeOf (Tdatatype)); Pass the value of data to the variant type that P points to

Finally

Vararrayunlock (result);

End

End

3>, client:

Procedure GetFile (const filename:string);

Var

P:pointer;

V:olevariant;

Data:tdatatype;

Begin

Fillchar (data, SizeOf (data), 0);

V: = SocketConnection1.AppServer.GetData;

Try

P: = Vararraylock (V);

Move (p^, data, SizeOf (data));

Finally

Vararrayunlock (V);

End

End

9, COM + to pass the recordset

Server side:

Uses Adoint;

You can send ADO data as a variant of variable type: Adodataset1.recordset this is native ADO data

This is the code for a method on the server side: Change the CodeSet type to variant*[in,out]

function TADORec.getData:OleVariant;

Begin

Adodataset1.open;

Result: = Adodataset1. RecordSet;

End

You can transfer ADO data as a variant of variable type: Adodataset1.recordset this is native ADO data

Client

Uses adoint;

Var

Myrecordset: _recordset;

Begin

Myrecordset: = IUnknown (codeset) as _recordset

End;

10, COM has the need to study and have questions about the place:

1, COM + mode:

2, to COM + resources pooling mechanism

3, the use of delphi6 to develop COM +, with neutral mode, as far as possible with the object pooling, of course, is to have no state, the transaction should be as short as possible, to avoid deadlock, with ADO do not use bde.dbexpress and so on.

4, my approach is to divide the components by function, the query and update into two functional components, because the query does not require transactions, so long as the support of the transaction on the line. Updates generally require transactions.

5. COM + 's Transaction properties COM + 's transaction default level is the sequence read, not the commit read we think, and the isolation level we set adoconnection is not working, and can only be enforced with explicit SQL. For example: a middle tier com:select * from a where py_code like ' s% ' continues to have a lot of code underneath; another middle tier com:update a set py_code=py_code where Py_code like ' b% '; At this time this COM will deadlock wait, although the change is not a data will also deadlock with commit read or uncommit read will not recommend: so be updated or read the table by multiple subsystems, to be opened and updated at the end, not early. You can also force a change in the isolation level of adoconnection

6. How do transactions be handled in COM +? In COM +, how do I use SetComplete and SetAbort for transaction management? How SetComplete and SetAbort can be invoked on the client. This is definitely not going to work. To guarantee a transaction, declare a specially saved interface method on the server side, for example:

Procedure Updatemydata (Var AData1, Adata2:olevariant; var amaxerror, Aerrorcount:integer); Begin

Try

Datasetprovider1.data: = AData1;

Datasetprovider1.applyupdate (Amaxerror, Aerrorcount);

Datasetprovider2.data: = AData2;

Datasetprovider2.applyupdate (Amaxerror, Aerrorcount);

SetComplete;

Except

SetAbort;

End

End

7, in the component Manager in the "transaction list" can not see the transaction (there should be transactions), how to see?

8, COM + 's two major studies: Business and pooling

9. Make a submit COM + object and a coordinated COM + object.

10, recommendations: Multiple DLLs in a package, a DLL in the COM common one adoconnection

   11, question: I have set true in the pooled attribute of transactional Data module, but in the "Activation" column of component properties of Win2000 Component Services management, it still cannot be opened. Enable object sharing Option---> You can set the threading mode to either Tmneutral or Tmboth.

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.

Tags Index: