c - 使用 C 的 Windows 进程间通信 (IPC)

标签 c windows ipc keep-alive watchdog

我有一个用 Microsoft Visual C++ 用 C 语言编写的旧程序,我需要实现某种“保持事件”,因此我能够将进程间通信接收到一个新程序中,该程序将杀死并重新启动该程序如果过去 5 秒内没有收到消息,则返回第一个。

问题是我一直在寻找 C 语言的 Windows IPC 教程或示例,但我找到的几乎所有内容都是 C++ 的。

有什么帮助或资源吗?

编辑:正如@Adriano 在答案中建议的那样,我正在尝试使用共享内存。但由于某种我无法捕获的异常,启动程序被 Windows 终止。调用 CopyMemory 时发生。

代码如下:

#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;

int launchMyProcess();
void killMyProcess();
bool checkIfMyProcessIsAlive();

STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;
HANDLE mappedFile;
LPVOID pSharedMemory;
long lastReceivedBeatTimeStamp;
const int MSECONDS_WITHOUT_BEAT = 500;
const LPTSTR lpCommandLine = "MyProcess.exe configuration.txt";


    int main(int argc, char* argv[])
    {
        mappedFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(int), "Global\\ActivityMonitor");
        LPVOID pSharedMemory = MapViewOfFile(mappedFile, FILE_MAP_READ, 0, 0, sizeof(int));
        if(!launchMyProcess()){
            cout<<"Error creating MyProcess.exe"<<endl;
            UnmapViewOfFile(pSharedMemory);
            CloseHandle(mappedFile);
            return -1;
        }
        while(true){
            Sleep(100);

            if(!checkIfMyProcessIsAlive()){
                cout<<"Relaunching MyProcess...";
                killMyProcess();
                if(!launchMyProcess()){
                    cout<<"Error relaunching MyProcess.exe"<<endl;
                    UnmapViewOfFile(pSharedMemory);
                    CloseHandle(mappedFile);
                    return -1;
                }
            }
        }

        UnmapViewOfFile(pSharedMemory);
        CloseHandle(mappedFile);
        return 0;
    }


    bool checkIfMyProcessIsAlive()
    {
        static int volatile latestMagicNumber = 0;
        int currentMagicNumber = 0;

        CopyMemory(&currentMagicNumber, pSharedMemory, sizeof(int));

        if(currentMagicNumber != latestMagicNumber){
            latestMagicNumber = currentMagicNumber;
            return true;
        }
        return false;
    }

    int launchMyProcess()
    {
        ZeroMemory(&sInfo, sizeof(sInfo));
        sInfo.cb = sizeof(sInfo);
        ZeroMemory(&pInfo, sizeof(pInfo));

        return CreateProcess(NULL, lpCommandLine, NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo);
    }

    void killMyProcess()
    {
        TerminateProcess(pInfo.hProcess, 0);
        CloseHandle(pInfo.hProcess);
        CloseHandle(pInfo.hThread);
        Sleep(3000);
    }

最佳答案

如果您的旧 C 应用程序有消息泵(因为它有 UI),那么检查它是否处于事件状态的最简单方法可能是 IsHungAppWindow()函数,Windows 将为您做这些事情。

如果这不是你的情况并且你需要IPC,那么有很多选择,这取决于你想使用哪种IPC机制。这里我只列出一些资源。

有关 IPC 技术的概述:http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx

一些例子:

编辑
我认为一个小例子比大量的文字更能说明问题。在此示例中,我将使用共享内存,但您可以使用您喜欢的任何内容(并且您感觉更舒服)。未经测试,仅供引用。

应该首先启动 MONITOR 进程。

VOID CALLBACK CheckItIsAlive(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
    static int volatile latestMagicNumber = 0;

    int currentMagicNumber = 0;
    CopyMemory(&currentMagicNumber, lpParam, sizeof(int));

    if (currentMagicNumber != latestMagicNumber)
        latestMagicNumber = currentMagicNumber;
    else
    {
        // Do something, it's hanged
    }
}

void main()
{
    // Shared memory used to communicate with the other process
    HANDLE mappedFile = CreateFileMapping(INVALID_HANDLE_VALUE,
        NULL, PAGE_READWRITE, 0, sizeof(int), "Global\\MyActivityMonitor");

    LPVOID pSharedMemory = MapViewOfFile(mappedFile,
        FILE_MAP_READ, 0, 0, sizeof(int));

    // Thread used to check activity
    HANDLE queue = CreateTimerQueue();
    HANDLE hTimer = NULL;
    CreateTimerQueueTimer(&hTimer, queue, 
        (WAITORTIMERCALLBACK)CheckItIsAlive, pSharedMemory,
        0, 5000, WT_EXECUTEDEFAULT);

    // Do your work here...

    // Clean up
    DeleteTimerQueue(queue);

    UnmapViewOfFile(pSharedMemory);
    CloseHandle(mappedFile);
}

MONITORED 进程,它将向 Monitor 进程发出其事件信号。

VOID CALLBACK NotifyImAlive(PVOID lpParam, BOOLEAN TimerOrWaitFired)
{
    static int volatile counter = 1;

    int tick = counter++;
    CopyMemory(lpParam, &tick, sizeof(int));
}

void main()
{
    // Shared memory used to communicate with the other process
    HANDLE mappedFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE,
        "Global\\MyActivityMonitor");

    LPVOID pSharedMemory = MapViewOfFile(mappedFile,
        FILE_MAP_WRITE, 0, 0, sizeof(int));

    // Thread used to signal activity
    HANDLE queue = CreateTimerQueue();
    HANDLE hTimer = NULL;
    CreateTimerQueueTimer(&hTimer, queue, 
        (WAITORTIMERCALLBACK)NotifyImAlive, pSharedMemory,
        0, 5000, WT_EXECUTEINTIMERTHREAD);

    // Do your work here...

    // Clean up
    DeleteTimerQueue(queue);

    UnmapViewOfFile(pSharedMemory);
    CloseHandle(mappedFile);
}

共享内存是一种非常轻量级的资源,您可以使用您喜欢的任何计时器(如果计时不是严格要求,您可以进行某种空闲处理。我个人喜欢这个,因为您不需要锁定任何线程,并且可能您有一个空闲时间处理线程)。

从 Windows 2000 开始支持定时器函数,请确保 _WIN32_WINNT 宏定义为 0x0500(或更多)。

附录
我没有在列表中提及,因为它们仅存在于较新版本的操作系统中,但您甚至可以使用 condition variables 。 Windows 8 将支持一个非常有用的 WaitOnAddress功能,但它仍然是 future ,所以我认为你不能使用它。

关于c - 使用 C 的 Windows 进程间通信 (IPC),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10535313/

相关文章:

python - 使用 Python 在 MS Office 文档中嵌入对象?

windows - NPM 安装 (Windows) 抛出 .SLN 错误...?

windows - 你如何让一个文件在 Windows 中从 vim 可写?

kubernetes - Kubernetes 中的 Pod 可以查看/访问在同一 Pod 中运行的其他容器的进程吗?

c - vfork() 与 pipeline() 的用法

c - 为什么在 Petersons 算法中需要内存栅栏

c - Pb 使用 nedflush 解决内存泄漏问题

perl - 如何防止 parent 在给 child 写信时阻塞?

c++ - OS X 上的 OpenMP 汇编程序错误

c - 如何从文件中获取 int - string - float?