473,407 Members | 2,314 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,407 software developers and data experts.

Cannot convert parameter from cli::interior_ptr<Type>

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

Jan 12 '06 #1
2 10922
"Steve Wilkinson" <St************@discussions.microsoft.com> wrote
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
Jan 12 '06 #2
Thanks Holger, that did the trick.

"Holger Grund" wrote:
"Steve Wilkinson" <St************@discussions.microsoft.com> wrote
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

Jan 12 '06 #3

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

Similar topics

8
by: =?Utf-8?B?V2hpdG5leSBLZXc=?= | last post by:
Hi there, I'm having a bit of trouble using an HRASCONN object as a class member variable inside my managed C++ class. When I call RasDial() and pass in the address of my HRASCONN object, I get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
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,...

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.