473,407 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Java bytebuffer equivalent in c#

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
Jan 4 '10 #1
2 7134
Plater
7,872 Expert 4TB
Try using IntPtr as argument the and passing it in as the bytebuffer?

IntPtr myP = Marshal.AllocHGlobal(8192);

Then use myP as the bytebuffer argument?
Jan 4 '10 #2
Hiya Plater,
I just did the attempt:

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

...

unsafe
{
IntPtr myP = Marshal.AllocHGlobal(8192);
size = myGenerateDummy._Java_com_osm_datamgmt_util_DummyG enerator_generateDummy("ID-CODE","a", "", "", myP);
}

but the violation exception error still remains...
Thank u a lot for the moment...
Giuseppe
Jan 5 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: ak | last post by:
am I the only one to say that the java.nio. is bullshit? i have been trying to write some code using it to transfer file over the net. small file it is ok, big file -- problematic. ...
73
by: RobertMaas | last post by:
After many years of using LISP, I'm taking a class in Java and finding the two roughly comparable in some ways and very different in other ways. Each has a decent size library of useful utilities...
6
by: Patrick | last post by:
Hello all! I am porting an application from C++ to Java and have run into a problem using the DataInputStream reader object. The file I am trying to read in is anywhere from 20 to 60 MB and has a...
30
by: Richard | last post by:
Level: Java newbie, C experienced Platform: Linux and Win32, Intel Another programmer and I are working on a small project together. He's writing a server process in Java that accepts input...
2
by: yaron | last post by:
Hi, for porting java to c# i need to know: What is the C#.NET equivalent for java.net.SocketAddress and java.nio.ByteBuffer ? Thanks.
0
by: yaron | last post by:
Hi, 1. How can i know that a socket finished the CONNECT operation or failed when i use the Socket.Select method to monitor a list of sockets ? 2. Is there is any more detailed document on...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
9
by: Simple Simon | last post by:
Java longs are 8 bytes. I have a Java long that is coming in from the network, and that represents milliseconds since Epoch (Jan 1 1970 00:00:00). I'm having trouble understanding how to get it...
2
by: sweety mehra | last post by:
How to convert ByteBuffer value into String
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.