How to share data between Service Program and Desktop Program?

재환 주 1 Reputation point
2021-01-29T01:18:08.077+00:00

I want to share data between service program and desktop program.

I made very simple DLL file like below

#pragma data_seg(".MyShare")
char  g_player_running = 0;                          // 0 - No Running, 1 - Running, 2 - User Terminated
#pragma data_seg()
#pragma comment( linker, "/SECTION:.MyShare,RWS")

extern "C" _declspec(dllexport) char GetPlayerRunningStatus()
{
    return g_player_running;
}

extern "C" _declspec(dllexport) void SetPlayerRunningStatus(char AStatus)
{
    g_player_running = AStatus;
}

extern "C" _declspec(dllexport) char* GetPlayerRunningStatusPointer()
{
    return &g_player_running;
}

Both service program and desktop program started with loading the dll file.
But, the data was not shared.
If desktop program runs twice, data sharing works well...

I used the function (GetPlayerRunningStatusPointer) to get address of the data variable.
Both service program and desktop program get the same address.
But Data sharing is not working......

Is it impossible? or Should I use another method?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,680 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,784 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Guido Franzke 2,186 Reputation points
    2021-01-29T07:11:29.123+00:00

    Hello,
    here is another method with memory-mapped files:
    creating-named-shared-memory
    Regards, Guido

    0 comments No comments

  2. Drake Wu - MSFT 991 Reputation points
    2021-01-29T07:25:09.153+00:00

    Hi, @재환 주 Services are running in the session 0, and Shared sections don't work across session boundaries any more.

    You could choose the most suitable IPC method in this document: Interprocess Communications. I suggest to use File Mapping to achieve shared memory.
    Here is the related issue on stackoverflow: https://stackoverflow.com/a/898863/10611792


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.