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

using pin_ptr with a whole pointer

I could not get the following code to compile:
public ref class wrapper
{
void Read(int^ mpLen)
{
pin_ptr<int> pLen = mpLen;
unmanaged* punman = new unmanaged();
punman->Read(pLen);
return;
}
}
The error was in the conversion from a managed pointer, mpLen, to
pin_ptr<int>.

When I changed the code by wrapping the int in a managed class, it worked:
public ref class BytesRead
{
public:
int Len;
};
public ref class wrapper
{
void Read(BytesRead^ mpBR)
{
pin_ptr<int> pLen = &mpBR->Len;
unmanaged* punman = new unmanaged();
punman->Read(pLen);
return;
}
}

I interpret that difference as pin_ptr being happy with an interior pointer
but not a whole pointer. But surely there is a way to pin the whole pointer
to a managed class. What did I do wrong?

Thanks in advance,
Ray

Nov 26 '05 #1
2 3189
"T Ray Humphrey" <TR**********@discussions.microsoft.com> wrote
I could not get the following code to compile:
public ref class wrapper
{
void Read(int^ mpLen)
{
pin_ptr<int> pLen = mpLen;
unmanaged* punman = new unmanaged();
punman->Read(pLen);
return;
}
}
The error was in the conversion from a managed pointer, mpLen, to
pin_ptr<int>.
int^ is not what the CLR calls a managed pointer. It's rather the boxed form
of an int (there's no representation in C# & VB).

So first of all, it is probably not the interface you want. You'd rather
want
interior_ptr<int> or int% which both translate to managed pointers (int&
in ILASM speak or ref/out in C#)

If you really want that interface, I think you should be able to use
&*mpLen.
However, IIRC the CLI standard did not guarantee that unbox gives you
a pointer to the original object (even though I fail to understand, why that
is).

I interpret that difference as pin_ptr being happy with an interior
pointer
but not a whole pointer. But surely there is a way to pin the whole
pointer
to a managed class. What did I do wrong?

R% and R^ are the same thing for the CLR (only C++ language semantics are
different). V% and V^ are different. The first one is a managed pointer
(V& in ILASM), the second one is the boxed representation with C++
pointer semantics.
interior_ptr<V> yields the same CLR type as V%. They only differ in
source-level semantics (and only for C++/CLI).

-hg
Nov 26 '05 #2
Thanks a bunch! That fixed it.
I changed the int^ to an int%. I was still able to do the pin with:
pin_ptr<int> p = &i;
Thanks, too, for your detailed explanation. It help a lot.

"Holger Grund" wrote:
"T Ray Humphrey" <TR**********@discussions.microsoft.com> wrote
I could not get the following code to compile:
public ref class wrapper
{
void Read(int^ mpLen)
{
pin_ptr<int> pLen = mpLen;
unmanaged* punman = new unmanaged();
punman->Read(pLen);
return;
}
}
The error was in the conversion from a managed pointer, mpLen, to
pin_ptr<int>.


int^ is not what the CLR calls a managed pointer. It's rather the boxed form
of an int (there's no representation in C# & VB).

So first of all, it is probably not the interface you want. You'd rather
want
interior_ptr<int> or int% which both translate to managed pointers (int&
in ILASM speak or ref/out in C#)

If you really want that interface, I think you should be able to use
&*mpLen.
However, IIRC the CLI standard did not guarantee that unbox gives you
a pointer to the original object (even though I fail to understand, why that
is).

I interpret that difference as pin_ptr being happy with an interior
pointer
but not a whole pointer. But surely there is a way to pin the whole
pointer
to a managed class. What did I do wrong?

R% and R^ are the same thing for the CLR (only C++ language semantics are
different). V% and V^ are different. The first one is a managed pointer
(V& in ILASM), the second one is the boxed representation with C++
pointer semantics.
interior_ptr<V> yields the same CLR type as V%. They only differ in
source-level semantics (and only for C++/CLI).

-hg

Nov 26 '05 #3

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

Similar topics

0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
1
by: marco_segurini | last post by:
Hi, I like to know if is it possible to use sizeof(T) inside a generic function. I don't know why the following function compiles while if I define ON_LINE_STATEMENT it does not. ...
11
by: Tao.Young | last post by:
when i doing this: pin_ptr<A> pA = gcnew A; // assume A is a ref class i'll get many compiler errors. does anyone know how to do it correctly? Thanks in advance.
1
by: _DS | last post by:
My managed code needs to shuttle an unmanaged buffer between a couple unmanaged functions. I know this could be done via pin_ptr, and I understand the implications re managed heap fragmentation...
3
by: Nobody | last post by:
I posted this bug to MS, but was wondering if someone else could try inserting the following into a C++/cli project and seeing if it causes the compiler to barf: array<float,2>^ fMatrix = gcnew...
7
by: _iycrd | last post by:
Is there an easy way to pin a pointer that is a member of a class, and leave that pointer pinned for the duration of the class's existence? The pointer would presumably be pinned inside the class's...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
1
by: Bruce | last post by:
public ref class GpsDevice abstract { /* Please note GarXface4::GpsDevice* is in a different namespace from this one and GarXface4::GpsDevice is an unmanaged class */ virtual...
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...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.