[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

ASP 2.0 web page -> C++/ATL/COM service -> C# test app for service

Gold Panther

4/27/2007 6:44:00 PM

I posted these message under another post that wasn't named well.
Since I don't know how to change the post name, I'm starting a new
one.

The tutorial
can be found at:

http://support.microsoft.com...

It's about writing event sinks in C# to catch events from C++/COM.

The idea is that I have an ASP 2.0 page with a C# code file. I wanted
to reference a DLL, which was written in C++/COM/ATL, whose only
responsibility is to fire an event when told to do so. The firing of
the event gets all the way to the point of a for loop used to let the
subscribed event sinks know that the event has fired. The generated
for loops looks like the following:


int CConnections = m_vec.GetSize();
for(IConnections = 0; IConnections < CConnections; IConnetions++)


Upon throwing in some message boxes, I found that CConnections is
equal to zero. Here's where the event sink comes in: there's another
program running that references the same DLL and has implemented an
event sink like in the above mentioned tutorial. It initializes the
event sink and doesn't cause an exception when it advises an object of
the class inside the DLL. My take from Microsoft is that that means
it is now waiting for the event. So, now I have a program firing an
event and one sinked to an event...but the event sink isn't catching
the event being fired.


I've tried two other options. One idea I had was thatmaybe it was
instantiating different instances of the DLL. The instances being in
different process spaces may make the catching of the event impossible
since they don't know of each other. Well, I tried to go back and
make the DLL an executable service. It works exactly the same way
with the exact same problem. Ok, well, I thought to try accomplishing
this through delegation like in the second half of the above mentioned
tutorial. Now, I'm in a whirlwind of confusing documentation trying
to find out how to create an event handler for a custom event. All I
keep getting are articles on how to create event handlers for events
under the System namespace (i.e. Click, SelectedIndexChanged, etc.) I
can't figure out if I need to just inherit from IEvent or what within
the service.


Anyway, any help would be greatly appreciated. To recreate the DLL
(ATL Control) or service (ATL Simple Object), use the tutorial on ATL
Objects at the following link:
http://msdn2.microsoft.com/en-us/librar...(vs.80,d=printer).aspx

*************What I discovered later************************

The initial problem is that the service is missing a class factory.
Since adding the macro DECLARE_CLASS_FACTORY_AUTO_THREAD (which was
the only "DECLARE_CLASS_FACTORY" that mentioned out of process
servers) in ProblemEventSrc.h under
DECLARE_REGISTRY_RESOURCEID(IDR_PROBLEMEVENTSRC), the applications
now
fail within atlcom.h with an E_FAIL HRESULT. A few lines above the
failed assertion there is the comment that the service "cannot
aggregate across apartments." I thought this might be caused by the
service being set up in the wizard as "Apartment" threading which is
STA instead of "Free" threading. I changed the first inherited class
template of CProblemEventSrc from
CComObjectRootEx<CComSingleThreadModel> to
CComObjectRootEx<CComMultiThreadModel> to see if I was right. It
still gave the same error.

********************What I think now************************

If the above is not the path of the solution, then there's a chance
that it could be that the interop is not calling the right function
when I instantiate the object. It needs to call CoGetClassObject
where it might just be creating a new instance of the service. If the
two programs are referencing a different instance of the service, that
would be the problem. I would just break down and use remoting, but
there's other times that this problem will come up. I need to know
how this works. Any help would be appreciated.

1 Answer

Gold Panther

6/8/2007 6:24:00 PM

0

I had to call Microsoft to get the answer. It was what I had started
figuring it was. The class factory was not creating a singleton. I
suggest this to MS and they said that that was the problem. The C++
service should have had DECLARE_CLASSFACTORY_SINGLETON() in it. After
that I ran into another problem you might like to hear about. The
service fired the event and the ASP page could catch it by using
delegates (after I made sure an IDispatch interface was implemented),
but the C# application could not capture the event by delegates. I
had to create a custom event sink to do so. I'm not sure why this is
and Microsoft doesn't seem to want to answer that question. Check if
this is the case if you run into this problem.