Click here to Skip to main content
15,921,156 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralRe: Handles and pointers... Pin
-=jarl=-26-Feb-03 23:19
-=jarl=-26-Feb-03 23:19 
GeneralManaged and Unmanaged structures Pin
-=jarl=-26-Feb-03 6:55
-=jarl=-26-Feb-03 6:55 
GeneralRe: Managed and Unmanaged structures Pin
-=jarl=-26-Feb-03 6:58
-=jarl=-26-Feb-03 6:58 
GeneralRe: Managed and Unmanaged structures Pin
Paul Selormey26-Feb-03 13:51
Paul Selormey26-Feb-03 13:51 
GeneralRe: Managed and Unmanaged structures Pin
-=jarl=-26-Feb-03 23:16
-=jarl=-26-Feb-03 23:16 
GeneralRe: Managed and Unmanaged structures Pin
Paul Selormey27-Feb-03 0:20
Paul Selormey27-Feb-03 0:20 
GeneralRe: Managed and Unmanaged structures Pin
-=jarl=-27-Feb-03 0:22
-=jarl=-27-Feb-03 0:22 
GeneralManaged/Unmanaged: pinning ptr madness Pin
VizOne25-Feb-03 22:04
VizOne25-Feb-03 22:04 
Hi!

Short form:
Imagine two managed functions<code>
void Class::Function1(SomeValueType val1)
void Class::Function2(SomeValueType * val2)</code>
And an unmanaged function:
<code>void Foo(void * pThis, void * pSomeValueType);</code>

pThis shall receive the this-pointer of Class , pSomeValueClass shall
receive a pointer to the SomeValueType instance. This, of course works:
<code>
void Class::Function1(SomeValueType val1)
{
Class __pin * pinnedThis = this;
SomeValueType __pin * pinnedVal = &val1;
Foo(reinterpret_cast<void*>(pinnedThis),
reinterpret_cast<void*>(pinnedVal));
}

void Class::Function2(SomeValueType * val2)
{
Class __pin * pinnedThis = this;
SomeValueType __pin * pinnedVal = val2;
Foo(reinterpret_cast<void*>(pinnedThis),
reinterpret_cast<void*>(pinnedVal));
}</code>

However, this works as well:
<code>
void Class::Function1(SomeValueType val1)
{
Foo(reinterpret_cast<void*>(&(*this)), reinterpret_cast<void*>(&val1));
}
</code>
How can it be, that &(*this) and &val1 are implicitely are converted to
__nogc pointers that can be castet to void*?

Thanks for your help!


Long form (including history and research):
Please take a view moments to read my post.

I am quite new to MC++ and .net, and so the following questions might sound
rediculous:

I am working on a managed classlibrary that wraps a small part of Direct3D8.
I know, Managed DX is out, however that is not what I need.

The classlib calls native functions. E.g. one function (it is one from
Direct3DX) has the following signature:

D3DXMATRIX * D3DXMatrixMultiply(D3DXMATRIX * out, D3DXMATRIX * mat1,
D3DXMATRIX * mat2);

First question: what is the better method: using DllImportAttribute to
import the method, or include the appropriate header file (<D3DX8.h>) and
lib ("D3DX8.lib")? Speed is more important than size.

So, now I have a __value class Matrix, which has the same memory layout as
D3DXMATRIX.
<code>
public __value class Matrix
{
public:
float M11, M12, M13, M14;
float M21, M22, M23, M24;
float M31, M32, M33, M34;
float M41, M42, M43, M44;

public:
void Multiply(Matrix * src);
};
</code>

Matrix::Multiply shall use D3DXMatrixMultiply. So naivly I tried this:
<code>
void Matrix::Multiply(Matrix * src)
{
D3DXMatrixMultiply(this, this, src);
}
</code>
OK, stupid me, the compiler complains that it cannot convert Matrix __gc
*const to D3DXMATRIX. Seems logical.

As a good citizen I use __pin pointers:
Second try:
<code>
void Matrix::Multiply(Matrix * src)
{
Matrix __pin * pThis = this;
Matrix __pin * pSrc = src
D3DXMatrixMultiply(reinterpret_cast<D3DXMATRIX*>(pThis),
reinterpret_cast<D3DXMATRIX*>(pThis),
reinterpret_cast<D3DXMATRIX*>(pSrc));
}</code>

Et voilá, it works. Being a bit proud of me, I ran Microsoft.DirectX.dll
(from Managed DX) through ildasm.exe and found out, that *their* version of
Matrix::Multiply only uses *one* pinning pointer. Furthermore their function
has a different sig: they provide the src Parameter by value.

<code>
void Matrix::Multiply(Matrix src);
</code>

Digging a bit more into the IL, I found out that say something like that:
<code>
void Matrix::Multiply(Matrix src)
{
Matrix __pin * pThis = this;
D3DXMatrixMultiply(reinterpret_cast<D3DXMATRIX*>(pThis),
reinterpret_cast<D3DXMATRIX*>(pThis),
reinterpret_cast<D3DXMATRIX*>(&src)); // use ptr of src
}</code>

Which - to my amazement - compiles. NO pinning pointer neccessary for src?
Because it is given by value?? Strange...

Now I tried something funny:
<code>
void Matrix::Multiply(Matrix src)
{
// we will deref this just to directly get the pointer of it
D3DXMatrixMultiply(reinterpret_cast<D3DXMATRIX*>(&(*this)),
reinterpret_cast<D3DXMATRIX*>(&(*this)),
reinterpret_cast<D3DXMATRIX*>(&src));
}</code>

Looks weird, but compiles!!

Second question: it seems obvious to me that calling this function has a
large overhead because Matrix src is passed by value. Looking at the IL of
the callee shows a cpobj, so I think this is not an appropriate solution.
(However, I wonder why MS did this that way...). Is it so?

Adding one and one, I tried the following:
<code>
void Matrix::Multiply(Matrix * src) // back to byref!
{

D3DXMatrixMultiply(reinterpret_cast<D3DXMATRIX*>(&(*this)),
reinterpret_cast<D3DXMATRIX*>(&(*this)),
reinterpret_cast<D3DXMATRIX*>(&(*src)));
}</code>

This indeed does not compile. Again the compiler argues about not being
abled to convert from __gc* to __nogc*.


Thanks for your patience!

- Andre


GeneralRe: Managed/Unmanaged: pinning ptr madness Pin
Jeff J26-Feb-03 16:24
Jeff J26-Feb-03 16:24 
GeneralRe: Managed/Unmanaged: pinning ptr madness Pin
VizOne27-Feb-03 1:56
VizOne27-Feb-03 1:56 
GeneralRe: Managed/Unmanaged: pinning ptr madness Pin
VizOne28-Feb-03 0:31
VizOne28-Feb-03 0:31 
GeneralRe: Managed/Unmanaged: pinning ptr madness Pin
Jeff J28-Feb-03 22:25
Jeff J28-Feb-03 22:25 
GeneralImplementing IDisposable Pin
VizOne24-Feb-03 9:49
VizOne24-Feb-03 9:49 
GeneralRe: Implementing IDisposable Pin
Paul Selormey25-Feb-03 0:23
Paul Selormey25-Feb-03 0:23 
GeneralRe: Implementing IDisposable Pin
VizOne25-Feb-03 0:30
VizOne25-Feb-03 0:30 
GeneralRe: Implementing IDisposable Pin
Paul Selormey25-Feb-03 0:49
Paul Selormey25-Feb-03 0:49 
GeneralRe: Implementing IDisposable Pin
VizOne25-Feb-03 0:58
VizOne25-Feb-03 0:58 
GeneralRe: Implementing IDisposable Pin
Paul Selormey25-Feb-03 1:48
Paul Selormey25-Feb-03 1:48 
GeneralRe: Implementing IDisposable Pin
VizOne25-Feb-03 2:45
VizOne25-Feb-03 2:45 
GeneralRe: Implementing IDisposable Pin
Paul Selormey25-Feb-03 2:54
Paul Selormey25-Feb-03 2:54 
QuestionManaged type??? Pin
Orkun GEDiK21-Feb-03 12:49
Orkun GEDiK21-Feb-03 12:49 
AnswerRe: Managed type??? Pin
Paul Selormey23-Feb-03 1:46
Paul Selormey23-Feb-03 1:46 
GeneralRe: Managed type??? Pin
Orkun GEDiK23-Feb-03 3:55
Orkun GEDiK23-Feb-03 3:55 
GeneralRe: Managed type??? Pin
Paul Selormey23-Feb-03 23:49
Paul Selormey23-Feb-03 23:49 
GeneralRe: Managed type??? Pin
Orkun GEDiK24-Feb-03 10:48
Orkun GEDiK24-Feb-03 10:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.