[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBS script run from a C++ program hangs onto thread, wont allow shut down and clean up

Posted on 2009-04-17
7
Medium Priority
?
930 Views
Last Modified: 2012-05-06
Hello,
I have a program written in Visual Studio 6.0 using C++. The program should run until it receives a message to switch, then it should shut itself down, wait a few seconds, and then start up its sister program.  Currently if I open the first program and close it manually, then run the sister program manually (double-clicking on it) everything works fine.  I am trying to make it simpler for the user though and use a script that will run to handle this action when a special message is received.  

Currently the flow of the program is for the main program to shut itself almost all the way down before it opens a VBScript file that will loop, waiting for the main program to close.
The main program then finishes closing itself.  When the main is closed the vbs file will then wait 30 seconds and then run the sister program.  The problem is that when I use ShellExecute to run the vbs file it seems to create a new thread.  When MAIN closes and the sister program attempts to open I run into numerous errors with memory and inabilty to acess things that should have been cleaned up and freed when MAIN shut down (They share many resources).  But if I run the script on my own from outside of my main program then it works fine... it is only if I call it from main that I receive the problem.

Anyone have any ideas /sample code on how I can run this vbs script from my program but not have this thread attaching back to the parent program?  Not much to it but I included the code on how I call ShellExecute.
HINSTANCE nResult = ShellExecute(NULL, NULL, _T("C:\\Projects\\EMMT\\Software\\Scripts\\Switch_Over.vbs"), _T("1"), NULL, SW_HIDE);
if( (int)nResult == ERROR_FILE_NOT_FOUND)
{
//not found
}

Open in new window

0
Comment
Question by:yelowsnw2
  • 4
  • 3
7 Comments
 
LVL 49

Expert Comment

by:DanRollins
ID: 24173317
I can say that it seems to work as expected when I try it....
#include "stdafx.h"
#include <windows.h>
#include <shellapi.h>
 
int main(int argc, char* argv[])
{
	char szFile[]="c:\\temp\\test.vbs";
	HINSTANCE nResult = ShellExecute(NULL, "open", szFile, "1", NULL, SW_SHOWNORMAL );
	if( (int)nResult == ERROR_FILE_NOT_FOUND) {
		//not found
	}
	if( (int)nResult <= 32 )  {
		// SOME OTHER ERROR
	}
 
	return 0;  /// exit from this console app
}

Open in new window

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 24173319
Here's the VBS I used "test.vbs"
Option Explicit
 
Const cIMG = "<img src='file:\\C:\TEMP\sourcegray.bmp' border='1'>"
 
Dim oIEA : Set oIEA = CreateObject("InternetExplorer.Application")
With oIEA
  .ToolBar = 0
  .AddressBar =false
  .StatusBar = 0
  .Width = 700
  .Height = 550
  .Left = 0
  .Top = 0
  .MenuBar = false
  .Navigate "about:blank"
  While oIEA.Busy
  Wend
  .Document.Title = "Murphy Gets The Finger!!!"
  .Document.Body.InnerHTML = cIMG
  .Visible = 1
End With 
 
WScript.Sleep 10 * 1000
 
On Error Resume Next 
oIEA.Quit
set oIEA = Nothing

Open in new window

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 24173328
In the Task Manager, I can see that CONSOLE22.EXE starts, then WSCRIPT.EXE starts, then CONSOLE22.EXE ends, then WSCRIPT.EXE ends a few seconds later (WSCRIPT.EXE is the program that actually runs a VBS or JS script file) .
BTW, always use SW_SHOWNORMAL for testing!! -- never leave yourself in the dark :-)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:yelowsnw2
ID: 24343766
The bigger problem I was having was that the C++ program would call the script , the script would run and TASKKILL the C++ program that called it, then would start up it's sister C++ program.  The only issue was that the C++ program that got killed would never clean up or free up the memory it was using until the script went away.  As if Windows saw the child thread was still running, not allowing end of program cleanup.  The sister program that then starts up (basically same exact program with a different name) and is not allowed to hook into that memory because it is still in use by the first program as the script hasn't finished yet.

We have since had our main C++ program gracefully close itself down (no more TASKKILL) but the problem still remained as it still called a script.
0
 

Author Comment

by:yelowsnw2
ID: 24343779
Oops, forgot to add.  We have since found a workaround and it works now, but not in the best of ways.  We had to create a new unrelated exe that basically just sits there and watches for a sign that it is time to switch over from one to the other.  Once the main C++ program exits on its own then it just calls the same script and it works fine now.  The memory gets released by windows now.

But I would still like better clarification on the problem, and I do wonder if there is a way to better avoid it.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 750 total points
ID: 24345949
Using the TaksKill command within the script might not be such a good idea, however, it seems to me that it would cause the system to free up all resources -- memory, file handles, etc -- as expected.  One thing worth trying is
   Taskkill /T
...which will also terminate any child processes that were started by it.
Having the original EXE die normally would be preferrable.  It could loop, waiting for a change in a registry value or the creation of a certain file (or other globally-available signal) and then exit normally.
0
 

Author Closing Comment

by:yelowsnw2
ID: 31571637
Haven't had a chance to test this out yet but it's something I haven't tried yet!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month20 days, 9 hours left to enroll

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question