如何在不打开应用程序的新实例的情况下在运行应用程序中打开新文档?

时间:2022-10-30 23:45:34

I have a situation that has been partially covered by other answers at SO, but I cannot find a complete answer. In short, we are trying to use URL's for our specific data types that when double clicked will open up our application and load those data sets into that app. We have this part working.

我的情况已被SO的其他答案部分涵盖,但我找不到完整的答案。简而言之,我们正在尝试将URL用于我们的特定数据类型,双击时将打开我们的应用程序并将这些数据集加载到该应用程序中。我们有这部分工作。

(for example, an URL might look like this: resource://shaders/basic_shader.hlsl)

(例如,URL可能如下所示:resource://shaders/basic_shader.hlsl)

What we would like to do is to prevent new instances of the application from opening when a new URL is double clicked. For example, let's say we have a URL that opens up a shader in our shader editor. When clicking this resource URL, it will open our shader editor. When a new shader URL is clicked, we'd like to be able to open up the shader in the currently running application and have it open up the new shader in a new tab in our editor.

我们想要做的是在双击新URL时阻止打开应用程序的新实例。例如,假设我们有一个在着色器编辑器中打开着色器的URL。单击此资源URL时,它将打开我们的着色器编辑器。单击新的着色器URL时,我们希望能够在当前运行的应用程序中打开着色器,并在编辑器的新选项卡中打开新的着色器。

We can easily detect if another instance of our application is running. The problem that we don't know how to easily solve is how to tell the currently running application to open up this new file for editing. This behavior is very much like the Apple Finder.

我们可以轻松检测是否有另一个应用程序实例正在运行。我们不知道如何轻松解决的问题是如何告诉当前运行的应用程序打开这个新文件进行编辑。这种行为非常类似于Apple Finder。

In unix, you could emulate this behavior by having your application open some named pipe and then new apps could check if this pipe is active and then send the document data down the pipe. Is there a more standard windows way of achieving this behavior?

在unix中,您可以通过让应用程序打开一些命名管道来模拟此行为,然后新应用程序可以检查此管道是否处于活动状态,然后将文档数据发送到管道。是否有更标准的Windows方式来实现这种行为?

We need a C/C++ solution. Thanks.

我们需要一个C / C ++解决方案。谢谢。

5 个解决方案

#1


Create a named mutex when application launches as David Grant said, then before displaying the UI for the second URL, check for this mutex, if it is already created then just quit by passing the new URL to the first launched application (Have interface in the application to set the URL and tell to redirect programatically)

在应用程序启动时创建一个命名的互斥锁,如David Grant所说,然后在显示第二个URL的UI之前,检查这个互斥锁,如果它已经创建,那么只需通过将新URL传递给第一个启动的应用程序退出(在应用程序设置URL并告诉以编程方式重定向)

#2


Named pipe is the best way. First instance of your application opens the pipe and listens to it (use PIPE_ACCESS_INBOUND as dwOpenMode and the same code will also allow you to detect running instances). All subsequent instances check that they are not alone, send command line argument to the pipe and shut down.

命名管道是最好的方法。应用程序的第一个实例打开管道并侦听它(使用PIPE_ACCESS_INBOUND作为dwOpenMode,相同的代码也允许您检测正在运行的实例)。所有后续实例都会检查它们并不是唯一的,将命令行参数发送到管道并关闭。

#3


You can't avoid the program associated with the url to be executed.

您无法避免与要执行的URL关联的程序。

The "windows" solutions would be to send a message (via DDE in the old days but maybe there is something more "modern" now) to the previously running application with the url then quit ...

“windows”解决方案是发送一条消息(过去通过DDE,但现在可能还有更“现代”的东西)到先前正在运行的应用程序中,然后退出...

#4


You can acquire a named mutex upon startup and enforce it that way.

您可以在启动时获取命名的互斥锁并以此方式强制执行。

#5


I got this working pretty well for my C++ MFC application by following Joseph Newcomer's tutorial here. He uses a named mutex that is checked on startup and a message sent to the already-running application with the new resource to be opened.

通过遵循Joseph Newcomer的教程,我的C ++ MFC应用程序运行良好。他使用在启动时检查的命名互斥锁,并使用要打开的新资源将消息发送到已在运行的应用程序。

#1


Create a named mutex when application launches as David Grant said, then before displaying the UI for the second URL, check for this mutex, if it is already created then just quit by passing the new URL to the first launched application (Have interface in the application to set the URL and tell to redirect programatically)

在应用程序启动时创建一个命名的互斥锁,如David Grant所说,然后在显示第二个URL的UI之前,检查这个互斥锁,如果它已经创建,那么只需通过将新URL传递给第一个启动的应用程序退出(在应用程序设置URL并告诉以编程方式重定向)

#2


Named pipe is the best way. First instance of your application opens the pipe and listens to it (use PIPE_ACCESS_INBOUND as dwOpenMode and the same code will also allow you to detect running instances). All subsequent instances check that they are not alone, send command line argument to the pipe and shut down.

命名管道是最好的方法。应用程序的第一个实例打开管道并侦听它(使用PIPE_ACCESS_INBOUND作为dwOpenMode,相同的代码也允许您检测正在运行的实例)。所有后续实例都会检查它们并不是唯一的,将命令行参数发送到管道并关闭。

#3


You can't avoid the program associated with the url to be executed.

您无法避免与要执行的URL关联的程序。

The "windows" solutions would be to send a message (via DDE in the old days but maybe there is something more "modern" now) to the previously running application with the url then quit ...

“windows”解决方案是发送一条消息(过去通过DDE,但现在可能还有更“现代”的东西)到先前正在运行的应用程序中,然后退出...

#4


You can acquire a named mutex upon startup and enforce it that way.

您可以在启动时获取命名的互斥锁并以此方式强制执行。

#5


I got this working pretty well for my C++ MFC application by following Joseph Newcomer's tutorial here. He uses a named mutex that is checked on startup and a message sent to the already-running application with the new resource to be opened.

通过遵循Joseph Newcomer的教程,我的C ++ MFC应用程序运行良好。他使用在启动时检查的命名互斥锁,并使用要打开的新资源将消息发送到已在运行的应用程序。