Qt/C++ access mapped memory file from a different program

482 views Asked by At

I'm looking to implement a way for a Qt application (can use any c++ methods or libraries) to access/find a mapped memory file created by a Matlab process. I've read several examples of how to do shared memory within a certain environment (i.e. 2 processes created in Matlab or Qt or VS) but none talk about how to go and find a mapped file from another program. Is there a way to do that?

Here is my Matlab script that maps a file:

filename = fullfile(tempdir, 'test.dat');
[f,~]=fopen(filename,'wb');
fwrite(f,67,'uint8');
fclose(f);

m = memmapfile(filename,'Writable',true,'Format','uint8');
keyboard

I'm not really interested in writing to the mapped file from Qt, right now I simply just want to know if it exists and then maybe write to it in the future. The only thing I could think to try in Qt was to see if the file exists, but my attempt is fundamentally flawed because from what I understand I want to know if the file is mapped (looking for a memory address?) not if it exists on the hard drive.

#include <QCoreApplication>
#include <QSharedMemory>
#include <QFile>
#include <QTextStream>
#include <QString>

void read(QString filename)
{
    QFile file(filename);
    if (!file.exists()) {
        QTextStream(stdout) << "File does not exist!" << endl;
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString filename = "C:/Users/Engineer/AppData/Local/Temp/test.dat";
    read(filename);

    return a.exec();
}

Has anyone tried to do this? Can anyone point me in the right direction?

0

There are 0 answers