LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Whether an application stays open after calling CA_DiscardObjHandle(apphandle)

I have a program that uses ActiveX to paste bitmaps into a newly created instance of PowerPoint and leaves PowerPoint open.  Sometime that instance of PowerPoint spontaneously closes many minutes after it was started and after the app is done with it. This can result in a lot of lost work if the user had been editing the ppt presentation. 
 
My app dutifully finishes by discarding the handles for both the presentation and the app with two calls to CA_DiscardObjHandle().  In the help for CA_DiscardObjHandle() I see the following:
 
If objHandle is the only reference to the ActiveX server and the ActiveX server is an application, the server application might shut down after you call CA_DiscardObjHandle.
 
From this I infer that perhaps I should not Discard the application handle if I want to leave PowerPoint open for the user to continue editing.
 
What is the correctly way to "clean up" so that the app is left open for the user to reliably use? i.e. do I discard the handle(s) or not?
0 Kudos
Message 1 of 4
(3,482 Views)
Hi Ian W,

I have been researching this issue and have been unable to reproduce it thus far.  I have been using the simple2000 PowerPoint / ActiveX example that ships with LabWindows/CVI.  You can access this example by going to Help » Find Examples, click on the Search tab and search for ActiveX, and then select the simple2000.cws example.

In this example, I went in and commented out the line errCheck(ErrMsgFltr (PP__ApplicationQuit (hApplication, NULL))); in the ExitPPTCB() function call.  This is the command that tells PowerPoint to exit.  With this disabled, PowerPoint remains open, even after CA_DiscardObjHandle has been called and the application has been closed.  It is commonly agreed that as good practice, you will want to close your object references using CA_DiscardObjHandle once you are done with them.

As I am unable to reproduce this issue, would it be possible for you to attach a simplified version of your application that exhibits this behavior?
0 Kudos
Message 2 of 4
(3,445 Views)

Thanks very much for looking into it, Jonathan.  I will try with the example and re-post with any discoveries - but to date it has been an occasional problem that makes troubleshooting a challenge.

--Ian

0 Kudos
Message 3 of 4
(3,416 Views)
You should always call CA_DiscardObjHandle on all open handles to properly cleanup memory. There is no general rule about how ActiveX EXE servers behave as far as exiting when all open handles are closed. The general convention is that the server should exit if there is no interactive user using it - the server implementor might determine this based on visibility or actual user interaction (mouse-clicks, keyboard input, etc).
 
If you want to leave the application running after 'automating' it from your CVI program, then I recommend that you make it visible if you are not already doing so. There should be a method/property in the server to do this. Even if you make some servers visible, they may exit when all handles are closed - they may or may not expose some other method/property (UserControl, Activate, etc) that affects this. In the worst case, you can leave a handle open if that is what it takes to keep the server running - this will result in a small memory leak, but should not be a big deal.
 
If you run the client program repeatedly, then you may want to ensure that your client program is not launching too many instances of the server. You can try to do this, by first calling the 'Active_Application' function instead of the 'New_Application' function. The Active function will try to connect to an already running instance of the server. If there is no instance running, then it returns an error, and you can then call the New_Application function. But this may not be what you want to do - because when you do this, you will affect the usability of any interactive users currently using the server program.
 
On the other hand, if you want to ensure that the server exits after you disconnect from the client program, you should call the exit or quit method of the server. In general, this is a better solution. You launch a new instance of the server program, keep it invisible, automate it from the client program to do what you want, and then call its quit method to make sure it exits. But in your case, this may not be desired.
0 Kudos
Message 4 of 4
(3,401 Views)