|
Wow your answer is just greate, and thanx for takeing so much time to anwer my query.
I dont thing there will be any problem with this solution.
Today i am not working so will let you know about the result tomorrow.
Regards,
Prakash.
Tech.Support : Mam, is your pc running under windows?
Customer : No actually its close to the main door.
|
|
|
|
|
Works like a charm.
thank you.
Tech.Support : Mam, is your pc running under windows?
Customer : No actually its close to the main door.
|
|
|
|
|
Hello Prakash,
Congratulations, Prakash . Please note that not all COM Servers and Clients will face this issue. It all depends on the threading model of the COM Server.
Some COM Servers are designed and developed using the Free-Threaded-Model. In this model, the COM Server itself will take care of thread synchronisation issues and will ensure well-synchronised access to its internal data.
The Apartment-Threaded-Model is Microsoft's default way of helping COM Server developers overcome threading issues.
Take Care and Best Wishes, Prakash,
Bio.
|
|
|
|
|
Thanx, i will keep that in mind and atleast for now i am very much satisfied with STA model.
Thanx Again.
Toughest Steel Comes From Hottest Furnance
|
|
|
|
|
Hello!
I browsed this topic and i thought you maybe the one to help me with a COM problem..
I'm making an ATL DLL from VC6.. supporting Connection Points.. and its client is a VB Program..
Now i have a function from a third party SDK...
<br />
void OnFunctionEvent()<br />
{<br />
Fire_AtlEvent()<br />
}<br />
Now, this code fragment generates an exception so the program stops here...
Now this maybe related to what you have explained in the previous messages..
Can you help why is this generating an exception, and how could i fix this?
tnx in advance
|
|
|
|
|
Hello Maverick,
Thanks very much for the question. I did some background research before writing this reply and I do anticipate more correspondence between us.
First, Maverick, some questions to help me understand your 3rd party SDK :
1. Your description for your OnFunctionEvent() function is :
//this is a derived function (pure virtual function)
//according to the documentation of the SDK,
//derived functions run on a different context from the application
//so operations inside the function must be made thread safe
This sounds to me like OnFunctionEvent() is a method that is part of a CALLBACK interface.
2. My guess is that you have to implement a class that implements the methods of this CALLBACK interface so that your 3rd party SDK can notify you of certain events.
3. If this is the case, their documentation makes sense as the callback methods (e.g. OnFunctionEvent()) can be invoked anytime and within the context of some thread not created by your application.
Please let me know if the above analysis is correct, Maverick. We'll proceed again after that.
Thanks, Maverick,
Bio.
|
|
|
|
|
Bio,
Hi,
Well you got all things right... The OnFunctionEvent is CallBack function... Actually its from a Network API, this function gets called when a maching requests a connection from the program...
2. Its not a CALLBACK interface per se, but the API has a Base Class with a mix of functions and callbacks... it just happened that the base class has a pure-virtual function OnFunctionEvent() so i have to SubClass from that base class so i can implement what to do when That Function is called...
3. Well actually the function is really called "OnRequestConnection()", it gets invoked everytime there is a request for connection... And yea, i think it creates a thread in the background not created by me...
hope to hear from you soon.. and help me soon.. pls
|
|
|
|
|
Hello Maverick,
Thanks for the reply, Maverick, and the confirmations. I have two more question to ask of you in order to confirm some other important points :
1. Can I assume that your COM Object is dual interfaced ?
2. Can I also assume that the event interface of your COM Object is a pure dispinterface ?
The above questions (esp question 2) are quite important to establish that you are using OLE-Automation-Compatible types. If this is confirmed, the solution to your problem will be much simplified.
I look forward to hearing from you again.
Thanks, Maverick,
Bio.
|
|
|
|
|
Hello Maverick,
I understand that you may be currently short of time, so I will assume that the answers to my two questions posed earlier are "YES" each (which is the typical case for ATL COM Objects which are used by VB Clients).
Now, I will further assume that you have also defined a C++ class derived from the Base Class (which contains the OnRequestConnection() virtual function) supplied by the 3rd Party Network SDK.
I certainly hope that this C++ class is NOT the same class as your ATL COM Object. Otherwise, things will get a little more complicated
Let's say the name of this class is "MyNetworkClass" and that you have instantiated this class with an object named "networkClass". I also assume that MyNetworkClass internally holds a pointer to your ATL COM Object.
Based on this scenario, my analysis of the problem is : interface marshalling. Let's explore this carefully :
1. We know that when the Network SDK calls MyNetworkClass::OnRequestConnection(), you are most likely in a thread different from any threads created by your application. The important thing is that this thread is not the thread in which your COM object is created.
2. The thread in which a COM Object is created is known as the owning thread. Your COM Object is created by your VB client and so the owning thread is most likely the VB client's main (GUI) thread.
3. Note that the interfaces of an Apartment-Threaded-Model COM Object cannot be used directly in non-owning threads (i.e. in threads other than the one which created the COM Object).
4. You can, however, use a pointer to your C++ class that implements the COM Object in any thread (as long as you can ensure thread safety). This pointer can be used to call any public functions of your C++ class. This is pure C++ programming and is not related to COM (this fact will be useful to us as you will see later on).
5. In order to use an interface of a COM Object in a non-owning thread, the interface must be marshalled to the target thread.
6. A twist to the story is that not only is your COM Object involved, but the COM Object behind the events interface is also involved. Note well that the COM Object that implements the events interface is NOT your COM Object but an object created by the VB Program !
7. Yes, that's right. When your COM Object fires an event to VB, you are actually calling a function living right inside VB code and this VB code is actually a method of a dispinterface (IDispatch Interface).
8. Your COM Object obtains a pointer to this dispinterface right at the beginning of the VB client code when your COM Object is created. At that time, alot of handshaking activities take place and one of these is the passing of a dispinterface pointer from the VB client to your COM Object.
9. This dispinterface pointer, like all other COM Interface pointers, cannot be used directly in non-owning threads. The owning thread of this dispinterface is most likely the main (GUI) thread of the VB client.
10. As of right now, we have the following situation :
10.1 When MyNetworkClass::OnRequestConnection() is called, you are in a non-owning thread.
10.2 You cannot use both your COM Object's interfaces (not too bad a problem) and the VB Event Functions' dispinterface (a bad problem).
10.3 The fact that your COM Object's interfaces cannot be used directly within the thread is not too bad a problem. In fact we do not need to use it.
10.4 The VB Event Functions dispinterface, however, -must- be marshalled to the currently executing thread (that called MyNetworkClass::OnRequestConnection()) in order to use it.
My reply is getting a little too long at this time, Maverick. I'll post another reply with the solution to the problem.
See You Later,
Bio.
|
|
|
|
|
Hello Maverick,
We'll continue with part two of our discussion :
11. How do we resolve the problems ? In short, we must marshall VB's Event dispinterface so that when MyNetworkClass::OnRequestConnection() is called, the dispinterface is ready to be used. The following is a summary of how we can achieve this :
11.1 In the MyNetworkClass C++ class, create 2 STL vectors :
typedef vector<LPSTREAM> ISTREAM_VECTOR;
typedef vector<LPDISPATCH> IDISPATCH_VECTOR;
ISTREAM_VECTOR m_vectorStream;
IDISPATCH_VECTOR m_vectorDispatch;
The first vector is used to hold IStream pointers. When COM marshalls interface pointers, it uses IStreams to temporarily hold interface data. The same IStreams are used to unmarshall interface pointers in destination threads.
The second vector is used to hold IDispatch pointers. Each IDispatch pointer points to a dispinterface of a VB Event Object.
11.2 In the MyNetworkClass C++ class, instead of internally holding a pointer to an interface of your ATL COM Object, hold a pointer to the C++ class that implements the ATL COM Object, e.g. :
instead of :
IMyCOMObjectInterfacePtr m_spIMyCOMObjectInterface;
or
IMyCOMObjectInterface* m_pIMyCOMObjectInterface;
use :
CMyCOMObject* m_pCMyCOMObject;
11.3 In the MyNetworkClass C++ class, create a function like the following :
long MyNetworkClass::MarshalEventDispatchInterfaces ()
{
int nConnections = (m_pCMyCOMObject -> m_vec).GetSize();
int nConnectionIndex = 0;
HRESULT hrTemp = S_OK;
long lRet = 0;
for (nConnectionIndex = 0; nConnectionIndex < nConnections; nConnectionIndex++)
{
IStream* pIStreamTemp = NULL;
m_pCMyCOMObject -> Lock();
CComPtr<IUnknown> sp = (m_pCMyCOMObject -> m_vec).GetAt(nConnectionIndex);
m_pCMyCOMObject -> Unlock();
IDispatch* pIDispatchTemp = reinterpret_cast<IDispatch*>(sp.p);
if (pIDispatchTemp)
{
// Create stream for marshaling IDispatch to search thread.
hrTemp = ::CoMarshalInterThreadInterfaceInStream
(
IID_IDispatch, // interface ID to marshal
pIDispatchTemp, // ptr to interface to marshal
&pIStreamTemp // output variable
);
// Place stream in member variable where COM thread will look for it.
// No need to call Release() on pStream. They will be Release()'d
// when we later call CoGetInterfaceAndReleaseStream().
if (pIStreamTemp)
{
m_vectorStream.push_back(pIStreamTemp);
}
// Also no need to call pIDispatchTemp->Release(). pIDispatchTemp is a temporary pointer
// to the IUnknown pointer in sp. And sp will automatically call Release() on its
// internal IUnknown pointer.
}
}
return lRet;
}
This function iterates through the connection points contained in the connection points vector (m_vec) of the COM Object. Note that a COM Object can be connected to more than one connection point.
Each connection point is an implementation of a specific event set. If your COM Object only implements one event set, then each connection point is of the same dispinterface.
We get each connection point, cast it into an IDispatch pointer and then call the Win32 API CoMarshalInterThreadInterfaceInStream() on it. The CoMarshalInterThreadInterfaceInStream() function will take the IDispatch pointer and create an IStream object that stores data pertaining the the IDispatch interface pointer. We then store the output IStream pointer into our stream vector m_vectorStream.
Call this function early, e.g. when your COM Object is first created or before you call the Network SDK function that start the thread. The idea is to prepare the dispinterfaces for marshalling into the Network SDK thread.
11.4 In the MyNetworkClass C++ class, create another function like the following :
long MyNetworkClass::UnMarshallEventDispatchInterfaces ()
{
ISTREAM_VECTOR::iterator theIterator;
int iIndex = 0;
HRESULT hrTemp = S_OK;
long lRet = 0;
// Unmarshal interface pointers
for (theIterator = m_vectorStream.begin(); theIterator != m_vectorStream.end(); theIterator++)
{
IDispatch* pIDispatchTemp = NULL;
IStream* pIStreamTemp = NULL;
// Get stream pointer.
pIStreamTemp = (*theIterator);
if (pIStreamTemp)
{
// Use stream pointer to create IDispatch pointer that we can call from this thread.
hrTemp = ::CoGetInterfaceAndReleaseStream
(
pIStreamTemp, // stream containing marshaling info
IID_IDispatch, // interface desired
(void**)&pIDispatchTemp // output variable
);
// Note that at this time, pIStreamTemp will be Release()'d and will no longer
// be valid.
// Put resulting IDispatch pointer in IDispatch pointers vector.
if (pIDispatchTemp)
{
m_vectorDispatch.push_back(pIDispatchTemp);
pIDispatchTemp -> AddRef(); // Since we have added pIDispatchTemp into a collection, we have an additional reference to it.
}
}
}
return lRet;
}
This function iterates through each IStream pointer stored in MyNetworkClass::m_vectorStream and uses it in a call to the Win32 API CoGetInterfaceAndReleaseStream().
This call will return to us a pointer to our IDispatch pointer from the IStream object.
After obtaining this IDispatch pointer, we will store it into our IDispatch vector m_vectorDispatch.
Call this function in the MyNetworkClass::OnRequestConnection() function. But call it only once so that the IDispatch vector is filled up only one time. After calling this function, we can invoke the specific event of your event set (by calling the IDispatch::Invoke() method).
11.5 We invoke the specific event by iterating through the IDispatch pointers contained in the IDispatch vector :
HRESULT FireEvent()
{
IDISPATCH_VECTOR::iterator theIterator;
int iIndex = 0;
CComVariant varResult;
CComVariant* pvars = new CComVariant[1];
HRESULT hrRet = S_OK;
for (theIterator = m_vectorDispatch.begin(); theIterator != m_vectorDispatch.end(); theIterator++)
{
IDispatch* pIDispatch = (*theIterator);
if (pIDispatch)
{
VariantClear(&varResult);
pvars[0] = 100;
DISPPARAMS disp = { pvars, NULL, 1, 0 };
pIDispatch->Invoke(0x1, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &disp, &varResult, NULL, NULL);
}
}
delete[] pvars;
hrRet = varResult.scode;
return hrRet;
}
The above function is an example of invoking an event that takes a long parameter (value 100).
Modify the above examples to suit your particular case, Maverick. Contact me again if you need further clarifications. Please note that my examples are for illustration purposes. I have emphasised on principles and have not paid attention to details like syntax errors or memory considerations.
You will, of course need to do things like clearing the vectors and ensuring that the functions MarshalEventDispatchInterfaces() and UnMarshallEventDispatchInterfaces() are called appropriately at the right time and place.
Best of luck, Maverick,
Bio.
|
|
|
|
|
Hello!!!
Thank you very much for the explanation.. I did have an additional info
about COM...
Well for your information, what i did was I made a COM Object using ATL in VC++, and i think it already inhertis an IDispInterFace Interface...
<br />
class ATL_NO_VTABLE CCsSdp : <br />
public CComObjectRootEx<CComSingleThreadModel>,<br />
public CComCoClass<CCsSdp, &CLSID_CsSdp>,<br />
public ISupportErrorInfo,<br />
public IConnectionPointContainerImpl<CCsSdp>,<br />
public IDispatchImpl<ICsSdp, &IID_ICsSdp, &LIBID_BTRFCOMMLib>,<br />
public CProxy_ICsSdpEvents< CCsSdp >,<br />
public CNetwork
{<br />
....<br />
...<br />
...<br />
DECLARE_REGISTRY_RESOURCEID(IDR_CSSDP)<br />
<br />
DECLARE_PROTECT_FINAL_CONSTRUCT()<br />
<br />
BEGIN_COM_MAP(CCsSdp)<br />
COM_INTERFACE_ENTRY(ICsSdp)<br />
COM_INTERFACE_ENTRY(IDispatch)<br />
COM_INTERFACE_ENTRY(ISupportErrorInfo)<br />
COM_INTERFACE_ENTRY(IConnectionPointContainer)<br />
COM_INTERFACE_ENTRY_IMPL(IConnectionPointContainer)<br />
END_COM_MAP()<br />
...<br />
...<br />
Is this ok?
i just added the Network Class to its base class of the Com Object.. Is this a nice idea? the multiple inheritance issue here? or do i have to make the Network Class/Object a member variable of the Com Class?
|
|
|
|
|
Hello Maverick,
I think adding CNetwork to the CCsSdp class is fine. I also think that it would be a better idea to send you a working example which contains concepts and constructs you mentioned (e.g. the base class with the callback function).
I'll try to come up with a good, simple and consise example and email it to you. Please give me some time.
Thanks, Maverick.
Bio.
|
|
|
|
|
Well thanks... that would be good...
First, what header files do i have to include so that i can include the definitions for
the vector.. I already included "include <vector>" but im still getting compiler errors and it points to this line:
typedef vector<lpstream> ISTREAM_VECTOR;
the error message is:
error C2143: syntax error : missing ';' before '<'
But actually im getting the idea...
i am incorporating your code MarshallInterface() and UnmarshallInterface() now...
so i hope that by incorporating your code, my program will work...
Now with regards to the Fire_Event() code you made, i think ATL has automatically done it for me so.. i don't have to deal with it..
And what about clearing the vectors? i can clear them on the destructor code ryt?...
And about calling the functions at the ryt time, i just have to follow your instructions on when to call because you said it on your explanation where to call it ryt?
One more thing, can you suggest a good book regarding COM? what r u reading ryt now?
thank you very much!!!
|
|
|
|
|
Hello Maverick,
To use the STL vector class, #include <vector> and declare the namespace you will be using, e.g. :
#include <vector>
using namespace std;
>> Now with regards to the Fire_Event() code you made, i think ATL has automatically done it for me so.. i don't have to deal with it..
A strong word of caution here, Maverick. You must not use the same wizard-generated code for the event firing function. The wizard-generated code will use the original non-marshalled IDispatch pointers contained in your COM Object. --This is the cause of the crash.--
You must use the marshalled IDispatch interfaces. I think an example program will best illustrate the importance of marshalling. I'll also put in some code to illustrate clearing of vectors. Just give me a few days' time.
Best Regards,
Bio.
|
|
|
|
|
Hi!!
I've been reading your explanations thoroughly... and after reading your explanations, i tried a different search keywords to search for a solution.. So I tried the keywords "interface marshalling".. and guess what? i found a solution in microsft knowledge base.... They made a custom class for CProxy_EVENT class... so basically they supplied a header file.. and then i used that Custom class in place of CProxy_EVENT class generated by ATL Wizard.. and that's it, my code is working now... I forgot to Knowledge Base number but i will post it here soon.. But I guess you know that already?
Well thanks for the info, and i would like to have more information about COM and threads, apartment as i will have to use multiple com Objects soon...
thanks very much
|
|
|
|
|
Hello Maverick,
That's great news, Maverick. By the way, I've also just finished my example program for you. But it's just as good that you have found a solution in MSDN.
However, if you should need my sample, please do not hesitate to email me directly. My email is :
bllim@singnet.com.sg
Best of Luck, Maverick,
Bio.
|
|
|
|
|
How to use the EnumConnectionPoints, IEnumConnectionPoints in connectable components
any example or sample which can help me out....
thanks in advance
kabir chugh
|
|
|
|
|
IConnectionPointContainer* pCont;
pUnk->QueryInterface(IID_IConnectionPointContainer, (void**)&pCont);
IEnumConnectionPoints pEnum;
pCont->EnumConnectionPoints(&pEnum);
IConnectionPoint* pConnPoint;
while(pEnum->Next(1, &pEnum, NULL) == S_OK) {
DoStuffWithConnectionPoint(pConnPoint);
pConnPoint->Release();
}
(error handling and irrelevant ->Release()'s omitted)
IEnumConnectionPoint follows the standard patterns as all IEnumXXX interfaces does. See the topic on IEnumXXX in the MSDN library.
--
<british-accent>Pass the jam, would you?
|
|
|
|
|
Hmmm,
I have read this chapter "Structured storage and compound files" from not soo good INSIDE OLE by kraig. I am not able to compile the source code given with this chapter, it needs inole.dll which i can't seem to find.
So kindly tell me how to compile it or direct me to some other link etc where i can see a working example of "Structured storage and compound files" in C++ (not using mfc or ATL).
Thank you.
|
|
|
|
|
|
I'm trying to implement the IProcessInitializer interface implemented in COM+ 1.5. I've implemented it in a COM+ DLL created in VC++ 6, using ATL objects, and included the comsvcs.h header file in my app.
When trying to run the object via ASP I get a 'No such interface supported' message. I'm running this on a Windows Server 2003 machine.
Any ideas?
|
|
|
|
|
I know nothing about COM, COM+, ActiveX, OLE or any of these technologies. I've never programed in Visual C++ before, and I have no intention in doing so. However, I have experience in Delphi and C#.
I need to know about COM, etc. Which book will help me grasp these technologies without delving into VC++?
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
Essential COM by Don Box is a good book. But I guess C++ knowledge is kind of a prerequisite for that book. Inside COM by Dale Rogerson.
But still, knowing C++ doesn't do you any harm to fully understand COM.
--
<british-accent>Pass the jam, would you?
|
|
|
|
|
Jörgen Sigvardsson wrote:
But still, knowing C++ doesn't do you any harm to fully understand COM.
???
Tech.Support : Mam, is your pc running under windows?
Customer : No actually its close to the main door.
|
|
|
|
|
COM evolved from C++ virtual table pointers. If you already understand the vtable concept in C++, you can often skip the first 2-3 chapters in a COM-book, and you don't have to give "the interface concept" a second thought as it's as natural as air.
--
<british-accent>Pass the jam, would you?
|
|
|
|
|