473,466 Members | 1,511 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A hint on marshalling a byte array into a void* pointer

I have to pass a byte array as an input parameter to a function in a
propertary dll.
The c++ signature of the original function is the following:

---------- C++ ----------

int ncWrite(unsigned long ObjHandle, unsigned long DataSize, void*
DataPtr)

---------- end C++ ----------
>From C# I have defined this function as:
---------- C# ----------

[DllImport("Nican.dll")]
unsafe static extern int ncWrite(uint ObjHandle, uint DataSize,
IntPtr DataPtr);

---------- end C# ----------

And I use it as:

---------- C# ----------

byte[] DataWrite = {0x23, 0x23, 0x30, 0x03, 0x78, 0xEC, 0xFF, 0xFF };
int myStatus;

IntPtr ptr = Marshal.AllocHGlobal(8);
Marshal.Copy(DataWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAddress, 8 , ptr);
Marshal.FreeHGlobal(ptr);

---------- end C# ----------

This works, but having to handle unmanaged code I''m worried by memory
leaks (this function is called thousdands of times per minute).
Is it the usage correct? Have I choosen the right Marshal methods
(Marshal.AllocHGlobal, Marshal.Copy, Marshal.FreeHGlobal)?

Thank you very much

May 30 '07 #1
3 21254
"Beorne" <ma*******@gmail.comschrieb im Newsbeitrag
news:11*********************@u30g2000hsc.googlegro ups.com...
>From C# I have defined this function as:

---------- C# ----------

[DllImport("Nican.dll")]
unsafe static extern int ncWrite(uint ObjHandle, uint DataSize,
IntPtr DataPtr);

---------- end C# ----------

And I use it as:

---------- C# ----------

byte[] DataWrite = {0x23, 0x23, 0x30, 0x03, 0x78, 0xEC, 0xFF, 0xFF };
int myStatus;

IntPtr ptr = Marshal.AllocHGlobal(8);
Marshal.Copy(DataWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAddress, 8 , ptr);
Marshal.FreeHGlobal(ptr);

---------- end C# ----------
This seems to be OK. You should add a try/finally to get sure the memory
gets cleared in case of an exception.

IntPtr ptr = Marshal.AllocHGlobal(8);
try
{
Marshal.Copy(DataWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAddress, 8, ptr);
}
finally
{
Marshal.FreeHGlobal(ptr);
}

Christof
May 30 '07 #2
Thanks!

On 30 Mag, 10:39, "Christof Nordiek" <c...@nospam.dewrote:
...
May 30 '07 #3
You don't really need to manually marshal the data. Change your C#
declaration to:

[DllImport("Nican.dll")]
unsafe static extern int ncWrite(uint ObjHandle, uint DataSize, byte[]
DataPtr);

and then just pass in DataWrite as is. The runtime marshaller will pin your
array and then pass in a pointer to the unmanaged function.

"Beorne" <ma*******@gmail.comwrote in message
news:11*********************@u30g2000hsc.googlegro ups.com...
>I have to pass a byte array as an input parameter to a function in a
propertary dll.
The c++ signature of the original function is the following:

---------- C++ ----------

int ncWrite(unsigned long ObjHandle, unsigned long DataSize, void*
DataPtr)

---------- end C++ ----------
>>From C# I have defined this function as:

---------- C# ----------

[DllImport("Nican.dll")]
unsafe static extern int ncWrite(uint ObjHandle, uint DataSize,
IntPtr DataPtr);

---------- end C# ----------

And I use it as:

---------- C# ----------

byte[] DataWrite = {0x23, 0x23, 0x30, 0x03, 0x78, 0xEC, 0xFF, 0xFF };
int myStatus;

IntPtr ptr = Marshal.AllocHGlobal(8);
Marshal.Copy(DataWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAddress, 8 , ptr);
Marshal.FreeHGlobal(ptr);

---------- end C# ----------

This works, but having to handle unmanaged code I''m worried by memory
leaks (this function is called thousdands of times per minute).
Is it the usage correct? Have I choosen the right Marshal methods
(Marshal.AllocHGlobal, Marshal.Copy, Marshal.FreeHGlobal)?

Thank you very much

May 30 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Terence | last post by:
Help Need!!! I am writing a program for a distributed system course. I need to marshall all the messages in order to send them thru a socket. The first 8 bytes of the message are used to store the...
7
by: Mick | last post by:
Does anyone know how to pass an array of structures to a DLL? Here's what I'm doing... the VB.Net error is at the end. *** C++ Structure Declaration: typedef struct _SOME_STRUCT { char...
4
by: TT (Tom Tempelaere) | last post by:
Hi people I am wrapping a C dll using PInvoke from C#. I need to wrap the following signature in C int dma_start( const UCHAR* data, UINT data_length ) The function should start with a DMA...
3
by: Lee Crabtree | last post by:
I have a managed DLL that I've used to expose a C++ class. One of the functions in this class reads a line out of a file into a buffer of type "unsigned char*". Since all the data is just...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
1
by: | last post by:
Hi, Is there any good links for datatype interop? I need to pass some structure pointers into an unmanaged method and return char* etc but having some problems in my C++/CLI proxy class. I...
2
by: BartMan | last post by:
Greetings, When working with managed c++, do you have to do anything special when going from simple types from managed to unmanaged and vice versa. Or is marshalling handled automatically for...
11
by: Daniel Bass | last post by:
Greetings! I'm trying to call this method in a c# app... SNAPIDLL_API int __stdcall SNAPI_SetCapabilitiesBuffer(HANDLE DeviceHandle, unsigned char *pData, long max_length); So far I've got...
2
by: calenlas | last post by:
Hi all, I'm taking my first steps into C# <--C++ DLL Interop and unfortunately I've run into (what seems to be) a very complicated case as my first task. Perhaps someone here can help me. I...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.