English 中文(简体)
视窗与C的界面
原标题:Inter-Process Communication (IPC) for Windows with C

我在C部与微软视像C++共同撰写了一个老方案,我需要实施某种“管理”,因此,我得以将这种通信纳入一个新方案,如果在最后5秒中没有收到任何ms,将首先杀人并重新发射。

问题在于,我一直在研究用C语文使用Windows的电传或视像,但我发现的几乎是C++。

任何帮助或资源?

EDIT:As @Adriano在答复中建议,Im试图使用共同记忆。 但是,由于某种例外情况,Im无法捕获,发射器方案正在由Windows终止。 Happens when callsbookmory.

该法典如下:

#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申请有电传泵(因为有电传机),那么如果它活着,则可能最简单地加以检查:IsHungAppWindow(功能和Windows将给你造成障碍。

If this is not your case and you need IPC there are many options, it depends on what kind of IPC mechanism you want to use. Here I ll just list some resources.

关于IPC技术的概述:

一些实例:

EDIT
I think a small example will clarify much more than tons of words. In this example I ll use shared memory but you can use whatever you prefer (and you feel more comfortable with). It s untested so please use it just as reference.

应首先启动监测局的工作。

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);
}

卫生部的工作向监测进程发出信号。

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开始得到支持,确保从0x0500(或更多)中确定。

Addendum
I didn t mentioned in the list because they exist only in newer versions of OS but you may even use condition variables. Windows 8 will support a very useful WaitOnAddress function but it s still the future so I think you can t use it.

问题回答

从《任择议定书》和各项评论来看,它好像主要目标是确定申请是否属实。 另一种应用可以监测的某种“智能肉类”的两种相当简单的方法要么是分享记忆,要么是名人。

你的意见可使用CreateFileMapping

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682428v=vs.85%29.aspx” rel=“nofollow”>CreateSemaphore

这似乎是另一例,即由于对你重新处理的平台缺乏了解而走得很远。

http://www.em>all/em。 如你在评论中所说,你需要知道你的方案是否活着,以便你能够杀死它,你甚至不需要“m>remotely。

在该方案开始时,希望监测:

HANDLE hMutex = CreateMutex(NULL, FALSE, _T("MyMagicKey"));
WaitForSingleObject(hMutex, INFINITE);

在“监视”方案中,你检查其他用途是否像现在这样:

HANDLE hMutex = OpenMutex(SYNCHRONIZE, FALSE, _T("MyMagicKey"));
if (hMutex == NULL && GetLastError() == ERROR_FILE_NOT_FOUND)
   //The app being watched is already dead
else
   //whatever you want

还有半衰期的其他解决办法同样适用(或更好)。 如果你的观察家是产生即将监测的信号的唯一的,你可以等到<条码>上(<条码>>)。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签