Pointer to interface?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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).
 
Mark Raishbrook said:
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.
 
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.
 
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? :-)
 
Mattias Sjögren said:
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?
 
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
 
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!
 
Back
Top