Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7930117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:18:59+00:00 2026-06-03T20:18:59+00:00

I have an old program written in C with Microsoft Visual C++, and I

  • 0

I have an old program written in C with Microsoft Visual C++, and I need to implement some kind of “keepalive”, so I am able to receive it thought interprocess communication into a new program which will kill and re-launch the first one if no msg has been received in the last 5 seconds.

The problem is that I have been looking for any tutorial or example of IPC for Windows in C language, but almost everything I find is for C++.

Any help or resource?

EDIT: As @Adriano suggested in answers, I’m trying to use Shared Memory. But the launcher program is being terminated by Windows due to some kind of exception I’m not being able to catch. Happens when calling CopyMemory.

The code is the following:

#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);
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:19:01+00:00Added an answer on June 3, 2026 at 8:19 pm

    If your old C application has a message pump (because it has an UI) maybe the simpliest way to check if it’s alive or not is IsHungAppWindow() function and Windows will do the stuff for you.

    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.

    For an overview of IPC techniques: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365574(v=vs.85).aspx

    Some examples:

    • MSMQ: http://msdn.microsoft.com/en-us/library/windows/desktop/ms705205(v=vs.85).aspx
    • Named pipes: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365590(v=vs.85).aspx and http://msdn.microsoft.com/en-us/library/windows/desktop/aa365592(v=vs.85).aspx
    • Shared memory: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx

    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.

    The MONITOR process, should be started first.

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

    The MONITORED process, it’ll signal its activity to the Monitor process.

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

    Shared memory is a pretty lightweight resource and you can use whatever you prefer for your timers (if timing isn’t a strict requirement you can do some kind of idle processing. Personally I like this ’cause you won’t need to lock any thread and probably you have an idle time processing thread).

    Timer functions are supported starting from Windows 2000, be sure that _WIN32_WINNT macro is defined with 0x0500 (or more).

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an old program written in borland pascal and in Delphi if I
I have an old update program written in vb6, which runs as admin. However,
I have an old program written in Dataflex/PowerFlex from back in the mid 90's
I have an old setup of the old program written on c++ which contains
I have rewritten an old program and designed a new database for it. I
I have an old C# program that is being ported to Python 3 for
I have an old, third party, command line, proprietary program which I'm calling from
We have an old web app written in classic ASP. We don't have the
I have an old program that uses XUL and Mozilla Prism to display a
I have a simple program written in C and Objective-C. It consists of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.