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 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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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.
...
|
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.
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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: 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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |