3

How can I read a shared memory section like "Global\something_something" in java that was created and is updated by a c#/c++ program? I found some tutorials, but they either work with "real files" or have some other additional stuff. I know that I have to make native calls via jna, for example, to the windows api and use something like the openFileMapping function.

Are there tutorials I have missed or could someone give me a bit of example code? Is using jna or jni the only way to that sort of thing in java?

2
  • Maybe you are completely right and should head for a JNI based solution implemented in some low-level language (C?) Commented Sep 7, 2015 at 14:07
  • Using a memory mapped file is the only way to do it in Java without additional native code. However, as long as you don’t call sync extensively, there is no difference between a “real” temporary file and any other means of creating shared memory. Commented Sep 7, 2015 at 14:34

1 Answer 1

3

If you can obtain a pointer to the memory location, you can use JNA's Pointer methods to access the memory. Alternatively you can create a direct NIO Buffer, which facilitates sharing memory between Java and native.

Any one of these methods should work equally well for sharing memory.

// Use JNA-allocated non-Java heap memory
Memory m = new Memory(size);
myNativeLib.useSharedMemory(m);
// Use JVM-allocated non-Java heap memory
Buffer b = ByteBuffer.allocateDirect(size);
myNativeLib.useSharedMemory(b);
// Use native-allocated memory
Pointer p = myNativeLib.getSharedPointer();

If you want to share more than available physical memory, then you'd be better off using a file-based mapping.

EDIT

Addressing the specific question of accessing a Windows block of Named Shared Memory, you need to understand how to access it via w32 APIs and then access those APIs via JNA (or JNI, if you prefer).

From the MS Docs:

   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;
   }

It's a straightforward matter to map these functions into JNA for use in Java:

import com.sun.jna.platform.win32.Kernel32;
import com.sun.jna.Native;

public interface MyKernel32 extends Kernel32 {
    int FILE_MAP_ALL_ACCESS = // look it up
    MyKernel32 INSTANCE = (MyKernel32)Native.loadLibrary(MyKernel32.class, W32APIOptions.DEFAULT_OPTIONS);
    HANDLE OpenFileMapping(int dwDesiredAccess, boolean bInheritHandle, String lpName);
}

Note that JNA's Kernel32 (in platform.jar) already includes a mapping for MapViewOfFile, which returns a Pointer. Using that returned value, you can read and write to the shared memory to your heart's content.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer, but I'm still a bit lost. I have an application A, which I don't have the source to, which creates a shared memory section called "Global\application_A" and updates the data in there every second (it's not a lot of data, only a few kb). Now I want to create an application B in java, which reads that shared memory of A to print it to the console for example. It doesn't need write access to it, only read.
Please link to details of the definition of "shared memory section". Perhaps you're referring to this?
Yes I think so. When I look at the process with the sysinternal process explorer and display the handles, the "Global\application_A" appears as "Section" and in its properties says "A memory mapped file of paging-file backed memory region" as description. I also can see the address there like "0xFFFFC00203422520", although I don't think that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.