Windows Events API 使用

MSDN連結

用於行程間同步

支援兩種模式:自動重設(接收到訊號後自動重置為Off)、手動重設(設定訊號後會一直為On的狀態)

使用WaitForSingleObject、WaitForMultipleObjects等待

建立使用CreateEvent

HANDLE WINAPI CreateEvent(
_In_opt_ LPSECURITY_ATTRIBUTES lpEventAttributes,
_In_     BOOL                  bManualReset, /* Event模式 */
_In_     BOOL                  bInitialState, 
_In_opt_ LPCTSTR               lpName /* 自訂的Event名稱 */
);

從其他Process取得Handle時,使用OpenEvent

HANDLE WINAPI OpenEvent(
_In_ DWORD   dwDesiredAccess,
_In_ BOOL    bInheritHandle,
_In_ LPCTSTR lpName
);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#define UNICODE
#include <windows.h>
#include <stdio.h>

// client 

void error()
{
    char buf[256] = {};
    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
    puts(buf);
}

int main()
{
    // use Global
    HANDLE hEvent = OpenEvent(SYNCHRONIZE, FALSE, L"Global\\AlertServiceEvent");
    if (hEvent == NULL)
    {
        printf("OpenEvent: ");
        error();
        return -1;
    }

    while (TRUE)
    {
        printf("Wait...\n");
        WaitForSingleObject(hEvent, INFINITE);
        printf("Get!\n");
    }

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#define UNICODE
#include <windows.h>
#include <stdio.h>

/* server */
void error()
{
    char buf[256] = {};
    FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(),
    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), buf, 256, NULL);
    puts(buf);
}

int main()
{
    // use Global
    HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, L"Global\\AlertServiceEvent");
    if (hEvent == NULL)
    {
        printf("OpenEvent:");
        error();
        return -1;
    }

    while(1)
    {
        printf("Sending\n");
        if(0 == PulseEvent(hEvent))
        {
            printf("PulseEvent:");
            error();
        } 
        sleep(1);
    }

    return 0;
}