milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
      • Côte d'Azur 2024
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. Delphi
  4. MSMQ

Microsoft Message Queuing

Details
Written by: Stanko Milosev
Category: MSMQ
Published: 09 December 2008
Last Updated: 10 December 2009
Hits: 8082

First of all you have to install MSMQ. You will do it through Control panel->Add remove programs->Add/Remove Windows components.

Second you will need to access MSMQ for managing, you can do it through Computer Management->Services and Applications->Message Queuing

Now, create one queue, for example "test", under Private Queues.

Then, send a message through Delphi :)

Here is the code:

For sending messages:

procedure TForm3.Button1Click(Sender: TObject);
var
  queuename, subject: string;
  data: olevariant;
  queueinfo, QueueInfosObj: IMSMQQueueInfo;
  queue, QueryObj: IMSMQQueue;
  msg: IMSMQMessage;
  tr: OLEVariant;
begin
{ STANKO is name of my computer, test is name of queue}
  queuename := 'Direct=OS:STANKOprivate$ est';
  subject := Edit1.text;
  data := Memo1.text;

  try
    queueinfo := CreateCOMObject (CLASS_MSMQQueueInfo) as IMSMQQueueInfo;
    queueinfo.FormatName := queuename;
    queue := queueinfo.Open (MQ_SEND_ACCESS, MQ_DENY_NONE);
    if queue.IsOpen 1 then
      statusbar1.Panels[0].Text := 'I can not get access to queue!'
   else
begin
  try
  msg := CreateCOMObject (CLASS_MSMQMessage) as IMSMQMessage;
  msg.label_ := subject;
  msg.body := data;
  tr := MQ_NO_TRANSACTION;
  msg.Send (queue, tr);
  statusbar1.Panels[0].Text := 'Sent!'
finally
  queue.close;
end;
  end;
  except
  on e: exception do showmessage (e.message);
  end;
end;

Now, for receiving messages:

procedure TForm3.Button2Click(Sender: TObject);
var
queuename: string;
data: olevariant;
queueinfo: IMSMQQueueInfo;
queue: IMSMQQueue;
msg: IMSMQMessage;
tr, wdq, wb, rt: OLEVariant;
begin
queuename:= 'Direct=OS:STANKOprivate$ est';
try
  queueinfo := CreateCOMObject (CLASS_MSMQQueueInfo) as IMSMQQueueInfo;
  queueinfo.FormatName := queuename;
  queue := queueinfo.Open (MQ_RECEIVE_ACCESS, MQ_DENY_NONE);
  if queue.IsOpen 1 then
  statusbar1.Panels[0].Text := 'I can not get access to queue!'
  else
  begin
  try
  tr := MQ_SINGLE_MESSAGE;
  wdq := False;
  wb := True;
  rt := 1000;
  msg := queue.Receive (tr, wdq, wb, rt);
  if msg nil then
  begin
  data := msg.body;
  statusbar1.Panels[0].Text := 'Received!';
  memo2.text:=data;
  edit2.text:=msg.label_;
  end
  else
  statusbar1.Panels[0].Text := 'No messages'
  finally
  queue.close;
  end;
  end;
except
  on e: exception do showmessage (e.message);
end;
end;

Code taken from here. It is on Slovenian, but I coudn't find it on English.

Here you can download source code created in D2007, and here is the exe file.

Page 2 of 2

  • 1
  • 2