Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Send data between processes via QSharedMemory
Forum Update on Monday, May 27th 2025

Send data between processes via QSharedMemory

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 4 Posters 7.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MR.Smith
    wrote on 20 Feb 2017, 12:53 last edited by
    #1

    Hello,

    I'm need to communicate with another process in OS.
    First process I would like to write on Qt (5.6.1). Second process on Visual Studio 2013 on WinAPI. I think that QSharedMemory is a good way for this.

    I used Shared Memory Example. It's correctly work in single process.

    Already I used Creating Named Shared Memory example from MSDN. It's also correctly work.

    But I can't transfer data from Qt application to WinAPI application. Qt application is correctly write data and I can read it in Qt application, but in WinAPI application is not possible to connect to shared memory and read it. Error: Could not open file mapping object.

    May be there is more detailed example for QSharedMemory?
    What is a problem here?

    My source code is bellow.
    Creating shared memory:

    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog),
        sharedMemory("Global\\SharedMemoryExample")
    {
        ui->setupUi(this);
    }
    

    For write data to shared memory:

    void Dialog::on_writeButton_clicked()
    {
        QString data = ui->bufferLineEdit->text();
        if(sharedMemory.isAttached())
            sharedMemory.detach();
    
        QBuffer buffer;
        buffer.open(QBuffer::ReadWrite);
        QDataStream out(&buffer);
        out << data;
        int size = buffer.size();
    
        if(!sharedMemory.create(size))
        {
            ui->bufferLineEdit->setText("ERROR: Shared Memory not created");
            return;
        }
        sharedMemory.lock();
        char *to = (char*)sharedMemory.data();
        const char *from = buffer.data().data();
        memcpy(to, from, qMin(sharedMemory.size(), size));
        sharedMemory.unlock();
    }
    

    For read shared memory:

    void Dialog::on_readButton_clicked()
    {
        ui->bufferLineEdit->clear();
        if (!sharedMemory.isAttached())
        {
            ui->bufferLineEdit->setText("ERROR: Shared Memory not attached");
            return;
        }
        QBuffer buffer;
        QDataStream in(&buffer);
        QString data;
        data.clear();
    
        sharedMemory.lock();
        buffer.setData((char*)sharedMemory.constData(), sharedMemory.size());
        buffer.open(QBuffer::ReadOnly);
        in >> data;
        sharedMemory.unlock();
    
        sharedMemory.detach();
        ui->bufferLineEdit->setText(data);
    }
    

    And second WinApi application:

    #include <windows.h>
    #include <stdio.h>
    #include <conio.h>
    #include <tchar.h>
    #pragma comment(lib, "user32.lib")
    
    #define BUF_SIZE 256
    TCHAR szName[]=TEXT("Global//SharedMemoryExample");
    
    int _tmain()
    {
       HANDLE hMapFile;
       LPCTSTR pBuf;
    
       hMapFile = OpenFileMapping(
                       FILE_MAP_ALL_ACCESS,   // read/write access
                       FALSE,                 // do not inherit the name
                       szName);               // name of mapping object
    
       if (hMapFile == NULL)
       {
          _tprintf(TEXT("Could not open file mapping object (%d).\n"),
                 GetLastError());
          return 1;
       }
    
       pBuf = (LPTSTR) MapViewOfFile(hMapFile, // handle to map object
                   FILE_MAP_ALL_ACCESS,  // read/write permission
                   0,
                   0,
                   BUF_SIZE);
    
       if (pBuf == NULL)
       {
          _tprintf(TEXT("Could not map view of file (%d).\n"),
                 GetLastError());
    
          CloseHandle(hMapFile);
    
          return 1;
       }
    
       MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);
    
       UnmapViewOfFile(pBuf);
    
       CloseHandle(hMapFile);
    
       return 0;
    }
    
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 20 Feb 2017, 12:58 last edited by
      #2

      Hi and welcome to devnet,

      Did you see that you are not using the same key for both applications ?

      For the Qt application you are using backslashes and for the WinAPI application your are using forward slashes.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 20 Feb 2017, 13:03
      1
      • S SGaist
        20 Feb 2017, 12:58

        Hi and welcome to devnet,

        Did you see that you are not using the same key for both applications ?

        For the Qt application you are using backslashes and for the WinAPI application your are using forward slashes.

        M Offline
        M Offline
        MR.Smith
        wrote on 20 Feb 2017, 13:03 last edited by
        #3

        @SGaist Thank you! I'm fix it. But it's doesn't fix a problem. I'm already testing both variants. In my post it is only typo.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 20 Feb 2017, 13:06 last edited by
          #4

          I don't remember if there's a limitation in the characters you can use as key, but i'd start with only ASCII stuff without any special signs.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply 20 Feb 2017, 13:16
          1
          • M Offline
            M Offline
            MR.Smith
            wrote on 20 Feb 2017, 13:08 last edited by
            #5

            I'm already try to run two copies of Qt application. And it can't transfet data from one thread to another.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 20 Feb 2017, 13:15 last edited by
              #6

              What exact error are you getting ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply 21 Feb 2017, 10:39
              0
              • S SGaist
                20 Feb 2017, 13:06

                I don't remember if there's a limitation in the characters you can use as key, but i'd start with only ASCII stuff without any special signs.

                M Offline
                M Offline
                MR.Smith
                wrote on 20 Feb 2017, 13:16 last edited by
                #7

                @SGaist I'm changed value of key to simple ASCII: "example".
                Not effect (((
                May be you have an example? I searching it in Google but only one example in qt.io was found.
                No any other(((

                1 Reply Last reply
                0
                • V Offline
                  V Offline
                  VRonin
                  wrote on 20 Feb 2017, 14:35 last edited by
                  #8

                  Probably off topic, but did you consider using QLocalSocket and WINAPI Named Pipe instead

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  M 1 Reply Last reply 20 Feb 2017, 15:11
                  2
                  • V VRonin
                    20 Feb 2017, 14:35

                    Probably off topic, but did you consider using QLocalSocket and WINAPI Named Pipe instead

                    M Offline
                    M Offline
                    MR.Smith
                    wrote on 20 Feb 2017, 15:11 last edited by
                    #9

                    @VRonin Good idea. It's will be my fallback. I need to high speed data transfer (up to 10 MBit/s) and shared memory is a faster method of IPC. It's most suitable for my application.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      VRonin
                      wrote on 20 Feb 2017, 15:24 last edited by VRonin
                      #10

                      Disclaimer

                      what follows is just speculation, not advice.


                      The problem with shared memory, as I understand it, is that is no way to signal the reader application that new data is available to consume.

                      10 MBit/s is not a lot, named pipe should be able to manage it without a problem, in fact, a TCP socket connected to localhost should also be able to handle that transfer rate without a sweat. Both their typical transfer rates are in the multiple Gbit/sec

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      M 1 Reply Last reply 21 Feb 2017, 10:51
                      2
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 20 Feb 2017, 16:51 last edited by
                        #11

                        Check the detailed documentation of QSharedMemory and the setNativeKey. They explain how to achieve communication with non Qt application.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3
                        • S SGaist
                          20 Feb 2017, 13:15

                          What exact error are you getting ?

                          M Offline
                          M Offline
                          MR.Smith
                          wrote on 21 Feb 2017, 10:39 last edited by
                          #12

                          @SGaist When I run two copies of Ot application and write some data to shared memory from one of them and the next trying read data from another I have problem: sharedMemory.isAttached() == FALSE for shared memory with same keys.

                          J 1 Reply Last reply 21 Feb 2017, 10:46
                          0
                          • M MR.Smith
                            21 Feb 2017, 10:39

                            @SGaist When I run two copies of Ot application and write some data to shared memory from one of them and the next trying read data from another I have problem: sharedMemory.isAttached() == FALSE for shared memory with same keys.

                            J Offline
                            J Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 21 Feb 2017, 10:46 last edited by
                            #13

                            @MR.Smith Didn't you say before that it is working when using two Qt apps and doesn't work if one app is not a Qt app?

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            M 1 Reply Last reply 22 Feb 2017, 07:30
                            0
                            • V VRonin
                              20 Feb 2017, 15:24

                              Disclaimer

                              what follows is just speculation, not advice.


                              The problem with shared memory, as I understand it, is that is no way to signal the reader application that new data is available to consume.

                              10 MBit/s is not a lot, named pipe should be able to manage it without a problem, in fact, a TCP socket connected to localhost should also be able to handle that transfer rate without a sweat. Both their typical transfer rates are in the multiple Gbit/sec

                              M Offline
                              M Offline
                              MR.Smith
                              wrote on 21 Feb 2017, 10:51 last edited by
                              #14

                              @VRonin Thank you for recommendation. I will try to use named pipe in my application. It's also good solution for my application.

                              1 Reply Last reply
                              0
                              • J jsulm
                                21 Feb 2017, 10:46

                                @MR.Smith Didn't you say before that it is working when using two Qt apps and doesn't work if one app is not a Qt app?

                                M Offline
                                M Offline
                                MR.Smith
                                wrote on 22 Feb 2017, 07:30 last edited by
                                #15

                                @jsulm Yes, it's true. I can transfer data from one Qt application to another copy of this Qt application, but I can't transfer data from Qt application to WinAPI application.

                                J 1 Reply Last reply 22 Feb 2017, 07:32
                                0
                                • M MR.Smith
                                  22 Feb 2017, 07:30

                                  @jsulm Yes, it's true. I can transfer data from one Qt application to another copy of this Qt application, but I can't transfer data from Qt application to WinAPI application.

                                  J Offline
                                  J Offline
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on 22 Feb 2017, 07:32 last edited by
                                  #16

                                  @MR.Smith @SGaist pointed out what you should try

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  1 Reply Last reply
                                  1
                                  • M Offline
                                    M Offline
                                    MR.Smith
                                    wrote on 22 Feb 2017, 07:38 last edited by
                                    #17

                                    I got successfully transfer data via named pipes. It's good solution and I will use it, not shared memory.
                                    Thank you every one for your help and recommendations.

                                    1 Reply Last reply
                                    0

                                    3/17

                                    20 Feb 2017, 13:03

                                    14 unread
                                    • Login

                                    • Login or register to search.
                                    3 out of 17
                                    • First post
                                      3/17
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • Users
                                    • Groups
                                    • Search
                                    • Get Qt Extensions
                                    • Unsolved