Jump to content
jaenicke

Using a memory-mapped file of a Delphi app in C#

Recommended Posts

Hello,

 

I used memory-mapped files a few times already, but it was always a Delphi application. I had no problems to create a MMF and open it with another Delphi application.

 

Now I tried to connect to a MMF created by a Delphi application, but this time I tried it from a C# application. Unfortunately it does not find the MMF. If I prepend Global\, I need administrator privileges, but it does not work either.

 

The code is straightforward:

// Delphi side:
CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, MMFSize, PChar(MMFName));

// C# side:
MemoryMappedFile.OpenExisting(mmfName, MemoryMappedFileRights.ReadWrite);

I thought about using the API functions directly, but I did not try it yet. Is anything wrong with this code? Why doesn't it work?

 

Thank you

Sebastian

Share this post


Link to post
On 9/9/2021 at 11:42 PM, jaenicke said:

Now I tried to connect to a MMF created by a Delphi application, but this time I tried it from a C# application. Unfortunately it does not find the MMF.

What is the actual value of MMFName on both sides?  What error does OpenExisting() report when it fails?

On 9/9/2021 at 11:42 PM, jaenicke said:

The code is straightforward:


// Delphi side:
CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, MMFSize, PChar(MMFName));

Is that call actually succeeding or failing? Also, why are you hard-coding $FFFFFFFF instead of using the INVALID_HANDLE_VALUE constant, like the documentation says to use?  Are you compiling for 32bit or 64bit? For 64bit, $FFFFFFFF extended to $00000000FFFFFFFF would be the wrong value to use, it would need to be $FFFFFFFFFFFFFFFF instead.

On 9/9/2021 at 11:42 PM, jaenicke said:

I thought about using the API functions directly, but I did not try it yet.

I don't think that is going to make a difference, since MemoryMappedFile.OpenExisting() just calls the Win32 OpenFileMapping() API (you can see the source code for yourself at https://referencesource.microsoft.com/#system.core/System/IO/MemoryMappedFiles/MemoryMappedFile.cs)

On 9/9/2021 at 11:42 PM, jaenicke said:

Is anything wrong with this code? Why doesn't it work?

Not enough information to diagnose that..

Share this post


Link to post

Thank you for your reply!

 

I wrote test applications to append them here.

 

When I was done, I tested a bit. And I found out, that this works:

MemoryMappedFile.OpenExisting(mmfName);

But if I use MemoryMappedFileRights.Read (which works in Delphi) or MemoryMappedFileRights.ReadWrite, it does not work anymore. I could use API Monitor to dig deeper, but for the moment I am glad that it works.

 

I'll append the test projects soon.

Share this post


Link to post
On 9/17/2021 at 11:24 PM, jaenicke said:

When I was done, I tested a bit. And I found out, that this works:


MemoryMappedFile.OpenExisting(mmfName);

As you can see in the source code I linked to earlier, that simply calls this:

 return OpenExisting(mapName, MemoryMappedFileRights.ReadWrite, HandleInheritability.None);
On 9/17/2021 at 11:24 PM, jaenicke said:

But if I use MemoryMappedFileRights.Read (which works in Delphi) or MemoryMappedFileRights.ReadWrite, it does not work anymore.

That simply calls this:

return OpenExisting(mapName, desiredAccessRights, HandleInheritability.None);

Which ultimately just calls OpenFileMapping() with the specified name, access/inheritance rights as-is, and then wraps the HANDLE in a MemoryMappedFile object.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×