Cannot convert parameter from cli::interior_ptr<Type>

G

Guest

I've just started using managed C++ with VS2005, so please forgive my
ignorance.
I'm investigating producing a managed wrapper for some functionality of the
Windows Media Format SDK.

I have the following very simple header and class files:

Header File:
// CSJ.h

#pragma once

using namespace System;

namespace CSJ {

public ref class Class1
{
private:
IWMReader* m_pReader;

public:
void Init();
};
}

Class File:
// This is the main DLL file.

#include "stdafx.h"
#include <vcclr.h>
#include <wmsdk.h>
#include <asferr.h>
#include <nserror.h>

#include "CSJ.h"

void CSJ::Class1::Init()
{
HRESULT hr;

hr = WMCreateReader(NULL, WMT_RIGHT_PLAYBACK, &m_pReader);
}

This results in a compilation error C2664: 'WMCreatereader' : cannot convert
parameter 3 from 'cli::interior_ptr<Type>' to 'IWMReader **'

If I move the declaration of m_pReader into the Init() function everything
compiles, but I need m_pReader as a class level variable. How do I correctly
declare m_pReader ?

Any help would be appreciated.

Thanks

Steve
 
H

Holger Grund

Steve Wilkinson said:
void CSJ::Class1::Init()
{
HRESULT hr;

hr = WMCreateReader(NULL, WMT_RIGHT_PLAYBACK, &m_pReader);
}

This results in a compilation error C2664: 'WMCreatereader' : cannot
convert
parameter 3 from 'cli::interior_ptr<Type>' to 'IWMReader **'
interior_ptr is a managed pointer. Managed pointers are reported to the GC.
If you want to call into unmanaged code you need to tell the execution
engine
that the pointee should not be moved and hence the pointer value doesn't
change.

To do so, you need a managed pointer with the pinned attribute to point
to the object. The corresponding C++/CLI construction is pin_ptr.

Try
pin_ptr<IWMReader*> p = &m_pReader;
WMCreateReader(....p);

pin_ptr implicitly converts to a native pointer which is valid so long as
the object is pinned (typically until p is assigned another value or goes
out of scope)
If I move the declaration of m_pReader into the Init() function everything
compiles, but I need m_pReader as a class level variable. How do I
correctly
declare m_pReader ?
That's because it resides on the stack. Items on the stack are never moved
by the GC. It is safe to assume that pointer is stable during the execution
of
the function.

-hg
 
G

Guest

Thanks Holger, that did the trick.

Holger Grund said:
interior_ptr is a managed pointer. Managed pointers are reported to the GC.
If you want to call into unmanaged code you need to tell the execution
engine
that the pointee should not be moved and hence the pointer value doesn't
change.

To do so, you need a managed pointer with the pinned attribute to point
to the object. The corresponding C++/CLI construction is pin_ptr.

Try
pin_ptr<IWMReader*> p = &m_pReader;
WMCreateReader(....p);

pin_ptr implicitly converts to a native pointer which is valid so long as
the object is pinned (typically until p is assigned another value or goes
out of scope)

That's because it resides on the stack. Items on the stack are never moved
by the GC. It is safe to assume that pointer is stable during the execution
of
the function.

-hg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top