DWORD WINAPI _camThread(LPVOID lpParam)
{
	_camThreadLaunched=true;
	while (_camThreadLaunched)
	{
		static bool firstHere=true;
		if (firstHere)
			displayAcknowledgment=true;
		firstHere=false;
		EnterCriticalSection(&m_cs);
		for (int i=0;i<4;i++)
		{
			if (openCaptureDevices[i])
			{
				if (isCaptureDone(i))
					doCapture(i);
			}
		}
		LeaveCriticalSection(&m_cs);
		Sleep(10);
	}
	for (int i=0;i<4;i++)
	{
		if (openCaptureDevices[i])
		{
			deinitCapture(i);
			delete[] captureInfo[i].mTargetBuf;
		}
		openCaptureDevices[i]=false;
	}
	_camThreadLaunched=true;
	return(0);
}
///
/// @return Returns TRUE upon completion
///
bool SimpleDirectShowAcq::Close()
{
	if (m_inited)
	{
		deinitCapture(m_cameraIndex);
	}

	return true;
}
void LUA_END_CALLBACK(SLuaCallBack* p)
{ // the callback function of the new Lua command
	int result=-1; // error

	if (p->inputArgCount>0)
	{ // Ok, we have at least 1 input argument
		if (p->inputArgTypeAndSize[0*2+0]==sim_lua_arg_int)
		{ // Ok, we have (at least) 1 int as argument
			if ( (p->inputInt[0]<4)&&(startCountPerDevice[p->inputInt[0]]>0) )
			{
				startCountOverall--;
				startCountPerDevice[p->inputInt[0]]--;
				if (startCountPerDevice[p->inputInt[0]]==0)
				{
					EnterCriticalSection(&m_cs);
					deinitCapture(p->inputInt[0]);
					delete[] captureInfo[p->inputInt[0]].mTargetBuf;
					openCaptureDevices[p->inputInt[0]]=false;
					LeaveCriticalSection(&m_cs);
				}
				if (startCountOverall==0)
					killThread();
				result=1;
			}
			else
				simSetLastError(LUA_END,"Invalid device index."); // output an error
		}
		else
			simSetLastError(LUA_END,"Wrong argument type/size."); // output an error
	}
	else
		simSetLastError(LUA_END,"Not enough arguments."); // output an error


	// Now we prepare the return value:
	p->outputArgCount=1; // 1 return value
	p->outputArgTypeAndSize=(simInt*)simCreateBuffer(p->outputArgCount*2*sizeof(simInt)); // x return values takes x*2 simInt for the type and size buffer
	p->outputArgTypeAndSize[2*0+0]=sim_lua_arg_int;	// The return value is an int
	p->outputArgTypeAndSize[2*0+1]=1;				// Not used (table size if the return value was a table)
	p->outputInt=(simInt*)simCreateBuffer(1*sizeof(result)); // 1 int return value
	p->outputInt[0]=result; // This is the int value we want to return
}