windows 事件可以是进程间的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/625504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:08:00  来源:igfitidea点击:

Can Events be Inter-Process?

c++windowseventsipc

提问by Mike Trader

I have created an event in one process and to test, sent the event handle via a pipe to a totally separate process (not a child thread)

我在一个进程中创建了一个事件并进行测试,通过管道将事件句柄发送到一个完全独立的进程(不是子线程)

When I fire the event in the first, WaitForSingleObject does not detect the event so I am guessing the answer is no unless I missed some trick in the SECURITY_ATTRIBUTES structure?

当我在第一个触发事件时,WaitForSingleObject 没有检测到该事件,所以我猜答案是否定的,除非我错过了SECURITY_ATTRIBUTES 结构中的一些技巧

Or perhaps I need to use a named event and call OpenEvent()?

或者我可能需要使用命名事件并调用 OpenEvent()?

In this case I cannot use window messages because I am trying to signal a windows service. I could use the pipe, but there will be many of these applications, and I would like to find a "low cost" solution if possible.

在这种情况下,我无法使用窗口消息,因为我正在尝试向 Windows 服务发出信号。我可以使用管道,但是会有很多这样的应用程序,如果可能的话,我想找到一个“低成本”的解决方案。

Other options like Memory mapped files have even more overhead than the pipe?

其他选项(例如内存映射文件)的开销甚至比管道还要多?

How would you do this?

你会怎么做?

采纳答案by i_am_jorf

You need to create a named event and open it in both processes. If you have multiple processes listening, you may consider using a semaphore.

您需要创建一个命名事件并在两个进程中打开它。如果您有多个进程在监听,您可以考虑使用semaphore

回答by Mike Trader

Yes this works:

是的,这有效:

  #COMPILE EXE "NamedEvent.exe"

  #INCLUDE "win32api.inc" 

  %EVENT_ALL_ACCESS = &h0001F0003

  FUNCTION PBMAIN() AS LONG  

    LOCAL lRet AS LONG, lError AS LONG, lEventName AS ASCIIZ * %MAX_PATH
    lEventName = "TestEvent"
    lRet   = CreateEvent (BYVAL %NULL, %False, %False, lEventName)
    lError = GetLastError ()
    IF ISFALSE lRet THEN
      MSGBOX "Unable to create Event, error:" + STR$(lError),,"CreateEvent error"
    ELSE
      IF lError = %ERROR_ALREADY_EXISTS THEN
        lRet = OpenEvent(BYVAL %EVENT_ALL_ACCESS, %False, lEventName)
        lError = GetLastError()
        IF lRet THEN
          MSGBOX "Opened existing Event, handle:" + STR$(lRet),,"OpenEvent:"
        ELSE
          MSGBOX "Unable to open Event, error:" + STR$(lError),,"OpenEvent error" : EXIT FUNCTION
        END IF
      ELSE
        MSGBOX "Created new Event, handle:" + STR$(lRet),,"CreateEvent:"
      END IF
    END IF    

  END FUNCTION

In general, what has a lower overhead:

一般来说,开销较低的方法是:

Pipes (assuming small size specified)

管道(假设指定的小尺寸)

MemMapFiles

内存映射文件

Events

活动