CoCreateInstance and P/Invoke

I

igd

Hello!

I would like to code a wrapper for the C/C++-ASIO API. In one of the
functions exported by an unmanaged ASIO driver DLL (loadAsioDriver)
CoCreateInstance is called. When I call loadAsioDriver() from an
unmanaged client, it works but when I call that function from managed
code (P/Invoke) CoCreateInstance in loadAsioDriver() returns
E_NOINTERFACE. How can I fix this?

Thanks in advance!
 
W

Willy Denoyette [MVP]

igd said:
Hello!

I would like to code a wrapper for the C/C++-ASIO API. In one of the
functions exported by an unmanaged ASIO driver DLL (loadAsioDriver)
CoCreateInstance is called. When I call loadAsioDriver() from an
unmanaged client, it works but when I call that function from managed
code (P/Invoke) CoCreateInstance in loadAsioDriver() returns
E_NOINTERFACE. How can I fix this?

Thanks in advance!

This is not a C# issue, even not a .NET issue. What happens if you call this function from a
native client (say C++)?

Willy.
 
I

igd

As I wrote in my last post ("When I call loadAsioDriver() from an
unmanaged client, it works but when I call that function from managed
code (P/Invoke) CoCreateInstance in loadAsioDriver() returns
E_NOINTERFACE") it works. The >same< DLL called from native c++ works
how it should be. That's why I think it is a .NET issue. Any idea?
 
W

Willy Denoyette [MVP]

igd said:
As I wrote in my last post ("When I call loadAsioDriver() from an
unmanaged client, it works but when I call that function from managed
code (P/Invoke) CoCreateInstance in loadAsioDriver() returns
E_NOINTERFACE") it works. The >same< DLL called from native c++ works
how it should be. That's why I think it is a .NET issue. Any idea?

True, but It's not your C# code who calls CoCreateInstance which means it's not a .NET
issue. It's quite impossible to help you with this without seeing any code, especially what
arguments are you passing to the loadAsioDriver() function?

Willy.
 
W

Willy Denoyette [MVP]

igd said:
I have found out how it works. The Main()-Method must be decorated with
[STAThread()]. :)

That means that the native code does not initialize COM (call CoInitialize()), but instead
relies on the caller to have done this before calling into the unmanaged dll, this is not
the correct behavior the native caller is the COM client, so he should initialize COM.
This aside, did you check the COM apartment requirements of the COM server? are you sure it
needs a STA to run in? Maybe it's a 'free' or 'both' threaded COM server which means you
better apply the [MTAThread] attribute.


Willy.
 

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

Top