Hi, guys,
I got a problem: a java routine:
--------------------------------------------------------------------------------
public class DummyGenerator {
static {
System.loadLibrary("dummy");
}
public static native int generateDummy(String sysid, char type, String symbname, String symbvers, ByteBuffer buffer);
}
public class Main {
public static void main(String[] args) {
final ByteBuffer b = ByteBuffer.allocateDirect(8192);
final int size = DummyGenerator.generateDummy("IDSTRING",
"p", "", "", b);
}
}
--------------------------------------------------------------------------------
allows me to allocate a memory portion of 8192 byte as a size and lets an external DLL written in VisualC++ (dummy.dll) to "return" (write within) the result of some elaboration.
Now, I need to migrate this code in .NET platform (i.e. C#); I wrote down the following code:
--------------------------------------------------------------------------------
using System;
using System.Runtime.InteropServices;
class myGenerateDummy
{
private static byte[] DUMMY_BUFFER = new byte[8192];
[DllImport("dummy.dll",EntryPoint="_Java_com_osm_da tamgmt_util_DummyGenerator_generateDummy@28",Exact Spelling=true,CallingConvention=CallingConvention. Cdecl)]
public static unsafe extern int _Java_com_osm_datamgmt_util_DummyGenerator_generat eDummy(String sysid, char type, String symbname, String symbvers, byte* buffer);
}
[...] public void testGenerateDummy()
{
const int memBlockSize = 8192;
IntPtr memBlock;
byte[] byteArray = new byte[memBlockSize];
int size = 0;
unsafe
{
memBlock = Marshal.AllocCoTaskMem(memBlockSize); // get a block of memory
void* pointer = memBlock.ToPointer();
byte* bpointer = (byte*) pointer;
// ------- here comes the access violation error ----
size = myGenerateDummy._Java_com_osm_datamgmt_util_DummyG enerator_generateDummy("ID-STRING","p", "", "", (byte*)memBlock.ToPointer());
// ------- here comes the access violation error ----
Marshal.Copy(memBlock, byteArray, 0, byteArray.Length);
Marshal.FreeCoTaskMem(memBlock);
}
}
}
}
--------------------------------------------------------------------------------
but it gives me a System.AccessViolationException (read/write attempt to a protected memory region).
Can you please suggest me hot to let the external dll write to that portion of memory without system exception?
Thank u a lot,
Giuseppe