473,396 Members | 2,070 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,396 software developers and data experts.

PInvoke - Pinning byte arrays

After numerous problems, I'm having second thoughts about using
C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like
it will take a huge amount of work, if it will work at all.

The native DLL uses raw data buffers that are normally handed to it
(via pointer) from another native function. If I'm piloting this with
C#/PInvoke, how could those raw data buffers be created and
immobilized? In C++/CLI, there is pin_ptr to keep the CLR's memory
manager from moving things, but I don't see how this can be done in
C#/PInvoke. Can it?

Mar 9 '06 #1
5 7843
Native memory buffers don't need to be pinned, they are not a subject of the
GC.

Willy.

"_iycrd" <_i****@spamtrap.com> wrote in message
news:vj********************************@4ax.com...
| After numerous problems, I'm having second thoughts about using
| C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like
| it will take a huge amount of work, if it will work at all.
|
| The native DLL uses raw data buffers that are normally handed to it
| (via pointer) from another native function. If I'm piloting this with
| C#/PInvoke, how could those raw data buffers be created and
| immobilized? In C++/CLI, there is pin_ptr to keep the CLR's memory
| manager from moving things, but I don't see how this can be done in
| C#/PInvoke. Can it?
|
Mar 9 '06 #2
Hi,

I'm not exactlu sure I understand your question correctly. Maybe Willy
already answered your question, but just in case if you want to pin a object
in the managed heap so the GC won't move it arround you need to create a
GCHandle to it of type Pinned such as

GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Pinned);

To unpin the object use GCHandle.Free method.

GCHandles can be converted to and from IntPtr for using with PInvoke.

Keep in mind that pinning objects in managed heap can undermine GC
efficiency.
--
HTH
Stoitcho Goutsev (100)

"_iycrd" <_i****@spamtrap.com> wrote in message
news:vj********************************@4ax.com...
After numerous problems, I'm having second thoughts about using
C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like
it will take a huge amount of work, if it will work at all.

The native DLL uses raw data buffers that are normally handed to it
(via pointer) from another native function. If I'm piloting this with
C#/PInvoke, how could those raw data buffers be created and
immobilized? In C++/CLI, there is pin_ptr to keep the CLR's memory
manager from moving things, but I don't see how this can be done in
C#/PInvoke. Can it?

Mar 9 '06 #3
If you declare the API correctly the framework will marshal data types
for you and ensure that they are either copied or pinned. In the case
of blittable types (a byte array is blittable) they are pinned during
marshaling.

Consider the following DllImport declarations.

[DllImport("kernel32.dll")]
public static extern void CopyMemory(IntPtr dst, IntPtr src, long
length);

or

[DllImport("kernel32.dll")]
public static extern void CopyMemory(byte[] dst, byte[] src, long
length);

You can make both declarations work, but in the first example you would
have to obtain a pointer to your buffer and the pin/unpin it manually.
If you declare the API similar to the second example then the marshaler
will do all of this for you.

Brian
_iycrd wrote:
After numerous problems, I'm having second thoughts about using
C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like
it will take a huge amount of work, if it will work at all.

The native DLL uses raw data buffers that are normally handed to it
(via pointer) from another native function. If I'm piloting this with
C#/PInvoke, how could those raw data buffers be created and
immobilized? In C++/CLI, there is pin_ptr to keep the CLR's memory
manager from moving things, but I don't see how this can be done in
C#/PInvoke. Can it?


Mar 9 '06 #4
On Thu, 9 Mar 2006 14:13:22 +0100, "Willy Denoyette [MVP]"
<wi*************@telenet.be> wrote:
Native memory buffers don't need to be pinned, they are not a subject of the
GC.

Willy.


Hi Willy,

Does C# have provision for creating native buffers?

Mar 10 '06 #5


"_iycrd" <_i****@spamtrap.com> wrote in message
news:td********************************@4ax.com...
| On Thu, 9 Mar 2006 14:13:22 +0100, "Willy Denoyette [MVP]"
| <wi*************@telenet.be> wrote:
|
| >Native memory buffers don't need to be pinned, they are not a subject of
the
| >GC.
| >
| >Willy.
|
| Hi Willy,
|
| Does C# have provision for creating native buffers?
|

If you mean buffers in the process heap instead of the GC heap, look at the
Marshal class, there are a number of methods (AllocHGloba, AllocCoTaskMem)
that do just that.

Willy.
Mar 10 '06 #6

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

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
3
by: Ted Miller | last post by:
Hi folks, I've got an unmanaged routine I'm pinvoking to. It takes a pointer to an array of 3 pointers to bytes, typed as byte**. public static extern void foo(byte **p3pb); unsafe {...
7
by: War Eagle | last post by:
I have two byte arrays and a char (the letter S) I was to concatenate to one byte array. Here is what code I have. I basically want to send this in a one buffer (byte array?) through a socket. ...
7
by: Joseph Lee | last post by:
Hi All, I am having problem when i am using hashtable to keep an array of bytes value as keys. Take a look at the code snippet below --------------------------------------------------- ...
2
by: Tom | last post by:
What's the best way to compare two byte arrays? Right now I am converting them to base64 strings and comparing those, as so: 'result1 and result2 are two existing byte arrays that have been...
6
by: Dennis | last post by:
I was trying to determine the fastest way to build a byte array from components where the size of the individual components varied depending on the user's input. I tried three classes I built: (1)...
5
by: Oleg Subachev | last post by:
Is there other way of comparing two byte arrays than iterating through the two and compare individual bytes ? Oleg Subachev
2
by: steve | last post by:
Hi all, I want to understand more about how the pinvoke pinning process works. I'm writing some code that calls DeviceIoControl. DeviceIoControl provides a generic interface to device drivers....
3
by: RobbSadler | last post by:
This post was meant to go at the end of another post named "How to Compare Two Byte Arrays" which was answered by Vadym Stetsyak, but it was closed. I used his code from that post to create this, and...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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...
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,...

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.