Hi, guys,
I got a problem: a java routine:
--------------------------------------------------------------------------------
public class DummyGenerator {
static {
System.loadLibr ary("dummy");
}
public static native int generateDummy(S tring sysid, char type, String symbname, String symbvers, ByteBuffer buffer);
}
public class Main {
public static void main(String[] args) {
final ByteBuffer b = ByteBuffer.allo cateDirect(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("dumm y.dll",EntryPoi nt="_Java_com_o sm_datamgmt_uti l_DummyGenerato r_generateDummy @28",ExactSpell ing=true,Callin gConvention=Cal lingConvention. Cdecl)]
public static unsafe extern int _Java_com_osm_d atamgmt_util_Du mmyGenerator_ge nerateDummy(Str ing sysid, char type, String symbname, String symbvers, byte* buffer);
}
[...] public void testGenerateDum my()
{
const int memBlockSize = 8192;
IntPtr memBlock;
byte[] byteArray = new byte[memBlockSize];
int size = 0;
unsafe
{
memBlock = Marshal.AllocCo TaskMem(memBloc kSize); // get a block of memory
void* pointer = memBlock.ToPoin ter();
byte* bpointer = (byte*) pointer;
// ------- here comes the access violation error ----
size = myGenerateDummy ._Java_com_osm_ datamgmt_util_D ummyGenerator_g enerateDummy("I D-STRING","p", "", "", (byte*)memBlock .ToPointer());
// ------- here comes the access violation error ----
Marshal.Copy(me mBlock, byteArray, 0, byteArray.Lengt h);
Marshal.FreeCoT askMem(memBlock );
}
}
}
}
--------------------------------------------------------------------------------
but it gives me a System.AccessVi olationExceptio n (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
I got a problem: a java routine:
--------------------------------------------------------------------------------
public class DummyGenerator {
static {
System.loadLibr ary("dummy");
}
public static native int generateDummy(S tring sysid, char type, String symbname, String symbvers, ByteBuffer buffer);
}
public class Main {
public static void main(String[] args) {
final ByteBuffer b = ByteBuffer.allo cateDirect(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("dumm y.dll",EntryPoi nt="_Java_com_o sm_datamgmt_uti l_DummyGenerato r_generateDummy @28",ExactSpell ing=true,Callin gConvention=Cal lingConvention. Cdecl)]
public static unsafe extern int _Java_com_osm_d atamgmt_util_Du mmyGenerator_ge nerateDummy(Str ing sysid, char type, String symbname, String symbvers, byte* buffer);
}
[...] public void testGenerateDum my()
{
const int memBlockSize = 8192;
IntPtr memBlock;
byte[] byteArray = new byte[memBlockSize];
int size = 0;
unsafe
{
memBlock = Marshal.AllocCo TaskMem(memBloc kSize); // get a block of memory
void* pointer = memBlock.ToPoin ter();
byte* bpointer = (byte*) pointer;
// ------- here comes the access violation error ----
size = myGenerateDummy ._Java_com_osm_ datamgmt_util_D ummyGenerator_g enerateDummy("I D-STRING","p", "", "", (byte*)memBlock .ToPointer());
// ------- here comes the access violation error ----
Marshal.Copy(me mBlock, byteArray, 0, byteArray.Lengt h);
Marshal.FreeCoT askMem(memBlock );
}
}
}
}
--------------------------------------------------------------------------------
but it gives me a System.AccessVi olationExceptio n (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
Comment