Mega Code Archive

 
Categories / Delphi / Examples
 

MSMQ

Title: MSMQ Question: Using Microsoft Message Queuing with Delphi. Answer: 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: procedure TForm3.btnGetClick(Sender: TObject); var queueinfo: IMSMQQueueInfo; queue: IMSMQQueue; msg: IMSMQMessage; tr, wdq, wb, rt: OLEVariant; begin memGetData.Clear; edtGetSubject.text := ''; sbGet.Panels[0].Text := ''; try queueinfo := CreateCOMObject (CLASS_MSMQQueueInfo) as IMSMQQueueInfo; queueinfo.FormatName := edtGetQName.Text; queue := queueinfo.Open (MQ_RECEIVE_ACCESS, MQ_DENY_NONE); if queue.IsOpen 1 then sbGet.Panels[0].Text := 'Can''t 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 memGetData.text := msg.body; edtGetSubject.text := msg.label_; sbGet.Panels[0].Text := 'Received!'; end else sbGet.Panels[0].Text := 'No messages!' finally queue.close; end; end; except on e: exception do showmessage (e.message); end; end; procedure TForm3.btnSendClick(Sender: TObject); var queueinfo: IMSMQQueueInfo; queue: IMSMQQueue; msg: IMSMQMessage; tr: OLEVariant; begin try queueinfo := CreateCOMObject (CLASS_MSMQQueueInfo) as IMSMQQueueInfo; queueinfo.FormatName := edtSndQName.Text; queue := queueinfo.Open (MQ_SEND_ACCESS, MQ_DENY_NONE); if queue.IsOpen 1 then sbSend.Panels[0].Text := 'Can''t get access to queue.' else begin try msg := CreateCOMObject (CLASS_MSMQMessage) as IMSMQMessage; msg.label_ := edtSndSubject.Text; msg.body := memSndData.Text; tr := MQ_NO_TRANSACTION; msg.Send (queue, tr); sbSend.Panels[0].Text := 'Sent!' finally queue.close; end; end; except on e: exception do showmessage (e.message); end; end;