examples/Qt/ISFEditor/ISFEditor_app/Spout/SpoutSDK.cpp
//
// ========= USER SELECTION PANEL TEST =====
//
//	This is necessary because the exit code needs to be tested
//

USER SELECTION PANEL TEST
This is necessary because the exit code needs to be tested
USER SELECTION PANEL TEST This is necessary because the exit code needs to be tested
bool Spout::CheckSpoutPanel()
{
	if(bSpoutPanelOpened) { 
		SharedTextureInfo TextureInfo;
		HANDLE hMutex = NULL;
		DWORD dwExitCode;
		char newname[256];
		char activename[256];
		bool bRet = false;
		hMutex = OpenMutexA(MUTEX_ALL_ACCESS, 0, "SpoutPanel");
		if(!bSpoutPanelActive) {
			if(hMutex) bSpoutPanelActive = true;
		}
		else if (!hMutex) { 
			bSpoutPanelOpened = false; 
			bSpoutPanelActive = false;
			if(m_ShExecInfo.hProcess) {
				GetExitCodeProcess(m_ShExecInfo.hProcess, &dwExitCode);
				if(dwExitCode == 0) {
					newname[0] = 0;
					if(!ReadPathFromRegistry(newname, "Software\\Leading Edge\\SpoutPanel", "Sendername")) {
						string line;
						HMODULE module;
						char path[MAX_PATH], drive[MAX_PATH], dir[MAX_PATH], fname[MAX_PATH];
						module = GetModuleHandle(NULL);
						GetModuleFileNameA(module, path, MAX_PATH);
						_splitpath_s(path, drive, MAX_PATH, dir, MAX_PATH, fname, MAX_PATH, NULL, 0);
						_makepath_s(path, MAX_PATH, drive, dir, "spoutpanel", ".txt");
						ifstream infile(path, ios::in);
						if (infile.is_open()) {
							if(getline(infile, line)) {
								strcpy_s(newname, 256, line.c_str());
							}
							infile.close();
							remove(path);
						}
						if(newname[0] != 0) {
							if(interop.senders.getSharedInfo(newname, &TextureInfo)) {
								interop.senders.RegisterSenderName(newname);
								interop.senders.SetActiveSender(newname);
							}
						}
					}
					if(newname[0] != 0) {
						if(interop.senders.GetActiveSender(activename)) {
							if(strcmp(activename, newname) != 0) { 
									strcpy_s(newname, activename); 
							}
						}
						if(interop.senders.getSharedInfo(newname, &TextureInfo)) {
							strcpy_s(g_SharedMemoryName, 256, newname);
							g_Width  = (unsigned int)TextureInfo.width;
							g_Height = (unsigned int)TextureInfo.height;
							g_Format = TextureInfo.format;
							g_ShareHandle = (HANDLE)TextureInfo.shareHandle;
							bRet = true; 
						}
					}
					else {
						if(interop.senders.GetActiveSender(newname)) { 
							if(interop.getSharedInfo(newname, &TextureInfo)) {
								strcpy_s(g_SharedMemoryName, 256, newname);
								g_Width  = (unsigned int)TextureInfo.width;
								g_Height = (unsigned int)TextureInfo.height;
								g_Format = TextureInfo.format;
								g_ShareHandle = (HANDLE)TextureInfo.shareHandle;
								bRet = true; 
							}
						} 
					} 
				} 
			} 
		} 
		CloseHandle(hMutex);
		return bRet;
	} 
	return false;
}
bool Spout :: CheckSpoutPanel ( ) { if ( bSpoutPanelOpened ) { SharedTextureInfo TextureInfo ; HANDLE hMutex = NULL ; DWORD dwExitCode ; char newname [ 256 ] ; char activename [ 256 ] ; bool bRet = false ; hMutex = OpenMutexA ( MUTEX_ALL_ACCESS , 0 , " " ) ; if ( ! bSpoutPanelActive ) { if ( hMutex ) bSpoutPanelActive = true ; } else if ( ! hMutex ) { bSpoutPanelOpened = false ; bSpoutPanelActive = false ; if ( m_ShExecInfo . hProcess ) { GetExitCodeProcess ( m_ShExecInfo . hProcess , & dwExitCode ) ; if ( dwExitCode == 0 ) { newname [ 0 ] = 0 ; if ( ! ReadPathFromRegistry ( newname , " \\ \\ " , " " ) ) { string line ; HMODULE module ; char path [ MAX_PATH ] , drive [ MAX_PATH ] , dir [ MAX_PATH ] , fname [ MAX_PATH ] ; module = GetModuleHandle ( NULL ) ; GetModuleFileNameA ( module , path , MAX_PATH ) ; _splitpath_s ( path , drive , MAX_PATH , dir , MAX_PATH , fname , MAX_PATH , NULL , 0 ) ; _makepath_s ( path , MAX_PATH , drive , dir , " " , " " ) ; ifstream infile ( path , ios :: in ) ; if ( infile . is_open ( ) ) { if ( getline ( infile , line ) ) { strcpy_s ( newname , 256 , line . c_str ( ) ) ; } infile . close ( ) ; remove ( path ) ; } if ( newname [ 0 ] != 0 ) { if ( interop . senders . getSharedInfo ( newname , & TextureInfo ) ) { interop . senders . RegisterSenderName ( newname ) ; interop . senders . SetActiveSender ( newname ) ; } } } if ( newname [ 0 ] != 0 ) { if ( interop . senders . GetActiveSender ( activename ) ) { if ( strcmp ( activename , newname ) != 0 ) { strcpy_s ( newname , activename ) ; } } if ( interop . senders . getSharedInfo ( newname , & TextureInfo ) ) { strcpy_s ( g_SharedMemoryName , 256 , newname ) ; g_Width = ( unsigned int ) TextureInfo . width ; g_Height = ( unsigned int ) TextureInfo . height ; g_Format = TextureInfo . format ; g_ShareHandle = ( HANDLE ) TextureInfo . shareHandle ; bRet = true ; } } else { if ( interop . senders . GetActiveSender ( newname ) ) { if ( interop . getSharedInfo ( newname , & TextureInfo ) ) { strcpy_s ( g_SharedMemoryName , 256 , newname ) ; g_Width = ( unsigned int ) TextureInfo . width ; g_Height = ( unsigned int ) TextureInfo . height ; g_Format = TextureInfo . format ; g_ShareHandle = ( HANDLE ) TextureInfo . shareHandle ; bRet = true ; } } } } } } CloseHandle ( hMutex ) ; return bRet ; } return false ; }
USER SELECTION PANEL TEST 
This is necessary because the exit code needs to be tested
USER SELECTION PANEL TEST This is necessary because the exit code needs to be tested
// MessageBoxA(NULL, "CheckSpoutPanel()", "SpoutSDK", MB_OK);
 // If SpoutPanel has been activated, test if the user has clicked OK
 // User has activated spout panel
 // Must find the mutex to signify that SpoutPanel has opened
 // and then wait for the mutex to close
 // Has it been activated 
 // If the mutex has been found, set the active flag true and quit
 // otherwise on the next round it will test for the mutex closed
 // It has now closed
 // Don't do this part again
 // call GetExitCodeProcess() with the hProcess member of SHELLEXECUTEINFO
 // to get the exit code from SpoutPanel
 // Only act if exit code = 0 (OK)
 //
 // SpoutPanel has been activated and OK clicked
 //
 // Sender name entry
 //
 // Check for an unregistered sender first because this will not have been set as active yet
 // Try to get the current sender name from the registry (24.05.15 instead of text file)
 // Text file method does not work if SpoutPanel is in the Program Files folder without Admin privileges
 // SpoutPanel now always writes the selected sender name to the registry
 // so this first check should always work
 // Otherwise try the text file method
 // Find the path of the host program where SpoutPanel should have been copied
 // 24.11.15
 // Does the sender exist - if so register it
 // Register in the list of senders and make it the active sender
 // Do we have a sender name from the registry or a text file ?
 // Here we can test the active sender which should have been set by SpoutPanel
 // instead of depending on the registry flush which might have returned the old name
 // They should both be the same - so most reliable might be the active sender
 // different names
 // use the acitev sender
 // Does the sender exist ?
 // 31.10.17
 // 24.11.15 - not needed if the sender exists - and it is already checked as active
 // Register in the list of senders and make it the active sender
 // interop.senders.RegisterSenderName(newname);
 // interop.senders.SetActiveSender(newname);
 // will pass on next call to receivetexture
 // No name in registry or text file, so get the active sender which is set by spoutpanel
 // returns the active sender name
 // 31.10.17
 // will pass on next call to receivetexture
 // no active sender
 // no active sender or unregistered sender
 // endif SpoutPanel OK
 // got the exit code
 // endif no mutex so SpoutPanel has closed
 // SpoutPanel has not been opened
