Advertisement

05.15.2008 at 03:46AM PDT, ID: 23404521 | Points: 200
[x]
Attachment Details

Trouble invoking SAS using ShellExecuteEx()

Tags: C/C++
Hello,
From my dll (written in C/C++), I want to invoke SAS to run a script of interest. I do this repeatedly to simulate a situation of interest. To invoke SAS, I am using the API ShellExecuteEx(), passing a SAS script file name as the commandline argument to SAS (through the lpParameters member of SHELLEXECUTEINFO structure).
I then call WaitForSingleObject() function of the hProcess member of SHELLEXECUTEINFO to wait for SAS to finish the job and exit. And then I proceed with the rest of the processing.
This works fine more or less. But on some machines, the code fails after certain number of calls (the count differs from run to run and from machine to machine). I have checked the code for any memory overrun or other runtime problems and haven't found any (I have used BoundsChecker to make sure of this). When the runtime error happens, GetLastError() returns either 126 or 183. I am not able to find out why this happens.
Any help will be appreciated. Thanks.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
long RunSAS(char* psScriptFile)
{
	char sParams[2000];
	long nRetVal = ERR_NONE;
	SHELLEXECUTEINFO ShExecInfo;
 
	SetLastError(0);
			
	try
	{
		try
		{
			sprintf(sParams, "\"%s\" -nosplash", psScriptFile);
			
			ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
			ShExecInfo.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE;
			ShExecInfo.hwnd = NULL;
			ShExecInfo.lpVerb = "open";
			ShExecInfo.lpFile = "sas.exe";
			ShExecInfo.lpParameters = sParams;
			ShExecInfo.lpDirectory = NULL;
			ShExecInfo.nShow = SW_SHOWMINIMIZED;
			ShExecInfo.hInstApp = NULL;
			
			if(ShellExecuteEx(&ShExecInfo) == FALSE)
			{
				// Failed to call SAS
				// TODO: Handle this error properly.
				throw (long)GetLastError();
			}
			
			if(ShExecInfo.hProcess != NULL)
			{
				while(WaitForSingleObject(ShExecInfo.hProcess, 50) == WAIT_TIMEOUT)
				{
					;
				}
				
				// Closing process and thread handles 
				CloseHandle(ShExecInfo.hProcess);
			}
 
			throw ERR_NONE;
		}
		catch(long nErr)
		{
			throw nErr;
		}
		catch(...)
		{
			if(GetLastError() != 0)
			{
				throw (long)GetLastError();
			}
 
			throw ERR_SAS_RUN;
		}
	}
	catch(long nErr)
	{
		nRetVal = nErr;
	}
 
	return nRetVal;
}
Start your free trial to view this solution
Question Stats
Zone: Programming
Question Asked By: AniOnLine
Question Asked On: 05.15.2008
Participating Experts: 1
Points: 200
Views: 0
Translate:
Loading Advertisement...
05.15.2008 at 08:06AM PDT, ID: 21574434

Rank: Genius

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080236-EE-VQP-29 / EE_QW_2_20070628