Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using Net 5 (C#).

I need to read a file into unmanaged memory (and write unmanaged memory to a file).

The only way I know is to use Windows API 'CreateFile' to get a handle to the file and then use ```ReadFile``` or ```WriteFile``` imported from ```Kernel32```.

This works directly in Windows.

It fails when running in a Linux Docker container under Windows 10 Pro.

It fails with:
Unable to load shared library 'kernel32' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libkernel32: cannot open shared object file: No such file or directory


How can I accomplish the read/write inside a Linux Docker container?

What I have tried:

Building a Docker image via:

FROM mcr.microsoft.com/dotnet/runtime:5.0 AS base


FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base


FROM mcr.microsoft.com/dotnet/sdk:5.0 AS base


None of the above worked.
Posted
Updated 9-Jun-21 13:08pm
Comments
Richard Deeming 3-Jun-21 4:05am    
The unmanaged APIs for Windows and Linux are completely different. You'll need to write different code for each platform you need to support.

Why are you using unmanaged memory? Perhaps if you explain what you're actually trying to achieve, somebody might be able to suggest a better solution.
[no name] 3-Jun-21 11:46am    
Yes. I also thought: do they let you stick an OS into a Docker container? Isn't that a VM then? (I don't know Docker)
david marcus 9-Jun-21 19:04pm    
The need for unmanaged memory comes from the data organization in the app. It is a memory-based DB that holds about 2.5M records per 1 GB of memory. Each record consists of many fields; some are scalar values, some fields are arrays. To get this many records per 1 GB of memory, the records contain inline values (NO managed objects - just a C struct with primitive values).

If these records had to be real .Net objects, the amount of memory required would be 10X, at least!

In any event, the real question is NOT why the app is using unmanaged memory (which IS supported in .Net 5), but rather how to read from a file (using .Net 5) into unmanaged memory.

After more research I found a solution:

I use File.Open(...) to get a FileStream object to the file. I then use Read(ReadOnlySpan<byte> buffer) to get it to read directly into unmanaged memory.

Thanks for trying to help.

1 solution

Use: var fs = File.Open(...)

to get a FileStream object to the file.

Use: fs.Read(new ReadOnlySpan(pBuffer)) ;

where: byte* pBuffer; //-- pointer to unmanaged memory
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900