Brandon
Brandon

Reputation: 23498

Shared Memory Objects Across Modules

How can I share Objects across DLL's?

Example:

DLLA is loaded by Process A. DLL A collects data (Intercepts calls from process A).

DLLB is loaded by a Pascal program and wants to use the data that DLLA collected.

I've created a mapping with DLLA like so:

GL_EXTERN bool __stdcall CreateSharedMemory()
{
    if ((hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, MapSize, SharedMemoryName.c_str())) == NULL)
    {
        return false;
    }

    if ((pData = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, MapSize)) == NULL)
    {
        CloseHandle(hMapFile);
        return false;
    }
    return true;
}

And DLLB just opens it and maps it.

They can share commands. So DLL B can give DLLA commands and retrieve data but I cannot seem to figure out how to share objects/classes/vectors across the memory.

I've tried using a pointer but realized that this is invalid and gives access violation errors. Also it seems to share integers fine but it doesn't seem to be able to share floats :S How can I accomplish these tasks?

EDIT: To communicate I do:

int* Foo = static_cast<int*>(pData); where pData is the map pointer.
//Then I do:

Foo[0] = X; Foo[1] = Y;

And have the processes read and write from the corresponding values.

Info:

This is the files for DLLA: http://pastebin.com/MFkqwxJe and http://pastebin.com/MDeWprkb

This is the files for DLLB: http://pastebin.com/Uj3dijc4 and http://pastebin.com/vkDdPp7e

EDIT: Narrowed it down to: http://pastebin.com/hNF6Cu2L as requested.

Upvotes: 0

Views: 210

Answers (1)

Kirill Kobelev
Kirill Kobelev

Reputation: 10557

In the shared memory region you can use all fundamental data types like integers and floating point numbers. If you have problems with float, this is unrelated to the shared nature of the memory, like different understanding between C/Pascal of what float/double/long_double is. You cannot use pointers. Structures that do not nave VMTs and do not have pointers are fine also. Complex data structures can be emulated using offsets (direct or indirect) from the beginning of the shared region.

To make synchronization (like events) you can use named objects.

You can also pass handles between processes. Check the DuplicateHandle function.

Continuation:

    Data[3] = &ListOfModels[0];

Here you put into the shared memory an address that does not belong to the shared memory region. The data in the shared memory region should be completely self contained meaning that all pieces of data should be in that region. Definition of the structure should look like:

struct SaredData
{
    int data_type;
    int status;
    union
    {
        struct
        {
            int       num_font_char_objects;
            FontChar  font_char_objects[MAX_FONT_CHARS_SUPPORTED];
        };
        ...........
    };
};

And you need to ensure that the size of the shared memory region is big enough to store all passed objects. It is highly unlikely that any container from STL with work with shared memory.

You cannot place your Model structure into the union above because it contains complex fields.

Upvotes: 1

Related Questions