need help with non-standard retvals in COM interop

G

Guest

i'm attempting to interop from C# to a COM DLL, and have found some trouble
with a non-standard interface (at bottom of post - followed by my C# version).

typically COM methods return an HRESULT or just void. for some reason, this
one is returning a string (char *) or in other interfaces is returning a
struct.

i'm pretty familiar with interop for normal params and using IntPtr
marshalling to structures, but i'm not sure how this works in this case.

when calling GetName or GetType in this interface (via C#), i'm just getting
'null' or '0' for each call.

thanks for any help!
Kirk

-------------


interface IAudioComponent : public IUnknown
{
virtual const char *
GetName() PURE;

// Application should first call this method to determine the type
// of the component and after that query the appropriate interface.
virtual EAudioComponentType
GetType() PURE;

// This method is called after all the components are enumerated.
// It should perform the actual initialization and store pMgr for
// later use to access functionality provided by other plugins.
// If the component initialization fails, it should return false
// and the plugin manager will remove it from components list so
// it will not be available.
virtual bool
Init(IAudioPluginMgr *pMgr, IStatus **ppStatus = NULL) PURE;

virtual bool
Done() PURE;
};


[ComImport]
[Guid("0D7EA5C4-DAF9-4738-BB57-EEFDE39CFF8C")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAudioComponent
{
StringBuilder GetName();

EAudioComponentType GetType();

bool Init(
[In, MarshalAs(UnmanagedType.Interface)] IAudioPluginMgr pMgr,
[Out, MarshalAs(UnmanagedType.Interface)] out IStatus ppStatus);

bool Done();
}
 
M

Mattias Sjögren

Kirk,

Try this:

[ComImport]
[Guid("0D7EA5C4-DAF9-4738-BB57-EEFDE39CFF8C")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IAudioComponent
{
[PreserveSig]
IntPtr GetName();

[PreserveSig]
EAudioComponentType GetType();

[PreserveSig]
[return: MarshalAs(UnmanagedType.U1)]
bool Init(IAudioPluginMgr Mgr, out IStatus ppStatus);

[PreserveSig]
[return: MarshalAs(UnmanagedType.U1)]
bool Done();
}



Mattias
 
G

Guest

Very cool, thanks!

I'm almost there... seems that GetCount works correctly and returns 2.

But when i call GetComponent to return the interface, i get an
AccessViolationException.

Any thoughts?

[ComImport]
[Guid("FC2E77B1-6278-452b-9756-7AC24E807778")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IComponentEnum
{
[PreserveSig]
[return: MarshalAs(UnmanagedType.I4)]
int GetCount();

[PreserveSig]
[return: MarshalAs(UnmanagedType.U1)]
bool GetComponent(int iNum, [Out,
MarshalAs(UnmanagedType.Interface)] out IAudioComponent ppComp);
}

here's the C# code that's calling these interop interfaces... it gets
through the first GetType() call and the GetCount() call, but fails on
GetComponent.

(the code here is directly translated from a C++ version.)

if (inputIA.GetType() !=
EAudioComponentType.ACT_ComponentEnumerator)
throw new InvalidOperationException("Primary audio object
does not support ACT_ComponentEnumerator.");

IComponentEnum inputICU = (IComponentEnum)inputIA;

if (inputICU.GetCount() == 0)
throw new InvalidOperationException("Component enumerator
contains no audio components.");

IAudioComponent inputIAC = null;


thanks for your help!
Kirk
 

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

Similar Threads


Top