473,327 Members | 1,976 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,327 software developers and data experts.

Pinning Pointers with __pin in Managed C++

I have two questions:
a) From documentation located at
http://msdn.microsoft.com/library/de...nsSpec_7_7.asp,
it says, "Pinning a sub-object defined in a managed object has the effect of
pinning the entire object. For example, if any element of an array is pinned,
then the whole array is also pinned. There are no extensions to the language
for declaring a pinned array. To pin an array, declare a pinning pointer to
its element type, and pin one of its elements."

My question is, does this still apply if the array is an array of reference
classes instead of value classes? Seems to me that pinning the array (by
pinning the first element of the array) would pin the array itself, and not
necessarily the actual reference class objects which are located elsewhere in
the managed heap.

b) If my assumption in (a) is correct, that means that it is harder to pin
all of the reference class elements of an array. How would you do this?
With an array of __pin pointers? Should this work?

MyObject __pin *pinners __gc[] = new MyObject*[numObjects];
vector<char*> v;
for (int i = 0; i < numObjects; i++)
{
pinners[i] = new MyObject();
v.push_back(static_cast<char*>(pinners[i]));
}
UnmanagedFunction(v);
Nov 17 '05 #1
3 2190
Hexar Anderson wrote:
I have two questions:
a) From documentation located at
http://msdn.microsoft.com/library/de...nsSpec_7_7.asp,
it says, "Pinning a sub-object defined in a managed object has the effect of
pinning the entire object. For example, if any element of an array is pinned,
then the whole array is also pinned. There are no extensions to the language
for declaring a pinned array. To pin an array, declare a pinning pointer to
its element type, and pin one of its elements."

My question is, does this still apply if the array is an array of reference
classes instead of value classes? Seems to me that pinning the array (by
pinning the first element of the array) would pin the array itself, and not
necessarily the actual reference class objects which are located elsewhere in
the managed heap.

b) If my assumption in (a) is correct, that means that it is harder to pin
all of the reference class elements of an array. How would you do this?
With an array of __pin pointers? Should this work?

MyObject __pin *pinners __gc[] = new MyObject*[numObjects];
vector<char*> v;
for (int i = 0; i < numObjects; i++)
{
pinners[i] = new MyObject();
v.push_back(static_cast<char*>(pinners[i]));
}
UnmanagedFunction(v);

For this situation you cannot use pinned pointers (which are only legal
on the stack) but you should use a pining handle. Create a GCHandle for
each reference, call the GCHandle Alloc member 2 parameter overload with
Pinned as the handle type. Put the handle in an array, create a second
array (of IntPTR) and put the result of calling
GCHandle::AddressOfPinnedObject into that and then you can pass that to
native code.

Note that a design that needs this probably requires rethinking since
keeping lots of pinnen objects (like an array of them) around for a long
time isn't something to do too lightly.

Ronald Laeremans
Visual C++ team

Nov 17 '05 #2
"Ronald Laeremans [MSFT]" wrote:
For this situation you cannot use pinned pointers (which are only legal
on the stack) but you should use a pinning handle. Create a GCHandle for
each reference, call the GCHandle Alloc member 2 parameter overload with
Pinned as the handle type. Put the handle in an array, create a second
array (of IntPTR) and put the result of calling
GCHandle::AddressOfPinnedObject into that and then you can pass that to
native code.

Note that a design that needs this probably requires rethinking since
keeping lots of pinnen objects (like an array of them) around for a long
time isn't something to do too lightly.

Ronald Laeremans
Visual C++ team


Thanks for your response. That answers the question of whether or not I can
use a __pin pointer in that way. I tried creating an array of GCHandles and
then calling GCHandle::Alloc to create the pinned handles, but it didn't
work, because I get an ArgumentException: "An instance with nonprimitive
(non-blittable) members cannot be pinned." What I'm actually trying to do is
a little more complicated, namely pin several 2D arrays of enums:

public __value enum MyEnum : unsigned char { ValueOne = 1, ValueTwo = 2};

public MyFunc()
{
Array* array __gc[] = new Array*[numObjects];
GCHandle pinners __gc[] = new GCHandle[numObjects];
vector<char*> v;
for (int i = 0; i < numObjects; i++)
{
MyEnum e __gc[,] = new MyEnum[10, 10];
array[i] = e;
pinners[i] = GCHandle::Alloc(__box(e[0, 0]), GCHandleType::Pinned);
v.push_back(static_cast<char*>(pinners[i].AddressOfPinnedObject()));
}
UnmanagedFunction(v);
// Omitted: Also call GCHandle.Free on the handles
}

I noticed also, that you cannot even create a GCHandle (pinned) on a single
enum of any kind. That seems really strange to me. Is there any way to pass
an array of 2D arrays of value-enums to unmanaged code?
Nov 17 '05 #3
Hexar Anderson wrote:
"Ronald Laeremans [MSFT]" wrote:
For this situation you cannot use pinned pointers (which are only legal
on the stack) but you should use a pinning handle. Create a GCHandle for
each reference, call the GCHandle Alloc member 2 parameter overload with
Pinned as the handle type. Put the handle in an array, create a second
array (of IntPTR) and put the result of calling
GCHandle::AddressOfPinnedObject into that and then you can pass that to
native code.

Note that a design that needs this probably requires rethinking since
keeping lots of pinnen objects (like an array of them) around for a long
time isn't something to do too lightly.

Ronald Laeremans
Visual C++ team

Thanks for your response. That answers the question of whether or not I can
use a __pin pointer in that way. I tried creating an array of GCHandles and
then calling GCHandle::Alloc to create the pinned handles, but it didn't
work, because I get an ArgumentException: "An instance with nonprimitive
(non-blittable) members cannot be pinned." What I'm actually trying to do is
a little more complicated, namely pin several 2D arrays of enums:

public __value enum MyEnum : unsigned char { ValueOne = 1, ValueTwo = 2};

public MyFunc()
{
Array* array __gc[] = new Array*[numObjects];
GCHandle pinners __gc[] = new GCHandle[numObjects];
vector<char*> v;
for (int i = 0; i < numObjects; i++)
{
MyEnum e __gc[,] = new MyEnum[10, 10];
array[i] = e;
pinners[i] = GCHandle::Alloc(__box(e[0, 0]), GCHandleType::Pinned);
v.push_back(static_cast<char*>(pinners[i].AddressOfPinnedObject()));
}
UnmanagedFunction(v);
// Omitted: Also call GCHandle.Free on the handles
}

I noticed also, that you cannot even create a GCHandle (pinned) on a single
enum of any kind. That seems really strange to me. Is there any way to pass
an array of 2D arrays of value-enums to unmanaged code?

You don't need to pin enums or other value types, they are always
allocated inline, for these it is enough that you pin the array
containing them.

I hope that is better news. :-)

Ronald
Nov 17 '05 #4

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

Similar topics

3
by: J | last post by:
I understand how to pin pointers as they are declared, but for some reason, I can'f figure out the syntax for predaclaring a pointer and then pinning in afterward like you would if you wanted to...
4
by: Sai Kit Tong | last post by:
I have to interface managed application with my legacy dll. I have employed the wrapper approach but I have to deal with the asynchronous callback from the legacy dll, which likely goes through a...
1
by: demofo | last post by:
Does the pinning pointer safe?since the Read function will change the ppBuffer points to unmanaged C++ heap,how does the dot net runtime knows they will not move the allocated memory (pointed by...
7
by: Ioannis Vranos | last post by:
Consider the code: wchar_t __pin *p= &(someCommand->ToCharArray()); _wsystem(p); p=0;
1
by: Lance Orner | last post by:
I wrote this letter to a colleague, and I thought I'd share it here: --- We had a problem with pointers being passed between managed C++ and unmanaged C++ code, but it only occurred once in...
0
by: Bjoern Jonsson via .NET 247 | last post by:
Hi all, i've just encountered a problem with pinning. it seems as if a managed object is moved altough a pinning pointer to this object is still alive code snippet: ManagedObject __pin*...
9
by: Brian Victor | last post by:
I have a situation where I have a vector<gcroot<ManagedWrapperClass*> > whose contents I need to pass to an unmanaged function. Is there a way to pin all the pointers in the vector for that...
5
by: Maxwell | last post by:
Hello, Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project where I have a managed class reference in a unmanaged class...simple enough. To keep things short I am for the most...
12
by: DaTurk | last post by:
Hi, I have a rather interesting problem. I have a unmanged c++ class which needs to communicate information to managed c++ via callbacks, with a layer of c# on top of the managed c++ ultimatley...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.