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(sta tic_cast<char*> (pinners[i]));
}
UnmanagedFuncti on(v); 3 2211
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(sta tic_cast<char*> (pinners[i])); } UnmanagedFuncti on(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::Addre ssOfPinnedObjec t 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
"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::Addre ssOfPinnedObjec t 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 ArgumentExcepti on: "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::P inned);
v.push_back(sta tic_cast<char*> (pinners[i].AddressOfPinne dObject()));
}
UnmanagedFuncti on(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?
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::Add ressOfPinnedObj ect 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 ArgumentExcepti on: "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::P inned); v.push_back(sta tic_cast<char*> (pinners[i].AddressOfPinne dObject())); } UnmanagedFuncti on(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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 embed an
unmanaged object: Assumingthat pAudioU has alread been defined
as the appriate type of pointer,
Instead of
pAudioU = new __pin AudioDriver_U; // syntax error
it seems necessary to take the extra step of creating an intermediate
|
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 thread other than the initial
calling thread. I got the idea from MSDN and other articles
on using the delegate. However, for garabage collection issue, I need to pin
the delegate. Since my callback is asynchronous, I have been thinking about...
|
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 ppBuffer)during the
garbage collection?
class NonGCClass
{
private:
int GetBufferLength()
{
|
by: Ioannis Vranos |
last post by:
Consider the code:
wchar_t __pin *p= &(someCommand->ToCharArray());
_wsystem(p);
p=0;
|
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 every 2000-3000
calls. There were some String* objects in the managed code, which
were marshalled into IntPtr* objects. This was cast into a const char
__pin* and passed to some unmanaged C++ code. When the problem would
occur, the breakpoint...
| |
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* pManagedObject = &this->managedObject;
/*lots of unmanaged memory allocation*/
|
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 function? I tried to
copy the vector into another vector with a pinned pointer type, but the
compiler will not allow __pin to be used in a template parameter.
Thanks!
--
Brian
|
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 part attempting to do
what is this article by Nish:
http://www.voidnish.com/articles/ShowArticle.aspx?code=cbwijw
I have to hook up a unmanaged callback to a managed method using IJW
NOT P\Invoke. So I am employing this "Thunk" or "Bridge" class...
|
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 retreiving the
data. Presently all of the c++ code is still in .NET 1.1, so we're
using a _nogc bridge class wrapped in a _gc c++ class in order to
facilitate this interop.
But we've converted everything not c++ to .NET 2.0 and would love to
|
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: 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: 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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |