Home All Groups Group Topic Archive Search About

Pointer to interface?

Author
7 Nov 2006 8:22 PM
Mark Raishbrook
I've added a library to the project I'm currently working on which contains
the following function:

ConfigureFilterUsingProfile(ByVal pProfile As System.IntPtr) As Integer

pProfile is a pointer to the Windows Media Encoder interface IWMEncProfile.

If I create a profile -- Dim EncProfile As IWMEncProfile -- and try and pass
this to the function  -- ASFConfig.ConfigureFilterUsingProfile(EncProfile) --
I (obviously) get the following error message:

Value of type 'WMEncoderLib.IWMEncProfile' cannot be converted to
'System.IntPtr'.

How can I get a pointer to my EncProfile object? I'd be much obliged to
anyone who can point me in the right direction (lame joke, but what the heck).

Author
7 Nov 2006 10:20 PM
Ben Voigt
Show quote
"Mark Raishbrook" <MarkRaishbr***@discussions.microsoft.com> wrote in
message news:17A1E25F-83E1-443D-8F04-3CD471A97B9A@microsoft.com...
> I've added a library to the project I'm currently working on which
> contains
> the following function:
>
> ConfigureFilterUsingProfile(ByVal pProfile As System.IntPtr) As Integer
>
> pProfile is a pointer to the Windows Media Encoder interface
> IWMEncProfile.
>
> If I create a profile -- Dim EncProfile As IWMEncProfile -- and try and
> pass
> this to the function  -- 
> ASFConfig.ConfigureFilterUsingProfile(EncProfile) --
> I (obviously) get the following error message:
>
> Value of type 'WMEncoderLib.IWMEncProfile' cannot be converted to
> 'System.IntPtr'.

did you try ASFConfig.ConfigureFilterUsingProfile(new IntPtr(EncProfile))?

Also, perhaps you just need to change the DllImport statement for
ConfigureFilterUsingProfile to have a IWMEncProfile parameter instead of
IntPtr.

Show quote
>
> How can I get a pointer to my EncProfile object? I'd be much obliged to
> anyone who can point me in the right direction (lame joke, but what the
> heck).
Author
7 Nov 2006 10:32 PM
Mark Raishbrook
Thanks for the reply, Ben.

> did you try ASFConfig.ConfigureFilterUsingProfile(new IntPtr(EncProfile))?

No, I haven't tried that yet. Will do. Thanks for the tip.

> Also, perhaps you just need to change the DllImport statement for
> ConfigureFilterUsingProfile to have a IWMEncProfile parameter instead of
> IntPtr.

It's not actually a DLL import. It's a COM assembly added to the project
which exposes this function, so I can't change it I'm afraid.
Author
7 Nov 2006 10:39 PM
Mark Raishbrook
> did you try ASFConfig.ConfigureFilterUsingProfile(new IntPtr(EncProfile))?

"Overload resolution failed... Value of type 'WMEncoderLib.IWMEncProfile'
cannot be converted to 'Long/Integer".

Where's Hard Core Visual Basic when you need it? :-)
Author
8 Nov 2006 5:54 AM
Mattias Sjögren
Mark,

>How can I get a pointer to my EncProfile object?

Use Marshal.GetComInterfaceForObject (and Marshal.Release when you're
done).


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
8 Nov 2006 4:01 PM
Mark Raishbrook
"Mattias Sjögren" wrote:
> >How can I get a pointer to my EncProfile object?
>
> Use Marshal.GetComInterfaceForObject (and Marshal.Release when you're
> done).

Thanks for the tip, Mattias.

I'm now getting an "Attempted to read or write protected memory" error when
using the pointer returned by Marshal.GetComInterfaceForObject.  Silly
Question of the Week: do I have to store it in a separate variable and call
Marshal.Release immediately before using it?
Author
8 Nov 2006 7:30 PM
Mattias Sjögren
Mark,

>Question of the Week: do I have to store it in a separate variable and call
>Marshal.Release immediately before using it?

Yes you have to store it, but you shouldn't call Release until after
you're done using it (i.e. after the call to
ConfigureFilterUsingProfile).


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
8 Nov 2006 10:39 PM
Mark Raishbrook
> Yes you have to store it, but you shouldn't call Release until after
> you're done using it (i.e. after the call to
> ConfigureFilterUsingProfile).

Still no luck, I'm afraid, and I've tried several ways. Does the following
code snippet shout "This wrong!" at you?

'* Add the ASF writer. Must be configured before connecting upstream
ASFWriter = DirectCast(New WMAsfWriter(), IBaseFilter)
hr = GraphBuilder.AddFilter(ASFWriter, "ASF Writer")

'* Load the custom WME profile
EncProfile = New WMEncProfile2
EncProfile.LoadFromFile("C:\Program Files\Windows Media
Components\Encoder\Profiles\UserProfile.prx")

'* Get a pointer to it
Dim ppProfile As IntPtr = Marshal.GetComInterfaceForObject(EncProfile,
GetType(WMEncProfile2))

'* Configure writer using custom profile
ASFConfig = DirectCast(ASFWriter, IConfigAsfWriter)
ASFConfig.ConfigureFilterUsingProfile(ppProfile)

'* Release interface pointer ref
Marshal.Release(ppProfile)

The "Attempted to read or write protected memory" error occurs at the
ConfigureFilterUsingProfile call (although the HRESULT is 0).

Any ideas? I've wasted enough of your time as it is, so please don't worry
if nothing appears to be wrong!

AddThis Social Bookmark Button