Sune wrote:
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
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?
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_RE AD = 0x0020;
const int PAGE_EXECUTE_RE ADWRITE = 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_AC CESS = 0x001F;
[StructLayout( LayoutKind.Sequ ential )]
class SECURITY_ATTRIB UTES
{
//int nLength;
//int lpSecurityDescr iptor;
//int bInheritHandle;
}
[DllImport("kern el32.dll")]
static extern int CreateFileMappi ngA ( uint hFile,
SECURITY_ATTRIB UTES
lpFileMappigAtt ributes,
int flProtect,
int dwMaximumSizeHi gh,
int dwMaximumSizeLo w,
string lpName);
[DllImport("kern el32.dll")]
static extern IntPtr MapViewOfFile ( int hFileMappingObj ect,
int dwDesiredAccess ,
int dwFileOffsetHig h,
int dwFileOffsetLow ,
int dwNumberOfBytes ToMap);
[DllImport("kern el32.dll")]
static extern int UnmapViewOfFile (IntPtr lpBaseAddress);
[DllImport("kern el32.dll")]
static extern int CloseHandle ( int hObject);
public static void Main(string[] args)
{
int mem = CreateFileMappi ngA(0xFFFFFFFF, null, PAGE_READWRITE,
0, 8192, "GBLMEM");
IntPtr data = MapViewOfFile(m em, FILE_MAP_WRITE, 0, 0, 8192);
Console.WriteLi ne("Run Delphi program and press enter");
Console.ReadLin e();
int[] data2 = new int[2048];
Marshal.Copy( data, data2, 0, 3 );
UnmapViewOfFile (data);
CloseHandle(mem );
Console.WriteLi ne(data2[0]);
Console.WriteLi ne(data2[1]);
Console.WriteLi ne(data2[2]);
}
}