/************************************************************************
 *
 * Licensed Materials - Property of IBM.
 * (C) Copyright IBM Corporation 2003, 2012.  All Rights Reserved.
 * U.S. Government Users Restricted Rights:
 * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 *
 ************************************************************************/

#include "util.h"
#ifdef WNT
#define UNICODE
#define _UNICODE
#include <Windows.h>

typedef LONG (WINAPI * lpfnRegOpenKeyExW) (
  HKEY hKey,         // handle to open key
  LPCWSTR lpSubKey,  // subkey name
  DWORD ulOptions,   // reserved
  REGSAM samDesired, // security access mask
  PHKEY phkResult    // handle to open key
);

typedef LONG (WINAPI *lpfnRegQueryValueExW) (
  HKEY hKey,            // handle to key
  LPCWSTR lpValueName,  // value name
  LPDWORD lpReserved,   // reserved
  LPDWORD lpType,       // type buffer
  LPBYTE lpData,        // data buffer
  LPDWORD lpcbData      // size of data buffer
);

typedef LONG (WINAPI *lpfnRegCloseKey) (
  HKEY hKey           // handle to key
);


#endif

bool launchSymphony()
{
	
	
#ifdef WNT
	//search the regstry and execute rcplauncher
	//HKEY_CLASSES_ROOT\CLSID\{82154420-0FBF-11d4-8313-005004526AB4}\LocalServer32
	long lRet;
	HKEY hKey;
	WCHAR tchData[512];
	DWORD dwSize;
	HMODULE hMod = ::LoadLibrary(L"advapi32.dll");
	if(! hMod ) 
	{
		FreeLibrary( hMod );
		return false;
	}
	lpfnRegOpenKeyExW fnRegOpenKeyExW= (lpfnRegOpenKeyExW) GetProcAddress(hMod, "RegOpenKeyExW" );
	if( !fnRegOpenKeyExW)
	{
		FreeLibrary( hMod );
		return false;
	}
	
	lRet = fnRegOpenKeyExW(	HKEY_CLASSES_ROOT, L"CLSID\\{82154420-0FBF-11d4-8313-005004526AB4}\\LocalServer32", 0, KEY_QUERY_VALUE, &hKey );

	if(lRet != ERROR_SUCCESS)
	{
		FreeLibrary( hMod );
		return false;
	}

	lpfnRegQueryValueExW fnRegQueryValueExW=(lpfnRegQueryValueExW) GetProcAddress(hMod, "RegQueryValueExW" );
	if( !fnRegQueryValueExW)
	{
		FreeLibrary( hMod );
		return false;
	}
	
	dwSize = sizeof(tchData);

	lRet = fnRegQueryValueExW( hKey, L"",  NULL,  NULL, (LPBYTE)tchData, &dwSize );

	lpfnRegCloseKey fnRegCloseKey=(lpfnRegCloseKey) GetProcAddress(hMod, "RegCloseKey" );
	if( fnRegCloseKey)
		fnRegCloseKey(hKey);

	if(lRet != ERROR_SUCCESS)
	{
		FreeLibrary( hMod );
		return false;
	}

	FreeLibrary( hMod );

	STARTUPINFO si;
	PROCESS_INFORMATION pi;
	ZeroMemory(&si,sizeof(si));
	ZeroMemory(&pi,sizeof(pi));
	si.dwFlags = CREATE_NO_WINDOW;
	si.wShowWindow = SW_HIDE;
	DWORD createFlag = CREATE_NO_WINDOW;
		
	::CreateProcess(NULL, tchData,NULL,NULL,TRUE,createFlag,NULL,NULL,&si,&pi);

	WaitForSingleObject(   pi.hProcess,   60000   );

	CloseHandle( pi.hProcess );
	CloseHandle( pi.hThread );
#endif
#ifdef UNX

#endif
	return true;
}

