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(unsigne d long ObjHandle, unsigned long DataSize, void*
DataPtr)
---------- end C++ ----------
>From C# I have defined this function as:
---------- C# ----------
[DllImport("Nica n.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.AllocHG lobal(8);
Marshal.Copy(Da taWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAd dress, 8 , ptr);
Marshal.FreeHGl obal(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.AllocH Global, Marshal.Copy, Marshal.FreeHGl obal)?
Thank you very much 3 21351
"Beorne" <ma*******@gmai l.comschrieb im Newsbeitrag
news:11******** *************@u 30g2000hsc.goog legroups.com...
>From C# I have defined this function as:
---------- C# ----------
[DllImport("Nica n.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.AllocHG lobal(8);
Marshal.Copy(Da taWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAd dress, 8 , ptr);
Marshal.FreeHGl obal(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.AllocHG lobal(8);
try
{
Marshal.Copy(Da taWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAd dress, 8, ptr);
}
finally
{
Marshal.FreeHGl obal(ptr);
}
Christof
Thanks!
On 30 Mag, 10:39, "Christof Nordiek" <c...@nospam.de wrote:
...
You don't really need to manually marshal the data. Change your C#
declaration to:
[DllImport("Nica n.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*******@gmai l.comwrote in message
news:11******** *************@u 30g2000hsc.goog legroups.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(unsigne d long ObjHandle, unsigned long DataSize, void*
DataPtr)
---------- end C++ ----------
>>From C# I have defined this function as:
---------- C# ----------
[DllImport("Nica n.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.AllocHG lobal(8);
Marshal.Copy(Da taWrite, 0, ptr, 8);
myStatus = ncWrite(WriteAd dress, 8 , ptr);
Marshal.FreeHGl obal(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.AllocH Global, Marshal.Copy, Marshal.FreeHGl obal)?
Thank you very much This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 length of the message,
and then the rest are the actual content of the message.
So I have a big char array, what is the easiest way to store an 8-byte integer
across the first 8 characters in the array?
For Examples:
|
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 *pId;
char *pBuffer;
|
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 operation, and I have to use it from my C# program. I am really unsure what type of marshalling I should use.
The data parameter is a pointer to an array of byte data. This data must not be copied when calling the function. Plus, the .NET...
|
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 straight byte data (that is,
I'm concerned with individual bits packed into the bytes), I'd like to put
that data into a byte array that's passed from the C# app to the DLL
function.
In the past, I've been able to marshal strings to character strings...
|
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 receive a byte array as one of its
parameters. The project is marked for COM interop, and that all
proceeds normally.
When I reference the type library in the VB6 project, and write the
code to call the function that returns the byte array, it works
| |
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 have a methods with signitures like the following...
unsigned char someMethod(unsigned char blah, SOMESTRUCT* somestruct);
|
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 you?
I have seen from several posts that you have to do special conversions for
strings, but what about the other types such as int, long,ect. In my case I
want to save it as a member variable from a managed class into an unmanaged
class's.
|
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 this:
public static extern int SNAPI_SetVersionBuffer(IntPtr DeviceHandle,
|
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 need to pass an array of RADIO_INFO2 structures to be filled by a
function in the DLL. This is how the structure is defined in the C++
example that comes with the DLL:
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |