473,806 Members | 2,594 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

COM Interop problem...

Hi,

Introduction:
*************** ******
I am writing a mixed mode application I have a COM module and a .NET module
that communicate with each other.
The COM exposes a custom sink interface, the .NET module implement the Sink
interface ( IUnknown based ) and the COM call the methods of this interface
asynchronously.

The problem:
*************** ******
When calling the sink methods on the .NET module from the context of a
managed thread ( the main app thread ) created by the .NET framework
everything works fine BUT when calling the sink methods ( implemented by the
managed module ) from an unmanaged thread that was created by the COM module
I get E_NOINTERFACE.

Note that The managed sink implementation is associated with the COM module
through an Advice( ISink sink, ref uint uiContext ) method.

What may cause this problem? I called CoInitializeEx on all of the new
unmanaged threads…

Following is a code snap of the two modules:

Unmanaged COM:
############### ############### ############### ###
interface IMediaEvents
{
virtual HRESULT OnState(eState state, VARIANT &var) = 0;
};

Protected:
HRESULT Fire_OnState(IN eState state, IN VARIANT &var)
{
HRESULT hrRet = S_OK;
SinkVector_t::i terator itr = m_vecSink.begin ();
while(itr != m_vecSink.end() )
{
//////////////////////////////
// Returns E_NOINTERFACE!! !
if(FAILED((*itr )->OnState(stat e, var)))
hrRet = S_FALSE;
//////////////////////////////
itr++;
}
return hrRet;
}

Public:
STDMETHOD(Advis e)(IMediaEvents * pSink,
ULONG* pulCookie)
{
…
while(itr != m_vecSink.end() )
{
if(pSink == (*itr).p)
return HRESULT_FROM_WI N32(ERROR_ALREA DY_CONNECTED);
itr++;
}
m_vecSink.push_ back(spSink);
*pulCookie = (ULONG)spSink.p ;
return S_OK;
}

STDMETHOD(Unadv ise)(/*[in]*/ ULONG ulCookie)
{
…
}

Managed Sink
############### ###############
public class Form1 : System.Windows. Forms.Form
, StreamingServer Lib.IMediaEvent s
{
…
…
…
private void btnRecord_Click (object sender, System.EventArg s e)
{
if(null != m_RecordSession )
{
m_RecChan.Unadv ise(m_uiRecCook ie);
m_MediaBuffer.C loseSession(m_R ecordSession);
m_RecordSession = null;
}
m_RecChan = null;
m_RecChan = m_MediaBuffer.C reateChan(txtOu tputPath.Text, -1);
///////////////////////////////////////////////
// Connects with the COM object
m_uiRecCookie = m_RecChan.Advis e(this);
…
m_RecordSession .Start(txtSourc ePath.Text);
}

/////////////////////////////////////////////////////////////////
// Called by the COM only when executed from a managed thread
// when invoked by an unmanaged thread E_NOINTERFACE is returned
// to the unmanaged caller
public void OnState(Streami ngServerLib.eSt ate state,
object varParam)
{
…
}
…
…
};

Nadav
http://www.sophin.com
Nov 17 '05 #1
8 3101
Nadav wrote:
Hi,

Introduction:
*************** ******
I am writing a mixed mode application I have a COM module and a .NET
module that communicate with each other.
The COM exposes a custom sink interface, the .NET module implement
the Sink interface ( IUnknown based ) and the COM call the methods of
this interface asynchronously.

The problem:
*************** ******
When calling the sink methods on the .NET module from the context of a
managed thread ( the main app thread ) created by the .NET framework
everything works fine BUT when calling the sink methods ( implemented
by the managed module ) from an unmanaged thread that was created by
the COM module I get E_NOINTERFACE.

Note that The managed sink implementation is associated with the COM
module through an Advice( ISink sink, ref uint uiContext ) method.

What may cause this problem? I called CoInitializeEx on all of the new
unmanaged threads.


You need to marshall a pointer to the managed event consumer into your
native thread. See CoMarshalInterT hreadInterfaceI nStream and
CoGetInterfaceA ndReleaseStream in MSDN.

-cd
Nov 17 '05 #2
Hi Carl,

Thanks for your responce, I wonder... why must I marshal the inteface? this
involve sending inter-thread windows messages and this may have impact on
performance... isn't there a way to call the method directly and letting my
code to manage synchronization issues [???]
--
Nadav
http://www.sophin.com
"Carl Daniel [VC++ MVP]" wrote:
Nadav wrote:
Hi,

Introduction:
*************** ******
I am writing a mixed mode application I have a COM module and a .NET
module that communicate with each other.
The COM exposes a custom sink interface, the .NET module implement
the Sink interface ( IUnknown based ) and the COM call the methods of
this interface asynchronously.

The problem:
*************** ******
When calling the sink methods on the .NET module from the context of a
managed thread ( the main app thread ) created by the .NET framework
everything works fine BUT when calling the sink methods ( implemented
by the managed module ) from an unmanaged thread that was created by
the COM module I get E_NOINTERFACE.

Note that The managed sink implementation is associated with the COM
module through an Advice( ISink sink, ref uint uiContext ) method.

What may cause this problem? I called CoInitializeEx on all of the new
unmanaged threads.


You need to marshall a pointer to the managed event consumer into your
native thread. See CoMarshalInterT hreadInterfaceI nStream and
CoGetInterfaceA ndReleaseStream in MSDN.

-cd

Nov 17 '05 #3
Both caller and callee run on diffrerent threads and different apartments
(UI thread - STA) unmanaged thread (STA/MTA?), so you need to marshal.
One possible solution might be to create the object on a (managed) MTA
thread (if the object doesn't realy requires an STA! which I doubt) and
initialize your unmanaged threads to enter the one and only MTA. Sure, this
will require to marshal the calls to the UI thread when you need to update
the UI from the managed MTA thread.

Willy.

"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:6E******** *************** ***********@mic rosoft.com...
Hi Carl,

Thanks for your responce, I wonder... why must I marshal the inteface?
this
involve sending inter-thread windows messages and this may have impact on
performance... isn't there a way to call the method directly and letting
my
code to manage synchronization issues [???]
--
Nadav
http://www.sophin.com
"Carl Daniel [VC++ MVP]" wrote:
Nadav wrote:
> Hi,
>
> Introduction:
> *************** ******
> I am writing a mixed mode application I have a COM module and a .NET
> module that communicate with each other.
> The COM exposes a custom sink interface, the .NET module implement
> the Sink interface ( IUnknown based ) and the COM call the methods of
> this interface asynchronously.
>
> The problem:
> *************** ******
> When calling the sink methods on the .NET module from the context of a
> managed thread ( the main app thread ) created by the .NET framework
> everything works fine BUT when calling the sink methods ( implemented
> by the managed module ) from an unmanaged thread that was created by
> the COM module I get E_NOINTERFACE.
>
> Note that The managed sink implementation is associated with the COM
> module through an Advice( ISink sink, ref uint uiContext ) method.
>
> What may cause this problem? I called CoInitializeEx on all of the new
> unmanaged threads.


You need to marshall a pointer to the managed event consumer into your
native thread. See CoMarshalInterT hreadInterfaceI nStream and
CoGetInterfaceA ndReleaseStream in MSDN.

-cd

Nov 17 '05 #4
Hi Willy,

Thanks for your responce, Well, setting the main thread to run as MTA
doesn't resolve the problem, still, E_NOINTERFACE is returned whn the
unmanaged COM calls the meneged object.
I wonder must this be a marshaling problem? if so, why does E_NOINTERFACE is
returned? what interface cannot be found on the managed object?

P.S.
How can associate a certain thread with a specific apartment???

Nadav
http://www.sophin.com
"Willy Denoyette [MVP]" wrote:
Both caller and callee run on diffrerent threads and different apartments
(UI thread - STA) unmanaged thread (STA/MTA?), so you need to marshal.
One possible solution might be to create the object on a (managed) MTA
thread (if the object doesn't realy requires an STA! which I doubt) and
initialize your unmanaged threads to enter the one and only MTA. Sure, this
will require to marshal the calls to the UI thread when you need to update
the UI from the managed MTA thread.

Willy.

"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:6E******** *************** ***********@mic rosoft.com...
Hi Carl,

Thanks for your responce, I wonder... why must I marshal the inteface?
this
involve sending inter-thread windows messages and this may have impact on
performance... isn't there a way to call the method directly and letting
my
code to manage synchronization issues [???]
--
Nadav
http://www.sophin.com
"Carl Daniel [VC++ MVP]" wrote:
Nadav wrote:
> Hi,
>
> Introduction:
> *************** ******
> I am writing a mixed mode application I have a COM module and a .NET
> module that communicate with each other.
> The COM exposes a custom sink interface, the .NET module implement
> the Sink interface ( IUnknown based ) and the COM call the methods of
> this interface asynchronously.
>
> The problem:
> *************** ******
> When calling the sink methods on the .NET module from the context of a
> managed thread ( the main app thread ) created by the .NET framework
> everything works fine BUT when calling the sink methods ( implemented
> by the managed module ) from an unmanaged thread that was created by
> the COM module I get E_NOINTERFACE.
>
> Note that The managed sink implementation is associated with the COM
> module through an Advice( ISink sink, ref uint uiContext ) method.
>
> What may cause this problem? I called CoInitializeEx on all of the new
> unmanaged threads.

You need to marshall a pointer to the managed event consumer into your
native thread. See CoMarshalInterT hreadInterfaceI nStream and
CoGetInterfaceA ndReleaseStream in MSDN.

-cd


Nov 17 '05 #5

"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:A6******** *************** ***********@mic rosoft.com...
Hi Willy,

Thanks for your responce, Well, setting the main thread to run as MTA
doesn't resolve the problem, still, E_NOINTERFACE is returned whn the
unmanaged COM calls the meneged object.
I wonder must this be a marshaling problem? if so, why does E_NOINTERFACE
is
returned? what interface cannot be found on the managed object?

P.S.
How can associate a certain thread with a specific apartment???

Nadav
http://www.sophin.com


Sorry, I only replied because you were asking about the
inter-thread/apartment marshaling issues (performance).
The E_NOINTERFACE is another issue, but it's hard to tell from the snippets
you posted what goes wrong other than - there is no such interface exposed.
Willy.
Nov 17 '05 #6
Hi Carl,

I have tried to use the technique you have suggested ( usage of
CoMarshalInterT hre... ), BUT Still the problem persist...
Calling 'CoMarshalInter ThreadInterface InStream' followed by a call to
'CoGetInterface AndReleaseStrea m' returns a pointer to an interface of the
correct type, actually the pointer returned by
'CoGetInterface AndReleaseStrea m' has the exact address of the one registered
with 'CoGetInterface AndReleaseStrea m'
My guess is that the interface isn't needed to be marshaled and as such an
pointer identical to the one registered with 'CoGetInterface AndReleaseStrea m'
is returned.
If my assumption is true there should be some-thing else causing this
problem, what is it????

Any help comment or remark would be appreciated.

Nadav
http://www.sophin.com
"Carl Daniel [VC++ MVP]" wrote:
Nadav wrote:
Hi,

Introduction:
*************** ******
I am writing a mixed mode application I have a COM module and a .NET
module that communicate with each other.
The COM exposes a custom sink interface, the .NET module implement
the Sink interface ( IUnknown based ) and the COM call the methods of
this interface asynchronously.

The problem:
*************** ******
When calling the sink methods on the .NET module from the context of a
managed thread ( the main app thread ) created by the .NET framework
everything works fine BUT when calling the sink methods ( implemented
by the managed module ) from an unmanaged thread that was created by
the COM module I get E_NOINTERFACE.

Note that The managed sink implementation is associated with the COM
module through an Advice( ISink sink, ref uint uiContext ) method.

What may cause this problem? I called CoInitializeEx on all of the new
unmanaged threads.


You need to marshall a pointer to the managed event consumer into your
native thread. See CoMarshalInterT hreadInterfaceI nStream and
CoGetInterfaceA ndReleaseStream in MSDN.

-cd

Nov 17 '05 #7
Nadav wrote:
Hi Carl,

I have tried to use the technique you have suggested ( usage of
CoMarshalInterT hre... ), BUT Still the problem persist...
Calling 'CoMarshalInter ThreadInterface InStream' followed by a call to
'CoGetInterface AndReleaseStrea m' returns a pointer to an interface of
the correct type, actually the pointer returned by
'CoGetInterface AndReleaseStrea m' has the exact address of the one
registered with 'CoGetInterface AndReleaseStrea m'
My guess is that the interface isn't needed to be marshaled and as
such an pointer identical to the one registered with
'CoGetInterface AndReleaseStrea m' is returned.
If my assumption is true there should be some-thing else causing this
problem, what is it????

Any help comment or remark would be appreciated.


Just to be clear - you need to call 'CoMarshalInter ThreadInterface InStream'
in the thread that loaded your DLL, and call
'CoGetInterface AndReleaseStrea m' in the thread that you created. Is that
what you tried? If so, the problem is clearly something other than
marshalling.

How have you associated the COM identity of the outbound interface with your
..NET sink? In the code snippets you posted, I don't see any of the COM
attributes decorating the class or the method, so I'm assuming that you've
just used tlbimp to generate an interop assembly for your native COM class.

Can you consume events from your COM class from another COM-based host, such
as JScript or ASP (not ASP.NET)?

-cd
Nov 17 '05 #8
The COM standard marshaler usually uses the type libraray. E_NOINTERFACE as
a result of a marshaling operation is often the result of missing type
library registration. Make sure the type library is registered, either via
regasm /tlb yourAsssembly.d ll or via regtlb yourDllOrTlb.[dll|tlb].

Marcus Heege

"Nadav" <Na***@discussi ons.microsoft.c om> wrote in message
news:04******** *************** ***********@mic rosoft.com...
Hi,

Introduction:
*************** ******
I am writing a mixed mode application I have a COM module and a .NET
module
that communicate with each other.
The COM exposes a custom sink interface, the .NET module implement the
Sink
interface ( IUnknown based ) and the COM call the methods of this
interface
asynchronously.

The problem:
*************** ******
When calling the sink methods on the .NET module from the context of a
managed thread ( the main app thread ) created by the .NET framework
everything works fine BUT when calling the sink methods ( implemented by
the
managed module ) from an unmanaged thread that was created by the COM
module
I get E_NOINTERFACE.

Note that The managed sink implementation is associated with the COM
module
through an Advice( ISink sink, ref uint uiContext ) method.

What may cause this problem? I called CoInitializeEx on all of the new
unmanaged threads.

Following is a code snap of the two modules:

Unmanaged COM:
############### ############### ############### ###
interface IMediaEvents
{
virtual HRESULT OnState(eState state, VARIANT &var) = 0;
};

Protected:
HRESULT Fire_OnState(IN eState state, IN VARIANT &var)
{
HRESULT hrRet = S_OK;
SinkVector_t::i terator itr = m_vecSink.begin ();
while(itr != m_vecSink.end() )
{
//////////////////////////////
// Returns E_NOINTERFACE!! !
if(FAILED((*itr )->OnState(stat e, var)))
hrRet = S_FALSE;
//////////////////////////////
itr++;
}
return hrRet;
}

Public:
STDMETHOD(Advis e)(IMediaEvents * pSink,
ULONG* pulCookie)
{
.
while(itr != m_vecSink.end() )
{
if(pSink == (*itr).p)
return HRESULT_FROM_WI N32(ERROR_ALREA DY_CONNECTED);
itr++;
}
m_vecSink.push_ back(spSink);
*pulCookie = (ULONG)spSink.p ;
return S_OK;
}

STDMETHOD(Unadv ise)(/*[in]*/ ULONG ulCookie)
{
.
}

Managed Sink
############### ###############
public class Form1 : System.Windows. Forms.Form
, StreamingServer Lib.IMediaEvent s
{
.
.
.
private void btnRecord_Click (object sender, System.EventArg s e)
{
if(null != m_RecordSession )
{
m_RecChan.Unadv ise(m_uiRecCook ie);
m_MediaBuffer.C loseSession(m_R ecordSession);
m_RecordSession = null;
}
m_RecChan = null;
m_RecChan = m_MediaBuffer.C reateChan(txtOu tputPath.Text, -1);
///////////////////////////////////////////////
// Connects with the COM object
m_uiRecCookie = m_RecChan.Advis e(this);
.
m_RecordSession .Start(txtSourc ePath.Text);
}

/////////////////////////////////////////////////////////////////
// Called by the COM only when executed from a managed thread
// when invoked by an unmanaged thread E_NOINTERFACE is returned
// to the unmanaged caller
public void OnState(Streami ngServerLib.eSt ate state,
object varParam)
{
.
}
.
.
};

Nadav
http://www.sophin.com

Nov 17 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1999
by: Nadav | last post by:
Hi, Introduction *************** I have a system build of a collection of 'Native COM objects' and '.NET COM interop' objects, all of the COM objects are managed through a 'Native COM' layer, this layer manage the underlying COM Objects and upon request, provide a pointer to those objects to the 'API Consumer', following is an illustration of the system: API Consumer ( Native C++/C# ) || ******************************************* * ...
20
3201
by: Razzie | last post by:
Hey all, I'm really going through a small hell right now - I've completely lost it :) I made a project, using two interop libraries from exchange (created them as in this msdn article: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnsmtps/html/writingmngsinks.asp). I set my project properties as 'Register as COM interop' to true, I can install it my project on my developement machine without a flaw. Great.
1
2302
by: Shiro | last post by:
Hi I have read the various postings relating to Interop strong name signing and cannot find an example similar to mine. I have stringly named my AxInterops/Interops and they all work just fine, but problems occur when I need to reference a thirdparty's component. In my case I am talking about Interop.ShDocVw and AxInterop.ShDocVw. If
8
3427
by: Rob Edwards | last post by:
When trying to add the Microsoft CDO for Exchange Management Library (aka CDOEXM.dll) I receive the following message: "A reference to 'Microsoft CDO for Exchange Management Library' could not be added. Converting the type library to a .Net assembly failed. A depended type library 'CDO' could not be converted to a .NET assembly. A dependent type library 'ADODB' could not be converted to a .NET assembly. Item has already been added." ...
7
10967
by: R Reyes | last post by:
Can someone please explain to me why I can't get the MS Word Interop assembly to work in my VS2005 project? I'm trying to manipulate MS Word from my Web Form application and I can't get passed this screen below. Please help, thanks in advance... Configuration Error Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify...
3
1476
by: Hospital S.Sebastiao | last post by:
Hi, i'm in desperate need of help to fix a problem that i have, the problem is the following: I have an ASP.NET aplication that to open an word template document, this aplication is in C#(using .NET 1.1 and VS2003), i have register the office interop DLL in the GAC an have also install the office program in the machine, this is in a virtual machine that i use to make the development, on the host machine i have the same interop DLL's and ...
2
7307
by: JC | last post by:
Anybody knows what problem has this code? I think, in the Garbage Collector? You know the Solution? The program in the test's case, whit 350 contacts, run OK before number 86. The error is a "Array index out of bounds". Microsoft.Office.Interop.Outlook._Application olApp = new Microsoft.Office.Interop.Outlook.ApplicationClass(); Microsoft.Office.Interop.Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
1
4561
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a number of other C# dlls referencing Interfaces.dll and using logger.dll interop for logging.
1
2872
by: allbelonging | last post by:
C#.Net Outlook 2003 automation (programmatically) with Office.Interop.Outlook Problem: I have my outlook 2003 configured with multiple mailbox on my local machine. I want to specify the mailbox and server (Exchange server mail box) to connect and then save the mailitems(from Inbox or any other folder) based on a filter to a*.msg file. I want to achieve this using only one Interop dll if this is possible. Tried so far:
0
2060
by: Tina | last post by:
I've gotten this before where it says there is a problem with Interop.MSDASC but I can't remember what causes this. This is a 1.1 app I'm trying to debug in vs2005. It was running yesterday just fine. Help! T Server Error in '/VT.Users' Application. -------------------------------------------------------------------------------- Configuration Error
0
9719
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10372
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6877
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5682
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4329
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3851
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.