Forum guidelines
AuthorQuestion
DirectSound Com Interface Import
Posted on: 08 Jul 2012
I am searching since weeks for a solution for my problem but i can t find anything. So I thought this is probably the biggest C#-Forum... I may ask here. The Question is quite difficult to explain so I just copy a link to stackoverflow where i asked before:

http://stackoverflow.com/questions/11382341/translate-c-code-com-interface-to-c-sharp

Again It would be sooooooo nice if someone could help me. I am really such anoyed by that interface but i really want to get i working :)


AuthorReply
Re: DirectSound Com Interface Import
Posted on: 08 Jul 2012  
I'd see if it makes any difference if you define the IUnknown interface as follows:

[ComImport]
[Guid("00000000-0000-0000-C000-000000000046")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IUnknown
{
    void QueryInterface([In] ref Guid giid,
    [Out, MarshalAs(UnmanagedType.IUnknown, IidParameterIndex = 0)] out object ppvObject);
 
    [PreserveSig]
    uint AddRef();
 
    [PreserveSig]
    uint Release();
}

Re: DirectSound Com Interface Import
Posted on: 08 Jul 2012  
Ok i ll try it but regardless of whether it works thanks a lot because you are the first one that tells me gives me a concret answer.

EDIT: I can play now the primarybuffer but I still get an InvalidCastException when I try to call SetVolume because QueryInterface is not supported on the COM-Component. (E_NOINTERFACE)

Re: DirectSound Com Interface Import
Posted on: 08 Jul 2012  
If the error message is saying that IDirectSoundBuffer doesn't implement QueryInterface, then that suggests that normal C# interface inheritance isn't working here.

Rather than inheriting from IUnknown, I'd try embedding its three methods within the body of the IDirectSoundBuffer interface itself.

Re: DirectSound Com Interface Import
Posted on: 10 Jul 2012  
That is exactly what I ve tried first. Same result :(
EDIT: OK no its not same result. I get HRESULT 0x8878001E at primaryBuffer.Play... What means that CTRL is not supported but i don t have to control volume and so on on primarybuffer... really confusing.

Re: DirectSound Com Interface Import
Posted on: 10 Jul 2012  
I just did a quick google on that HRESULT and it seems to come up a lot with DirectSound:

https://www.google.co.uk/search?sugexp=chrome,mod=5&sourceid=chrome&ie=UTF-8&q=HRESULT+0x8878001E 

My guess is that it's not a problem with your translation of the COM interface but a problem with something else such as the sound-card.

Re: DirectSound Com Interface Import
Posted on: 12 Jul 2012  
hmm but other dx wrappers are working Oo
Re: DirectSound Com Interface Import
Posted on: 12 Jul 2012  
Even so, if you have access to another machine, it would be worth trying the code on there to see whether the problem is hardware or OS related.
Re: DirectSound Com Interface Import
Posted on: 13 Jul 2012  
hmmm i try it but i would say it won t work on other laptop.... But another idea. I just have implemented the IDirectSound interface. There is also a IDirectSound8 Interface. I may have to use that because other wrappers are using that. And so the next question. Is it possible that I can do something like that: IDirectSound8 : IDirectSound?

EDIT
OK I just tried it on another Computer. Same Result. With the IUnknown Interface it works best. sound plays on volume change --> crash.


Re: DirectSound Com Interface Import
Posted on: 13 Jul 2012  
There's a list of the IDirectSound8 interfaces here:

http://msdn.microsoft.com/en-us/library/windows/desktop/ee416963(v=vs.85).aspx 

There are just three new ones : IDirectSound8, IDirectSoundBuffer8, and IDirectSoundCaptureBuffer8 and the rest have just been renamed.

I don't think that the new ones inherit, as such,  from the corresponding IDirectSound interfaces, though they do have some members in common.

IUnknown is still there, of course :)

Re: DirectSound Com Interface Import
Posted on: 13 Jul 2012  
I know that it is still there but something has to be wrong.
So I am searching and searching and searching. But I can find anything.

Another question: The Interface itself seems to be right. I mean all that marshalling stuff and so on...

Re: DirectSound Com Interface Import
Posted on: 14 Jul 2012  
OMG I found a solution. But its not a real solution.
It works on MTAThread config but not on STAThread. Does anyone know why?

Re: DirectSound Com Interface Import
Posted on: 14 Jul 2012  
It looks like DirectSound uses COM's multi-threaded apartment model (MTA) rather than the single-threaded model (STA).
Re: DirectSound Com Interface Import
Posted on: 15 Jul 2012  
Yeah thats exactly what I've found out. But I am writing a Libary and so the Libary has to be supported by MTA and STA Thread. Is there a possibility to solve this problem?

It doesn t seems like to be very common problem because nobody really knows a solution for this problem :(

Re: DirectSound Com Interface Import
Posted on: 15 Jul 2012  
I don't think it will be necessarily be a problem if you check the apartment state of whatever thread is calling into the dll first using Thread.CurrentThead.GetApartmentState().

If it's STA and your COM object requires STA or if it's MTA and the COM object requires MTA, then you can go ahead and create your COM object on the calling theead.

However, if it's STA and the COM object requires MTA or vice versa, then the only safe thing to do would be to create a new thread, use Thead.SetApartmentState() to set it to whatever the COM object needs, start the thread and then create the COM object on it.

If the apartment state is reported as 'Unknown', then that's effectively MTA.

Although you can change the apartment state of an existing thread in certain circumstances, that's probably not a good idea as you don't know what else the user may be doing on that thread.


Re: DirectSound Com Interface Import
Posted on: 15 Jul 2012  
hmmm I defenently can t change the state of the current thread because if its a gui thread it there will be an exception.

But creating a new thread every time I call SetVolume is not that good solution because for example if the user creates a scrollbar that sets the volume. I would have to create new Threads thaaat often.

Re: DirectSound Com Interface Import
Posted on: 15 Jul 2012  
That's right, if the user is calling the dll from the main UI thread of a Windows Form or WPF application, then the apartment state will almost certainly be STA and you won't be able to change it to MTA without getting an exception. However, if the user is calling from any other thread, then you may be able to change it though I don't think this is a good idea for the reasons stated in my previous post.

Whilst starting a new thread may pose some problems of its own, I don't see what other option is open to you. However, you should be able to use the threadpool rather than creating your own thread manually if MTA is required, because that's what the threadpool uses and (to my knowledge) it can't be changed.

Re: DirectSound Com Interface Import
Posted on: 15 Jul 2012  
Hmmm but I wonder how other DirectX Wrappers are able to solve that problem.

And by the way whats about creating a background thread that works with something like a queue that contains Actions the thread has to call. And every time the thread gets a new action into to queue it gets pushed?

Re: DirectSound Com Interface Import
Posted on: 15 Jul 2012  
If those wrappers are designed to be used from WF/WPF applications, then my guess is that they use threadpool threads. Telling the user to change the attribute on the Main() method from STA to MTA wouldn't be a good idea as a lot of stuff won't then work.

Unless those Actions themselves create threads, then you wouldn't be able to mix Actions which require STA or MTA as the background thread itself would have to be one or the other.

I suppose you could have two background threads - one for Actions requiring STA and the other for Actions requiring MTA. 



Re: DirectSound Com Interface Import
Posted on: 15 Jul 2012  
Hmm ok I've got it working now :)
It is not that way I would like to have it but it works. I've created a MTABackgroundthread.

Thanks a lot for your help :)

Employers - Post Free Jobs

SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.