Java bytebuffer equivalent in c#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GiuseppeMicheli
    New Member
    • Jan 2010
    • 2

    Java bytebuffer equivalent in c#

    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
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Try using IntPtr as argument the and passing it in as the bytebuffer?

    IntPtr myP = Marshal.AllocHG lobal(8192);

    Then use myP as the bytebuffer argument?

    Comment

    • GiuseppeMicheli
      New Member
      • Jan 2010
      • 2

      #3
      Hiya Plater,
      I just did the attempt:

      [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, IntPtr buffer);

      ...

      unsafe
      {
      IntPtr myP = Marshal.AllocHG lobal(8192);
      size = myGenerateDummy ._Java_com_osm_ datamgmt_util_D ummyGenerator_g enerateDummy("I D-CODE","a", "", "", myP);
      }

      but the violation exception error still remains...
      Thank u a lot for the moment...
      Giuseppe

      Comment

      Working...