如何使用查找队列?
我使用 Delphi 2007,并尝试找出如何询问 Windows(XP、Server 2003 或 2008)是否安装了指定的 MSMQ 队列。我发现这个 但它是用 C++ 编写的,所以在 Delphi 中使用起来并不容易。例如,我安装了一个名为“.\private$\nctsinqueue”的队列。通过以下方式使用它效果很好:
var
QueueInfo : IMSMQQueueInfo2;
begin
QueueInfo := CoMSMQQueueInfo.Create;
问题是,在安装了我的应用程序的某些 Windows 安装中,此队列不存在。是否需要队列取决于偏好。所以我想询问 Windows 是否安装了命名队列,在这种情况下我可以继续上面的代码。
编辑: 尝试了这段代码
function Test: Boolean;
var
QueueInfo : IMSMQQueueInfo2;
begin
Result := True;
QueueInfo := CoMSMQQueueInfo.Create;
QueueInfo.PathName := '.\private$\nonexistingqueue';
FQueue := QueueInfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE);
end;
,它在最后一行引发了异常。我当然可以在这里尝试/例外并在这种情况下返回 False 但我不喜欢对此进行异常处理。我想问一下WinApi什么的队列是否存在。 kobik 建议的 Queue.IsOpen 仅表示是否打开现有队列。当然它必须存在才能打开。
编辑2: 我对此采取了更实用的方法,因此我使用应用程序的 ini 文件解决了这个问题。 仅当队列存在于 ini 文件中时,它才会尝试打开。 当然,缺点是 ini 文件必须与系统中的队列同步,但这部分是相当静态的。
I use Delphi 2007 and I try to find out how to ask Windows (XP, Server 2003 or 2008) if a named MSMQ queue is installed. I have found this but it is in C++ so it is not easy to use from Delphi. Example, I have an installed queue named '.\private$\nctsinqueue'. It works fine to use it by:
var
QueueInfo : IMSMQQueueInfo2;
begin
QueueInfo := CoMSMQQueueInfo.Create;
The problem is that in some installations of Windows where my application is installed this queue does not exists. It depend of the preferences if a queue is needed. So I want to ask Windows if a named queue is installed and in that case I can go on with the code above.
EDIT:
Tried this code
function Test: Boolean;
var
QueueInfo : IMSMQQueueInfo2;
begin
Result := True;
QueueInfo := CoMSMQQueueInfo.Create;
QueueInfo.PathName := '.\private$\nonexistingqueue';
FQueue := QueueInfo.Open(MQ_RECEIVE_ACCESS, MQ_DENY_NONE);
end;
And it raise an exception on the last line. I can of course have a try/except here and return False in that case but I don't like to have exceptionhandling for this. I want to ask WinApi or something if the queue exists. Queue.IsOpen that kobik suggest only says if an existing queue is opened. It must of course exist before it can be opened.
Edit2:
I take a more practical approach to this, so I solved it with ini-files for my application.
It tries to open only if the queue is present in the ini-file.
Disadvantage is of course that the ini-file must be in sync with the queues in the system, but that part is rather static.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论