Patrick Hughes Guest
|
Posted: Tue Jan 25, 2005 4:08 am Post subject: Desperately need help with CoInitialize |
|
|
Thanks for any help you might have, here's my dilemma.
I'm working on a project that runs in the background and uses a timer to
grab an AutoCad session when it starts up (EDS_TIMER). When I find the
AutoCad window the timer is killed and I begin the ACAD_TIMER which then
monitors the window for changes to the document.
The problem is I can't seem to keep track of the number of CoInitialize
calls to keep them balanced with CoUnInitialize calls. It ends up every time
I close AutoCAD I receive a runtime error, "Abnormal Program Termination"
I've tried trapping the return values of S_OK, S_FALSE, etc., adding
CoUnInit in the exceptions, adding counters to count the number of time
CoInit is called, hooking into windows to trap a WM_QUIT message, so on and
so on.
Any Ideas or suggestions???
WM_TIMER : // Add code here for all timers to be used.
begin
case (wParam) of
EDS_TIMER: // every 5 seconds - Try to get acad application
begin
GetAcad;
If AcadRunning = true then //AutoCad is running
begin
AcadSessionBegin := Now;
SetTimer(h_Wnd, ACAD_TIMER, ACAD_INTERVAL, nil);
KillTimer(h_Wnd, EDS_TIMER);
end;
Result := 0;
end; //end EDS_TIMER
ACAD_TIMER: // every .5 seconds - After AutoCad Opens, Monitor
AutoCad
begin
If AcadRunning = true then
begin
AcadSessionEnd := Now;
AcadSessionTime := AcadSessionEnd - AcadSessionBegin;
if (AcadWindowTextChanged = true) or (RepeatGetAcad =
true) then
GetAcad;
end
else //AutoCad No Longer Running
begin
WriteToSessionLog;
VarReset;
//messagebeep(MB_ICONQUESTION);
SetTimer(h_Wnd, EDS_TIMER, EDS_INTERVAL, nil);
KillTimer(h_Wnd, ACAD_TIMER);
end;
Result := 0;
end; //end ACAD_TIMER
........
function GetAcad: string;
begin
try
CoInitializeEx(nil, COINIT_APARTMENTTHREADED);
Acad := GetActiveOLEObject('AutoCAD.Application'); //Returns
Classname(programmatic ID)
AcadDoc := Acad.ActiveDocument;
AcadCap := Acad.Caption;
GetAcadDocVariables;
//Windows.Beep(1767, 50);
RepeatGetAcad := false;
CoUnInitialize();
except
// on EOleSysError do RepeatGetAcad := true;
// on EAccessViolation do RepeatGetAcad := true;
// on EOleError do RepeatGetAcad := true; //WriteToErrorLog('EOleError in
GetAcad');
// on EVariantError do RepeatGetAcad := true;
//WriteToErrorLog('EVariantError in GetAcad');
on E: Exception do RepeatGetAcad := true; //WriteToErrorLog('Unknown
Exception in GetAcad');
end;//end try
end;
--
Patrick Hughes
Engineered Design Solutions
http://www.engds.com
|
|