|
The application that I'm working on has an OnIdle() handler. It was written by someone else - despite my suggestion that a timer would better suit the situation. :P
Now, I've added a few more ATL objects (the project already had a few), and for some strange reason, simply having the object created at initialization time causes an OnIdle() call after the App has closed and cleared some of it's state.
class CMyObject :
public CComObjectRootEx<CComSingleThreadModel>,
public IMyInterface {
...
};
class CMyApp : public CWinApp {
...
CComObject<CMyObject> *m_pObj;
...
BOOL OnIdle(long);
void Init();
...
};
void CMyApp::Init() {
...
CComObject<CMyObject>::CreateInstance(&m_pObj);
...
}
BOOL CMyApp::OnIdle(long) {
...
CMainFrame *pMF = static_cast<CMainFrame *>(m_pMainWnd);
...
}
The initialization goes fine, and hell it has nothing to do with the App or main frame. Then, when I close the application, it crashes because OnIdle() is getting called AFTER the App state has been cleared, and pMF is NULL.
Does simply creating an ATL object somehow screw around with the application message queues? Is the OnIdle() getting called from another thread? My ATL objects are all using CComSingleThreadModel...
J
|
|
|
|
|
If anyone's curious as to what the hell I was babbling about, I found the problem.
The trouble is, when the CMainFrame closes, it calls AfxOleCanExitApp() to check if there are any outstanding object references. If there are, the frame gets hidden but NOT destroyed. That's why OnIdle() was still being called, and why it was dying when it tried to use the CMainFrame.
Now, my problem has changed. I have to find a way to release all my objects before the main window gets closed. Seems a little odd...
J
|
|
|
|
|
Not Odd, that's the correct way to go. You must get the object pointer and
release it in OnDestroy(). For ActiveX, you should not have to do this since
CWnd does it for you.
|
|
|
|
|
I agree, it makes sense for graphic components and ActiveX controls. However, the object to which the dangling reference belongs is an Active Script engine, and is currently owned by the App. If I simply move it to the main frame, everything will probably work.
But that doesn't fit the conceptual layout of our application. We have a scripting engine that gets created when the application starts, and killed when the application ends. That makes more sense than a scripting engine that gets created and destroyed with the frame window...
J
|
|
|
|
|
I have a com object in a local server (ATL) with one method that just sets some dummy output data. If I set the parameter type to be:
[out] long* pLong
then it at least works in release mode (I can get the value from a different process). If I set the parameter type to be:
[out] BSTR* pString
then it will generate a "out of memory error". Both versions won't work in debug mode.
My question is, how do I specify output parameter type if it is not an in-proc-server?
|
|
|
|
|
The IDL used for returning data described is correct. The problem will be in how the data is being assigned to the pString. Can you post some more code?
Michael
|
|
|
|
|
For the case of the "[out] long* pLong" parameter, it is:
*pLong = 1000; // I can get this value in another process
For the case of the "[out] BSTR* pString" parameter, it is:
*pString = ::SysAllocString(L"This is an output string");
|
|
|
|
|
Dir forum,
I am a beginner in ATL, WTL, and COM. I want to write a WTL a program like IE. And I found a article in Codeproject. It is "Using WTL's Built-in Dialog Resizing Class" by Michael Dunn. I like his articles very much! But I don't know how to capture IE event like OnDocumentComplete and others, to disable and enable some key like Stop, Back, Forward ...
If you have another project to demo. Please show it to me!
Thank you very much!
|
|
|
|
|
There are a few questions. I am desperately seeking answers. Please do reply if you know any of them as it will save me a lot of time.
#1. What exactly is an apartment? Do we mean a method / function? What if a method call to a COM object internally calls another method of the same COM object? Will it still be ONE apartment?
#2. Do we ever need proxy / stubs for COM objects packaged as DLLs? Can you please come up with a scenario? DLLs are in-process so what process is the stub part of?
#3. This last one is lengthy and will require some concentration:
I have Beginning ATL 3 COM Programming (borrowed from Safri Ustaad). According to the example in the book (bare bone pure C++ COM implemented as a DLL), a COM class implementing IFoo should have these: a CFoo class implementing IFoo (and thus IUnknown) and a CClassFactory class implementing IClassFactory (and thus IUnknown). The DLL should have a method DllGetClassObject that should call CreateInstance of CClassFactory which should internally create a new CFoo and call it's QueryInterface. The pointer to CFoo should be returned by DllGetClassObject. However, to the best of my experiments, things are not working this way. I am afraid the book is wrong.
The call to DllGetClassObject should not call CreateInstance; instead it should return a pointer to CClassFactory. The CoCreateInstance of COM mechanism will then _itself_ call CreateInstance on this pointer (as it implements IClassFactory). The CreateInstance should then create a new CFoo and call it's QueryInterface and return it to the caller.
In short, the difference is between the stuff returned by DllGetClassObject and calling CreateInstance of CClassFactory. Do tell me whether my hypothesis is right or wrong as I am still not confident that the book from Wrox has such an error. Overall, the book is an excellent introduction to ATL programming and there are a couple of chapters on the background theory.
Waiting for a response....
Muhammad Ali Shah
~~~~~~~~~~~~~~~
Our scientific power has outrun our spiritual power. We have guided missiles and misguided men (Martin Luther King Jr)
|
|
|
|
|
Right. The DllGetClassObject gets an interface to the class factory for the .dll, not any particular object in the .dll. This will then be used to handle calls to CoCreateInstance by the run time. Client code does not need to be aware of the existence of the class factory.
Here's an excerpt from the documentation for the function. In the generic ATL code generated for you in ATL COM projects, you'll get a class factory, and an implementation of DllGetClassObject. The class factory will tie together the CLSID's for all of the objects that it knows how to create to the actual implementing classes through the OBJECT_MAP.
You could do something similar in raw C++, you just need to tie together the CLSID's with the actual classes for your implementation of a class factory. The DllGetClassObject will just return an interface to the the class factory, which does all of the real work.
STDAPI DllGetClassObject(
REFCLSID rclsid, //CLSID for the class object
REFIID riid, //Reference to the identifier of the interface
// that communicates with the class object
LPVOID * ppv //Address of output variable that receives the
// interface pointer requested in riid
);
Parameters
rclsid
[in] CLSID that will associate the correct data and code.
riid
[in] Reference to the identifier of the interface that the caller is to use to communicate with the class object. Usually, this is IID_IClassFactory (defined in the OLE headers as the interface identifier for IClassFactory).
ppv
[out] Address of pointer variable that receives the interface pointer requested in riid. Upon successful return, *ppv contains the requested interface pointer. If an error occurs, the interface pointer is NULL.
|
|
|
|
|
I've been wondering about marshalling issues in the following senerio
ObjA - Is a single-threaded ATL COM object singleton
ObjA is instantiated in Application A. Sometime later Application B is started and cocreates ObjA. Since it is a singleton they both have the same instance. My questions are whether their are any marshalling issues that have to be address because ObjA was created in a different process. Would it matter if ObjA where multi-threaded.
|
|
|
|
|
Yes, you need to synchronize method and property in your object with Semaphore
and critical section (your choice...). I did the same thing with a Local server
Component and it works really fine.
|
|
|
|
|
Thanks, but my questions were about marshaling not synchronization. I don't see synchronization as an issue since the object is single-threaded. Obviously a multi-threaded object would require it.
|
|
|
|
|
I have an apartmented ATL server program running on W2K at work. Now mangement has decided to run it on a fancy, spaning new machine with 1 gig ram, dual processors, and 4 90-gig hard drives.
Question: is it possible to change to server program to take advantage of the dual-processing capabilities? If it is, how? Is there a book or white paper some place that explains how to do that ?
|
|
|
|
|
just install windows2000 adn make it recognize that the server runs on dual processor. thats it. It will take care. Yes. there are ways in win32 in which u can use the dual processor feature and control them. But then that will defeat the idea on COMponent at all, because it will becomes windows proprietary
Ganesh Ramaswamy
|
|
|
|
|
I've created an ActiveX control with VC++ 6.0, and i try to call CoInitializeEx() at the beginning of a worker thread, but it doesnt recognize the API, and all I get is an undeclared identifier error for that
and COINIT_MULTITHREADED;
I linked to "ole32.lib" and i threw in <objbase.h> in for kicks at the top of the .cpp and in a separate case, the .h
I AM LIKE ----------------| |-------------------
^
THIS CLOSE
to getting this crap... i dont have a clue... i've running the project on a different machine as well and i get the same errors. i've tried to throw in just about every other library as well... am i missing a library? Could someone show me what libraries they link to in a project that works? Any suggestions would be so helpful...
Thanks,
~Tim
SHABBA!!
|
|
|
|
|
CoInitializeEx requires either NT 4.x or greater or Win 98 (Win 95 with DCOM).
Try either #define _WIN32_DCOM or #define WIN32_WINNT 0x0400.
Michael
|
|
|
|
|
If you are using ATL, in your Stdafx.h
make sure sure have before all includes
#define STRICT
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#define _ATL_FREE_THREADED //_ATL_APARTMENT_THREADED
Make sure your WinMain look like this, you need CLSCTX_LOCAL_SERVER
if you are using a local server.
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE , LPTSTR lpCmdLine, int )
{
lpCmdLine = GetCommandLine();
#if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
#else
HRESULT hRes = CoInitialize(NULL);
#endif
if (bRun)
{
_Module.StartMonitor();
#if _WIN32_WINNT >= 0x0400 & defined(_ATL_FREE_THREADED)
hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED);
_ASSERTE(SUCCEEDED(hRes));
hRes = CoResumeClassObjects();
#else
hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE);
#endif
_ASSERTE(SUCCEEDED(hRes));
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
DispatchMessage(&msg);
_Module.RevokeClassObjects();
Sleep(dwPause);
}
_Module.Term();
CoUninitialize();
return nRet;
}
|
|
|
|
|
Sweet guys! that definitely fixed it.
Now on to our next question... (the next thread)
SHABBA!!
|
|
|
|
|
er... we'll do it right here...
ok... It goes through all of the many events raised, and then when it gets to the last one, it'll crash. hmm... it crashes in COleDispatchDriver::InvokeHelperV 's call to
COleDispatchDriver::Invoke... then it goes into assembly.(grr)
(it's referencing something with nothing in it (0x000005)
Has anyone had an error similar to that?
I'm pretty darn sure i'm not wiping anything out before that last event finishes... i really need this last event too...<sigh>
I hope you guys have had this happen to you as well, and just so you know, the events are sent to a VB app... i'm sure y'all knew that though. Not as much stress on me now, but it's still lingering...
Help!! Thanks, people! Learnin' from the best I guess!
~Tim
SHABBA!!
|
|
|
|
|
Happy to know that you finally made it!
The only reason I can see is that you are calling that function with
invalid arguments (why the other event function crash?)
If you were using an old client with VB, you may need to rebuild your
VB client.
Give a sample of your code, people will help you faster!
|
|
|
|
|
ok here comes the sample:
I have my MFC ActiveX Full Control (according to VC++ 6.0 Wizard).
//Which Calls:
CESpyderCtl::Start() {
//oh, and i call CoInitializeEx(NULL, COINIT_MULTITHREADED)
// in CESpyderApp::InitInstance() also, but i dont know what runs first...
// havent tried to find out. *shrug*
_beginthread(...)
//err checking
}
//Begin Thread Calls a global function called:
void gStart(void *spy) {
//which ultimately calls Parse() that fires an event pretty often
//so it can update a color box to show what it's doing.
CoInitializeEx(NULL, COINIT_MULTITHREADED); //i call this in
//CESpyderApp::InitInstance() as well.
Parse();
CoInitializeEx(NULL, COINIT_MULTITHREADED);
}
//So inside of Parse(); we've got this
int CESpyder::Parse() {
//parse a file
Fire_StatusChange(STAT_SEARCHING); //events are status changes
//connecting, adding, searching, error, and done.
//and on the last one, it's doing the same thing, and i'm firing a done event.
Fire_StatusChange(STAT_DONE);
//i also fire a separate done event, but it never gets to it, and i've also tried to just fire a done event, but it crashes the same way.
Fire_Done();
return 1;
}
I figure this still wont give you enough information, i've tried putting
Sleep()'s all over the place.., but it seems like it doesnt affect it...
<sigh>, i'm using apartment threading, if i remember correctly it wont let me pick apartment or free threaded. i think i even tried doing ATL but free was greyed out... either way i dont know how to Fire events in VC with an ATL object... (you could show me that, but i'll never be able to get rid of this thing that will eat at my soul for eternity... haha!
Thanks,
~Tim
SHABBA!!
|
|
|
|
|
Sorry if I can't help you. All I can tell you is your bug
is maybe related to MFC. I always use ATL to develop ActiveX control.
I know MFC has a macro, I think it's: AFX_MANAGE_STATE( ), where I have
seen developer using: AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
but again I have never use it. Do you always fire event from the same thread?
What's the other thread is doing?
|
|
|
|
|
i tried throwing that in there to no avail, but i went and searched for firing events from worker threads with AFX_MANAGE_STATE in there as well and found some stuff, so i'm working on trying one of those out right at this moment..
do you think you could show me how to raise an event from C++ using an ATL control ? (for kicks i can try it through ATL and see how well it works then)
~Timothy T. Rymer
www.digipen.edu
tim.xpertz.com
|
|
|
|
|
Okay I will send you an example how I did something similar.
|
|
|
|