Communication between two programs via registry?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 19

Thread: Communication between two programs via registry?

Hybrid View

  1. #1
    Join Date
    Aug 2010
    Posts
    4

    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
    Posts
    12,107

    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
    5,990

    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
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,026

    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 Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  6. #6
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,026

    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 Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,453

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

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

    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 / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,309

    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
    3,920

    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
    Join Date
    Jan 2012
    Location
    India
    Posts
    138

    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 ..

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

    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 ?"

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center