代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;
type
MQDataInfo = record
ID : Integer;
Name : PAnsiChar;
Value : PAnsiChar;
Image : TBitmap;
end;
PMQDataInfo = ^MQDataInfo;
type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
LabeledEdit1: TLabeledEdit;
LabeledEdit2: TLabeledEdit;
LabeledEdit3: TLabeledEdit;
Button2: TButton;
Button3: TButton;
Image1: TImage;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses MQI;
const
QM_NAME : MQCHAR48 = 'MyTest'#0;
QUEUE_NAME : MQCHAR48 = 'Test001'#0;
procedure TForm1.Button1Click(Sender: TObject);
var
Hconn : MQHCONN;
Hobj : MQHOBJ;
ConnectOpts : MQCNO;
QMgrName : MQCHAR48;
ObjDesc : MQOD;
Options, CompCode, Reason : MQLONG;
Buffer : MQDataInfo; //PAnsiChar;//Array[0..1024000] of Byte;
BufferLength : Integer;
MsgDesc : MQMD;
PutMsgOptions : MQPMO;
begin
QMgrName := QM_NAME;
ConnectOpts := MQCNO_DEFAULT;
MQCONNX ( @QMgrName, @ConnectOpts, @HConn, @Compcode, @Reason);
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Connect Failed');
end
else
begin
Self.Memo1.Lines.Add('Connected successed');
end;
Options := MQOO_FAIL_IF_QUIESCING + MQOO_OUTPUT;
ObjDesc := MQOD_DEFAULT;
ObjDesc.ObjectName := QUEUE_NAME;
MQOPEN (HConn, @ObjDesc, Options, @HObj, @Compcode, @Reason );
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Open Failed');
end
else
begin
Self.Memo1.Lines.Add('Open Successed');
end;
MsgDesc := MQMD_DEFAULT;
PutMsgOptions := MQPMO_DEFAULT;
Buffer.ID := 100;
Buffer.Name := 'Name001'#0;
Buffer.Value := 'Value001'#0;
Buffer.Image := TBitmap.Create;
Buffer.Image.Assign(Self.Image1.Picture.Bitmap);
BufferLength := sizeof(Buffer);
MQPUT(HConn, Hobj, @MsgDesc, @PutMsgOptions, BufferLength, @Buffer, @Compcode, @Reason);
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Put Failed');
end
else
begin
Self.Memo1.Lines.Add('Put Successed');
end;
Options := MQCO_NONE;
MQCLOSE ( HConn, @Hobj, Options, @Compcode, @Reason);
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Close Failed');
end
else
begin
Self.Memo1.Lines.Add('Close Successed');
end;
MQDISC ( @HConn, @Compcode, @Reason );
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Disconnect Failed');
end
else
begin
Self.Memo1.Lines.Add('Disconnected successed');
end;
Self.Memo1.Lines.Add('----------------------');
end;
procedure TForm1.Button2Click(Sender: TObject);
var
Hconn : MQHCONN;
Hobj : MQHOBJ;
ConnectOpts : MQCNO;
QMgrName : MQCHAR48;
ObjDesc : MQOD;
Options, CompCode, Reason : MQLONG;
Buffer : MQDataInfo;// Array[0..1024000] of Char;
BufferLength : Integer;
MsgDesc : MQMD;
GetMsgOptions : MQGMO;
DataLength : MQLONG;
begin
QMgrName := QM_NAME;
ConnectOpts := MQCNO_DEFAULT;
MQCONNX ( @QMgrName, @ConnectOpts, @HConn, @Compcode, @Reason);
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Connect Failed');
end
else
begin
Self.Memo1.Lines.Add('Connected successed');
end;
Options := MQOO_FAIL_IF_QUIESCING + MQOO_INPUT_AS_Q_DEF;
ObjDesc := MQOD_DEFAULT;
ObjDesc.ObjectName := QUEUE_NAME;
MQOPEN (HConn, @ObjDesc, Options, @HObj, @Compcode, @Reason );
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Open Failed');
end
else
begin
Self.Memo1.Lines.Add('Open Successed');
end;
BufferLength := sizeof(Buffer);
DataLength := 0;
MsgDesc := MQMD_DEFAULT;
GetMsgOptions := MQGMO_DEFAULT;
MQGET(HConn, Hobj, @MsgDesc, @GetMsgOptions, BufferLength, @Buffer, @DataLength, @Compcode, @Reason);
if CompCode <> MQCC_OK then
begin
//队列无消息信息
if Reason = MQRC_NO_MSG_AVAILABLE then
begin
Self.Memo1.Lines.Add('The queue is empty');
end
else
begin
Self.Memo1.Lines.Add('Get Failed');
end;
end
else
begin
Self.LabeledEdit1.Text := IntToStr(Buffer.ID);
Self.LabeledEdit2.Text := Buffer.Name;
Self.LabeledEdit3.Text := Buffer.Value;
Self.Image1.Picture.Bitmap.Assign(Buffer.Image);
Self.Memo1.Lines.Add('Get Successed');
end;
Options := MQCO_NONE;
MQCLOSE ( HConn, @Hobj, Options, @Compcode, @Reason);
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Close Failed');
end
else
begin
Self.Memo1.Lines.Add('Close Successed');
end;
MQDISC ( @HConn, @Compcode, @Reason );
if CompCode <> MQCC_OK then
begin
Self.Memo1.Lines.Add('Disconnect Failed');
end
else
begin
Self.Memo1.Lines.Add('Disconnected successed');
end;
Self.Memo1.Lines.Add('----------------------');
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Self.LabeledEdit1.Text := '0';
Self.LabeledEdit2.Text := '';
Self.LabeledEdit3.Text := '';
Self.Image1.Picture.Assign(nil);
end;
end.