Hi all,
I'm not a C# programmer so please be gentle:
Prereq:
---------------
- The lookup service (see below) implemented by a C module cannot be
re-written in C# ;-)
- The lookup service is to be loaded into the process of the C#
application, i.e. I want to avoid expensive IPC
1)
I got a C module that organizes a big block of memory into a fairly
nice lookup service. This C module exposes a C API.
>From my understanding, C# being a pointerless language, C# client
code cannot by any means, by mistake, access the memory block managed
by the C module. Is this correct? Will the bridge between C and C#
open the can of worms I try to avoid by using a pointerless language?
Note:
In C/C++, a rouge pointer can very easily damage the lookup service
and this would not be noticed until several days have passed. This is
not acceptable.
Thank you for your time
/Sune
Nicholas Paldino [.NET/C# MVP] - 09 Sep 2007 17:00 GMT
Sune,
Well, you could access the memory block that is used by the C module if
you REALLY wanted to, but generally, the way it works is that you are going
to call the API through the P/Invoke layer, which is going to be responsible
for marshaling the .NET types to the unmanaged module and back.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hi all,
>
[quoted text clipped - 23 lines]
> Thank you for your time
> /Sune
Sune - 10 Sep 2007 01:39 GMT
On 10 Sep, 01:00, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.com> wrote:
> Sune,
>
[quoted text clipped - 34 lines]
> > Thank you for your time
> > /Sune
Thanks Nicholas!
Arne Vajhøj - 10 Sep 2007 03:37 GMT
> Prereq:
> ---------------
> - The lookup service (see below) implemented by a C module cannot be
> re-written in C# ;-)
> - The lookup service is to be loaded into the process of the C#
> application, i.e. I want to avoid expensive IPC
You asked the same question in the Java group. Do you need the
solution in C# or Java or both ?
> 1)
> I got a C module that organizes a big block of memory into a fairly
[quoted text clipped - 4 lines]
> by the C module. Is this correct? Will the bridge between C and C#
> open the can of worms I try to avoid by using a pointerless language?
Pure managed C# can not overwrite memory.
You will need to call unmanaged (native) code to do what
you want.
And that code can overwrite memory.
If you want to have your C# code access global memory
managed by the C code, then I think you can use only your C# code and
native code provided by Microsoft.
See below for a code snippet (some C# code that communicates
via some Delphi code via global memory.
Arne
==============================
using System;
using System.Runtime.InteropServices;
class MainClass
{
const int PAGE_READONLY = 0x0002;
const int PAGE_READWRITE = 0x0004;
const int PAGE_EXECUTE = 0x0010;
const int PAGE_EXECUTE_READ = 0x0020;
const int PAGE_EXECUTE_READWRITE = 0x0040;
const int PAGE_GUARD = 0x0100;
const int PAGE_NOACCESS = 0x0001;
const int PAGE_NOCACHE = 0x0200;
const int FILE_MAP_COPY = 0x0001;
const int FILE_MAP_WRITE = 0x0002;
const int FILE_MAP_READ = 0x0004;
const int FILE_MAP_ALL_ACCESS = 0x001F;
[StructLayout( LayoutKind.Sequential )]
class SECURITY_ATTRIBUTES
{
//int nLength;
//int lpSecurityDescriptor;
//int bInheritHandle;
}
[DllImport("kernel32.dll")]
static extern int CreateFileMappingA ( uint hFile,
SECURITY_ATTRIBUTES
lpFileMappigAttributes,
int flProtect,
int dwMaximumSizeHigh,
int dwMaximumSizeLow,
string lpName);
[DllImport("kernel32.dll")]
static extern IntPtr MapViewOfFile ( int hFileMappingObject,
int dwDesiredAccess,
int dwFileOffsetHigh,
int dwFileOffsetLow,
int dwNumberOfBytesToMap);
[DllImport("kernel32.dll")]
static extern int UnmapViewOfFile (IntPtr lpBaseAddress);
[DllImport("kernel32.dll")]
static extern int CloseHandle ( int hObject);
public static void Main(string[] args)
{
int mem = CreateFileMappingA(0xFFFFFFFF, null, PAGE_READWRITE,
0, 8192, "GBLMEM");
IntPtr data = MapViewOfFile(mem, FILE_MAP_WRITE, 0, 0, 8192);
Console.WriteLine("Run Delphi program and press enter");
Console.ReadLine();
int[] data2 = new int[2048];
Marshal.Copy( data, data2, 0, 3 );
UnmapViewOfFile(data);
CloseHandle(mem);
Console.WriteLine(data2[0]);
Console.WriteLine(data2[1]);
Console.WriteLine(data2[2]);
}
}