- Details
- Written by: Stanko Milosev
- Category: MSMQ
- Hits: 7698
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.
- Details
- Written by: Stanko Milosev
- Category: Oracle
- Hits: 8186
- Details
- Written by: Stanko Milosev
- Category: Oracle
- Hits: 5920