CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Aug 2010
    Posts
    12

    Communication between two programs via registry?

    Hi,

    I'm trying to set up some basic communication between two programs via the registry.

    For this I would like to use the functions

    Code:
    int value=10;
    CString strSection="Data";
    CWinApp* pApp = AfxGetApp();
    pApp->WriteProfileInt(strSection,ValueName,value);
    and

    Code:
    CString strSection="Data";
    CWinApp* pApp = AfxGetApp();
    int value=pApp->GetProfileInt(strSection,ValueName,0);
    which read/write to the application's registry.

    Is there some way of making two different programs use the same application registry?

    I realize there's probably more conventional methods for communication, but I'm just looking for something really basic here.

    Thank you in advance.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Communication between two programs via registry?

    Look at CWinApp::SetRegistryKey, which is called in InitInstance.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Communication between two programs via registry?

    Quote Originally Posted by ebraak View Post
    Is there some way of making two different programs use the same application registry?
    The way is to use the same registry key.
    Best regards,
    Igor

  4. #4
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Communication between two programs via registry?

    How does one program know that the other has changed some data?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Communication between two programs via registry?

    Functions like CWinApp::WriteProfileString and CWinApp::GetProfileString access values under the following key:
    HKEY_CURRENT_USER\"Software"\RegistryKey\ProfileName\SectionName
    where RegistryKey is the value set by a call of CWinApp::SetRegistryKey and ProfileName is usually the application name.

    So CWinApp::WriteProfileString, CWinApp::GetProfileString and related, do not provide a good way to read what another application wrote into registry.
    Use CRegKey ATL class instead, as Igor alrerady suggested.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Communication between two programs via registry?

    Quote Originally Posted by 2kaud View Post
    How does one program know that the other has changed some data?
    It is possible with RegNotifyChangeKeyValue.
    But of course, that's not so easy as the OP is expecting. Or in other words, is like scraping own head with the foot.
    So this case we can say, using other ways like for example sending WM_COPYDATA message become "something really basic"
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    2kaud's Avatar
    2kaud is online now Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Communication between two programs via registry?

    Quote Originally Posted by ovidiucucu View Post
    It is possible with RegNotifyChangeKeyValue.
    But of course, that's not so easy as the OP is expecting. Or in other words, is like scraping own head with the foot.
    So this case we can say, using other ways like for example sending WM_COPYDATA message become "something really basic"
    I don't know the purpose of the OP usage of 'basic' process communication, but potentially there's the issue of both programs trying to change the data at the same time - or one changing it before action has been taken on a change notification from the other.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Communication between two programs via registry?

    Quote Originally Posted by 2kaud View Post
    I don't know the purpose of the OP usage of 'basic' process communication, but potentially there's the issue of both programs trying to change the data at the same time - or one changing it before action has been taken on a change notification from the other.
    Yes. Consider it a shared resource. As such, when two different threads (or two different processes in this case) are accessing the resource, access to the resource needs to be protected. Since, two different processes are accessing it, one approach to synchronizing access is to use a named mutex.

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Communication between two programs via registry?

    Another approach is to use [in memory] Memory Mapped Files. See the Memory Mapped Files article listed in my signature line. The article contains a wrapper class that makes creating, using and receiving change notifications from memory mapped files easy.

  10. #10
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Communication between two programs via registry?

    there are tons of way to make one program "talk" to another. While the registry works, it'd not really the most ideal method.

    for a one-to-one communication, a "named pipe" is probably the most Obvious/reliable/easy.
    for a one-to-many, there are "mailslots".
    the advantage of the above two is that they're designed for that specific purpose, while the other methods are often "incorrect use" or even "abuse" of a side effect of what the function is intended for.

    You can also use sockets, memory mapped files, shared memory, windows messages (WM_COPYDATA, but you could make your own), a regular file, registry, direct reading/writing of process memory, ...


    the problem with memory mapped files, 'regular files', registry is that there's some persistant "mess" you eventually need to clean up.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Communication between two programs via registry?

    Quote Originally Posted by OReubens View Post
    the problem with memory mapped files, 'regular files', registry is that there's some persistant "mess" you eventually need to clean up.
    In terms of memory mapped files, there is no cleanup mess if you use a pagefile backed MMF. Once both processes (or multiple processes) are closed the system frees the MMF.

  12. #12
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: Communication between two programs via registry?

    ovidiucucu sir , I have also a doubt .. When two applications are sharing a Registry , then Application name and registry key need to be same .. Suppose one program is for data acquisition and other program is for data analysis , then they have different application names ..
    then they have different AFX_IDS_APP_TITLE .. so how can they communicate through Registry or how can they share the registry ?

    pl guide ..

  13. #13
    Join Date
    Jan 2012
    Location
    India
    Posts
    193

    Re: Communication between two programs via registry?

    sir one more doubt .. if I set same Registry Key for two applications , and if they open two different types of files by file open menu , then will it affect "Recent File list ?"

  14. #14
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: Communication between two programs via registry?

    Quote Originally Posted by Arjay View Post
    In terms of memory mapped files, there is no cleanup mess if you use a pagefile backed MMF. Once both processes (or multiple processes) are closed the system frees the MMF.
    And how will you share data between two separate processes using a pagefile backed MMF ? (which the OP is asking about)

    Afaik, if you want shared data between processes using an MMF, this can only be done with a MMF on an actual file.

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Communication between two programs via registry?

    Quote Originally Posted by OReubens View Post
    And how will you share data between two separate processes using a pagefile backed MMF ? (which the OP is asking about)

    Afaik, if you want shared data between processes using an MMF, this can only be done with a MMF on an actual file.
    Not at all, an MMF can use a system file or a pagefile as its backing 'store'.

    From CreateFileMapping in msdn:

    If hFile is INVALID_HANDLE_VALUE, the calling process must also specify a size for the file mapping object in the dwMaximumSizeHigh and dwMaximumSizeLow parameters. In this scenario, CreateFileMapping creates a file mapping object of a specified size that is backed by the system paging file instead of by a file in the file system.
    Take a look at the memory mapped file example in my signature line. It contains a sample project that uses an MMF between a log writer and log reader - each in different processes.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured