A JUCE Process-Control Class


#1

Hi Jules,
maybe this code is a good beginning to extend this to a full process control class for JUCE.
It just starts a external process and check if its running.

Interface

class Process
{

public:
	Process(File _file): pfile(_file),proc_info(0)
	{
		
	};


	~Process()
	{
		winDeleteHandle(proc_info);		
	};

	bool start(String arguments)
	{
		winDeleteHandle(proc_info);
		String commandLine(pfile.getFullPathName()+" "+arguments);
		return winStartProcess(commandLine,proc_info);
	}

	bool isRunning()
	{
		if (proc_info!=0)
		{
			if (winIsProcessRunning(proc_info))
			{
				return true;
			}
		}
		return false;
	}
private:
	File pfile;
	void* proc_info;
};

Platform-Code

bool winStartProcess(wchar_t* commandLine, void*& processInfo   )
{
	DWORD dwCreationFlags = CREATE_NO_WINDOW;	// if Console application

	dwCreationFlags |= CREATE_UNICODE_ENVIRONMENT;

	HANDLE stdinHandle=0;
	HANDLE stdoutHandle=0;
	HANDLE stderrHandle=0;

	STARTUPINFOW startupInfo = { sizeof( STARTUPINFO ), 0, 0, 0,
		(DWORD)CW_USEDEFAULT, (DWORD)CW_USEDEFAULT,
		(DWORD)CW_USEDEFAULT, (DWORD)CW_USEDEFAULT,
		0, 0, 0,
		0,
		0, 0, 0,
		stdinHandle,stdoutHandle,stderrHandle
	};


	LPPROCESS_INFORMATION pinf=new PROCESS_INFORMATION;
	processInfo=(void*) pinf;

	BOOL ret = CreateProcess(0, commandLine,0, 0, TRUE, dwCreationFlags, 0 ,0,&startupInfo, pinf);

	return ret!=0;
}

bool winIsProcessRunning( void* processInfo   )
{
	LPPROCESS_INFORMATION pinf=(LPPROCESS_INFORMATION)processInfo;

	if (WaitForSingleObject(pinf->hProcess, 0) == WAIT_OBJECT_0)
	{
		return false;
	} else
	{
		return true;
	}
};


void winDeleteHandle(void*& processInfo   )
{
	LPPROCESS_INFORMATION pinf=(LPPROCESS_INFORMATION)processInfo;

	if (pinf!=0) {
		CloseHandle(pinf->hThread);
		CloseHandle(pinf->hProcess);
		delete pinf;
		processInfo = 0;
	}
};

#2

Thanks, yes, I’ve been meaning to write one of those for a long time! Will try to find time to do it soon!


#3

Cross platform, please :wink:


#4

:smiley: thats why i share this code, and read() and write() methods for interprocess communication via stdin/stdout