Use Queued Components in Delphi

zhaozj2021-02-17  61

Use Queued Components in Delphi

Queued Components are a COM technology based on Microsoft Message Queuing Services. It provides a simple asynchronous reference and method of executing components. The sender and the receiver don't think about whether the other party is now available and can be run independently.

As a COM technology. The queue refers to a space for storing a message to retrieve in the future. The queue provides a non-connected communication mechanism (ie, the sender and the pick-up side are not directly connected, but to communicate with each other through the queue). The queue is responsible for storage information until the receiver is ready. Since the sender and the receiver are not directly communicating, it can be operated independently without affecting both sides.

The following example shows how to create and use queue components using Delphi.

First, we will create a server. All methods in the interface must use only IN parameters and cannot have a return value. This is because there is no direct connection between the client and the server. Because it is not necessary to know when the request will be executed, the client cannot wait for a response from the server.

In Delphi, first open File / New / Other / ActiveX, create an ActiveX Library, then join an Automation object. Take a name for your server object, such as qdcomponent, other options use the default value, click OK. You will see Type Library Editor, add a method, name Task1, add a parameter to this method, Named TaskMessage, set its type BSTR (ie the wideString type in Delphi). If Type Library Options are set to show Pascal Code, then your method declares that the code should be similar to the following code:

Procedure Task1 (TaskMessage: WideString) [Dispid $ 00000001]; SaFECALL;

Save the project file for myqdserver, the unit file is QDServer.

The specific code is as follows:

Unit QDServer;

{WARN SYMBOL_PLATFORM OFF}

Interface

Uses Comobj, ActiveX, MyqdServer_TLB, STDVCL, Dialogs;

Type

TQDComponent = Class (Tautoobject, Iqdcomponent)

Protected

Procedure Task1: WideString; SaFECALL;

END;

IMPLEMENTION

Uses Comserv;

Procedure tqdcomponent.task1 (const taskmessag: wideString);

Begin

ShowMessage (TaskMessage);

END;

Initialization

TautoobjectFactory.create (Comserver, TqdComponent, Class_qdcomponent

CIMULTIINSTANCE, TMAPARTMENT);

End.

Install server

Open the component service tool (Component Services Tool) in the control panel, find "COM Application" in the file tree, then right click, select New / Application to select "Create An Empty Applcation" to find your new program according to the prompt, right click It goes to the property page, find the queuing column, and select two items in the check box. Queued 2.ListenTen.Listen indicates any message that is pressed into the queue. When it is hit, it will be handled immediately. Under your new program, right-click "Components" to select New / Component, select "Install New Component (s), find and install your DLL file, then expand the file tree to find" Interface ", right-click IQDComponent, Open the property page, select "Queued" in the queuing column (if you see the error message "MSMQ IS Not Running") Then that you don't have MSMQ because MSMQ is not the default Windows 2000 installation option. Create a client

Create a new Delphi application, add a MyqdServer_tlb.pas unit file in the USES clause, add a button and a text box.

You need to create your object when running, so use a symbol variable to tag component information, this parameter declares and uses in the FormShow event handler.

The specific code is as follows:

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, Stdctrls, Comobj, ActiveX, Myqdsever_TLB,

Type

TFORM1 = Class (TFORM)

Teder;

Button1: tbutton;

Procedure Button1Click (Sender: TOBJECT);

Procedure FormShow (Sender: TOBJECT);

Private

Qinterface: iqdcomponent;

Public

{Public declarations}

END;

VAR

FORM1: TFORM1;

IMPLEMENTION

{$ R * .dfm}

Function newcogetObject (pazname: pwidechar; pbindoptions: pbindopts;

Const IID: TIID; OUT PPV): HRESULT; stdcall; external 'ole32.dll' Name'CogetOject '

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

Qinterface.Task1 (Edit1.Text);

END;

Procedure TFORM1.FORMSHOW (Sender: TOBJECT);

VAR

QMONIKER: PWIDECHAR;

Begin

QMONIKER: = 'Queue: / New {server GUID or Server.InterfaceName}';

Olecheck (NewcogetObject (QMonikeTObject (QMonikeTObject (QMonikeTObject (QMonikeTObject (QMonikeTOBject (QMonikeTOBJECT);

END;

End.

Run the client program, then send a message, because you didn't start the server at this time, this message is not displayed, the server needs to start the server, and the easiest way is to open the component service in the control panel (Component Services) ), Find your server, right click to select startup. # This is my first translation work, please let me know.

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

New Post(0)