473,624 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7871
Native memory buffers don't need to be pinned, they are not a subject of the
GC.

Willy.

"_iycrd" <_i****@spamtra p.com> wrote in message
news:vj******** *************** *********@4ax.c om...
| 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.Pi nned);

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****@spamtra p.com> wrote in message
news:vj******** *************** *********@4ax.c om...
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("kern el32.dll")]
public static extern void CopyMemory(IntP tr dst, IntPtr src, long
length);

or

[DllImport("kern el32.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****@spamtra p.com> wrote in message
news:td******** *************** *********@4ax.c om...
| 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
461
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
3
1883
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 { byte pB = new byte;
7
6741
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. SWXXXXXXXXXYYYYZZZZZZZZZZZZZZZZZZZZZ Where S is the command for SEND and should just be the character S. Where W is a byte representing how long the filename (testfile.txt) is. In this case 12. Where XXXXXXX is converted from a string that...
7
6857
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 --------------------------------------------------- ASCIIEncoding asciiEncoder = new ASCIIEncoding();
2
11307
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 loaded Dim data64_1 As String = System.Convert.ToBase64String(result1, 0, result1.Length) Dim data64_2 As String = System.Convert.ToBase64String(result2, 0, result2.Length) Debug.WriteLine(IIf(data64_1 = data64_2, "Equals", "NOT Equals"))
6
2765
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) using redim arrays to add to a normal byte array (2) using an ArrayList and finally (3) using a memorystream. These three classes are listed below the test sub called "TestBuildByteArray". It was interesting that using the memorystream was...
5
40084
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
7900
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. Its signature is deliberately open-ended so that it can be highly generic. Refer SDK for more. I want to access the drive geommetry of an SD-Card via
3
4170
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 added two items discussed in the previous post: 1. different length arrays 2. buffer must be divisible by 4. I simply check the remaining bytes using the two byte pointers. public unsafe static bool CompareByteArrays(byte b1, byte b2) {...
0
8240
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, 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...
0
8625
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 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...
1
8336
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,...
0
8482
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4082
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
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
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
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...

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.