Ejemplo n.º 1
0
size_t CIniFile::ReadSection(const tchar* pszSection, CStrArray& astrEntries)
{
	ASSERT(pszSection);

	// Allocate initial buffer.
	size_t nChars     = 1024;
	tchar* pszEntries = static_cast<tchar*>(alloca(Core::numBytes<tchar>(nChars+1)));

	// Read all entries, reallocating if necessary...
	while (::GetPrivateProfileSection(pszSection, pszEntries, static_cast<DWORD>(nChars), m_strPath) >= (nChars-2))
	{
		// Double the buffer size.
		nChars    *= 2;
		pszEntries = static_cast<tchar*>(alloca(Core::numBytes<tchar>(nChars+1)));
	}

	// For all strings.
	while (*pszEntries != TXT('\0'))
	{
		astrEntries.Add(pszEntries);
		pszEntries += tstrlen(pszEntries) + 1;
	}

	return astrEntries.Size();
}
Ejemplo n.º 2
0
void CLinkCache::Purge(const CDDEConv* pConv)
{
	typedef CLinksMap::iterator LinksIter;

	CStrArray astrLinks;

	// Format the cache entry prefix for the conversation.
	CString strPrefix = CString::Fmt(TXT("%s|%s!"), pConv->Service().c_str(), pConv->Topic().c_str());
	size_t  nLength   = strPrefix.Length();

	// Find all links for the conversation...
	for (LinksIter it = m_oLinks.begin(); it != m_oLinks.end(); ++it)
	{
		const CString& strLink = it->first;

		if (tstrnicmp(strLink, strPrefix, nLength) == 0)
		{
			// Delete value, but remember key.
			astrLinks.Add(strLink);
			delete it->second;
		}
	}

	// Purge all matching links...
	for (size_t i = 0; i < astrLinks.Size(); ++i)
		m_oLinks.erase(astrLinks[i]);
}
Ejemplo n.º 3
0
void MockDDEClient::QueryAll(CStrArray& servers, CStrArray& topics) const
{
	typedef ServiceTopics::const_iterator const_iter;

	for (const_iter it = m_runningServers.begin(); it != m_runningServers.end(); ++it)
	{
		ServiceTopicPtr conversation = *it;

		servers.Add(conversation->m_server.c_str());
		topics.Add(conversation->m_topic.c_str());
	}
}
Ejemplo n.º 4
0
bool ListenSink::OnWildConnect(CStrArray& services, CStrArray& topics)
{
	m_out << TXT("XTYP_WILDCONNECT")
	      << std::endl;

	services.Add(m_server.c_str());
	topics.Add(!m_topic.empty() ? m_topic.c_str() : TXT("*"));

//	if (m_delay != 0)
//		::Sleep(m_delay);

	return true;
}
Ejemplo n.º 5
0
void CDDEClient::QueryAll(CStrArray& astrServers, CStrArray& astrTopics) const
{
	// Query all servers.
	HCONVLIST hList = ::DdeConnectList(m_dwInst, NULL, NULL, NULL, nullptr);

	// Failed?
	if (hList == NULL)
		throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());

	HCONV hConv = NULL;

	// For all servers & topics...
	while ((hConv = ::DdeQueryNextServer(hList, hConv)) != NULL)
	{
		CONVINFO oConvInfo = { sizeof(oConvInfo), 0 };

		// Query server details.
		if (!::DdeQueryConvInfo(hConv, QID_SYNC, &oConvInfo))
		{
			::DdeDisconnectList(hList);
			throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());
		}

		tchar szServer[MAX_SERVER_LEN+1] = { 0 };

		// Get the server name.
		if (!::DdeQueryString(m_dwInst, oConvInfo.hszSvcPartner, szServer, MAX_SERVER_LEN, CP_WIN_TCHAR))
		{
			::DdeDisconnectList(hList);
			throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());
		}

		tchar szTopic[MAX_TOPIC_LEN+1] = { 0 };

		// Get the topic name.
		if (!::DdeQueryString(m_dwInst, oConvInfo.hszTopic, szTopic, MAX_TOPIC_LEN, CP_WIN_TCHAR))
		{
			::DdeDisconnectList(hList);
			throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());
		}

		// Add to the collections.
		astrServers.Add(szServer);
		astrTopics.Add(szTopic);
	}

	// Free connection list.
	::DdeDisconnectList(hList);
}
Ejemplo n.º 6
0
size_t CIniFile::ReadSection(const tchar* pszSection, CStrArray& astrKeys, CStrArray& astrValues)
{
	ASSERT(pszSection);

	CStrArray astrEntries;

	// Read all the entries...
	if (ReadSection(pszSection, astrEntries))
	{
		// Split all entries.
		for (size_t i = 0; i < astrEntries.Size(); ++i)
		{
			// Split into key and value.
			CString strEntry = astrEntries[i];
			size_t  nLength  = strEntry.Length();
			size_t  nSepPos  = strEntry.Find(TXT('='));

			// Key set AND value set?
			if ( (nSepPos > 0) && ((nLength-nSepPos-1) > 0) )
			{
				astrKeys.Add(strEntry.Left(nSepPos));
				astrValues.Add(strEntry.Right(nLength-nSepPos-1));
			}
		}
	}

	ASSERT(astrKeys.Size() == astrValues.Size());

	return astrKeys.Size();
}
Ejemplo n.º 7
0
void CDDEClient::QueryServerTopics(const tchar* pszServer, CStrArray& astrTopics) const
{
	ASSERT(pszServer != nullptr);

	CDDEString strServer(g_pDDEClient, pszServer);

	// Query all servers.
	HCONVLIST hList = ::DdeConnectList(m_dwInst, strServer, NULL, NULL, nullptr);

	// Failed?
	if (hList == NULL)
		throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());

	HCONV hConv = NULL;

	// For all servers...
	while ((hConv = ::DdeQueryNextServer(hList, hConv)) != NULL)
	{
		CONVINFO oConvInfo = { sizeof(oConvInfo), 0 };

		// Query server details.
		if (!::DdeQueryConvInfo(hConv, QID_SYNC, &oConvInfo))
		{
			::DdeDisconnectList(hList);
			throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());
		}

		tchar szServer[MAX_SERVER_LEN+1] = { 0 };

		// Get the server name.
		// NB: Some servers will return all service names regardless.
		if (!::DdeQueryString(m_dwInst, oConvInfo.hszSvcPartner, szServer, MAX_SERVER_LEN, CP_WIN_TCHAR))
		{
			::DdeDisconnectList(hList);
			throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());
		}

		// Not the server we're after?
		if (tstricmp(szServer, pszServer) != 0)
			continue;

		tchar szTopic[MAX_TOPIC_LEN] = { 0 };

		// Get the topic name.
		if (!::DdeQueryString(m_dwInst, oConvInfo.hszTopic, szTopic, MAX_TOPIC_LEN, CP_WIN_TCHAR))
		{
			::DdeDisconnectList(hList);
			throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());
		}

		astrTopics.Add(szTopic);
	}

	// Free connection list.
	::DdeDisconnectList(hList);
}
Ejemplo n.º 8
0
void CDDEClient::QueryServers(CStrArray& astrServers) const
{
	// Query all servers.
	HCONVLIST hList = ::DdeConnectList(m_dwInst, NULL, NULL, NULL, nullptr);

	// Failed?
	if (hList == NULL)
		throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());

	HCONV hConv = NULL;

	// For all servers...
	while ((hConv = ::DdeQueryNextServer(hList, hConv)) != NULL)
	{
		CONVINFO oConvInfo = { sizeof(oConvInfo), 0 };

		// Query server details.
		if (!::DdeQueryConvInfo(hConv, QID_SYNC, &oConvInfo))
		{
			::DdeDisconnectList(hList);
			throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());
		}

		const size_t MAX_LEN = 256;

		tchar szServer[MAX_LEN];

		// Get the server name.
		if (!::DdeQueryString(m_dwInst, oConvInfo.hszSvcPartner, szServer, MAX_LEN, CP_WIN_TCHAR))
		{
			::DdeDisconnectList(hList);
			throw CDDEException(CDDEException::E_QUERY_FAILED, LastError());
		}

		// Add, if not a duplicate.
		if (astrServers.Find(szServer, true) == Core::npos)
			astrServers.Add(szServer);
	}

	// Free connection list.
	::DdeDisconnectList(hList);
}
Ejemplo n.º 9
0
void CODBCSource::InstalledSources(CStrArray& astrSources)
{
	const size_t MAX_DSN_LEN  = 256;
	const size_t MAX_DESC_LEN = 256;

	SQLHENV		hEnv = SQL_NULL_HENV;
	SQLRETURN	rc;
	SQLTCHAR	szDSN[MAX_DSN_LEN+1] = { 0 };
	SQLTCHAR	szDesc[MAX_DESC_LEN+1] = { 0 };
	SQLSMALLINT	nDSNSize  = MAX_DSN_LEN;
	SQLSMALLINT	nDescSize = MAX_DESC_LEN;
	SQLSMALLINT	nDSNRetSize;
	SQLSMALLINT	nDescRetSize;

	// Allocate an environment handle.
	rc = ::SQLAllocHandle(SQL_HANDLE_ENV, NULL, &hEnv);

	if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) )
		throw CODBCException(CODBCException::E_ENUMINFO_FAILED, TXT("SQLAllocHandle"), NULL, 0);

	// Say we're ODBC v3.x compliant.
	rc = ::SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, reinterpret_cast<SQLPOINTER>(SQL_OV_ODBC3), SQL_IS_INTEGER);

	if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) )
		throw CODBCException(CODBCException::E_CONNECT_FAILED, TXT("SQLSetEnvAttr"), hEnv, SQL_HANDLE_ENV);

	// Fetch the first data source.
	rc = ::SQLDataSources(hEnv, SQL_FETCH_FIRST, szDSN, nDSNSize, &nDSNRetSize, szDesc, nDescSize, &nDescRetSize);

	if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) && (rc != SQL_NO_DATA) )
	{
		::SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
		throw CODBCException(CODBCException::E_ENUMINFO_FAILED, TXT("SQLDataSources"), hEnv, SQL_HANDLE_ENV);
	}

	// For all sources.
	while (rc != SQL_NO_DATA)
	{
		astrSources.Add(reinterpret_cast<tchar*>(szDSN));

		rc = ::SQLDataSources(hEnv, SQL_FETCH_NEXT, szDSN, nDSNSize, &nDSNRetSize, szDesc, nDescSize, &nDescRetSize);

		if ( (rc != SQL_SUCCESS) && (rc != SQL_SUCCESS_WITH_INFO) && (rc != SQL_NO_DATA) )
		{
			::SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
			throw CODBCException(CODBCException::E_ENUMINFO_FAILED, TXT("SQLDataSources"), hEnv, SQL_HANDLE_ENV);
		}
	}

	// Free environment.
	::SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
}
Ejemplo n.º 10
0
bool ListenSink::OnWildConnectTopic(const tchar* topic, CStrArray& services)
{
	m_out << TXT("XTYP_WILDCONNECT: '")
	      << topic << TXT("'")
	      << std::endl;

	services.Add(m_server.c_str());

//	if (m_delay != 0)
//		::Sleep(m_delay);

	return true;
}
Ejemplo n.º 11
0
bool ListenSink::OnWildConnectService(const tchar* service, CStrArray& topics)
{
	m_out << TXT("XTYP_WILDCONNECT: '")
	      << service << TXT("'")
	      << std::endl;

	topics.Add(!m_topic.empty() ? m_topic.c_str() : TXT("*"));

//	if (m_delay != 0)
//		::Sleep(m_delay);

	return true;
}
Ejemplo n.º 12
0
int
CIniFile::Split( const CStr &source, LPCTSTR sep, CStrArray &dest, BOOL trim /* = TRUE */ )
{
	int pos = source.Find( sep );
	int startPos = 0;
	CStr elem;
	int sepLen = wcslen( sep );

	dest.RemoveAll();
	while( pos != -1 )
	{
		elem = source.Mid( startPos, pos-startPos );
        if ( trim ) { elem.TrimLeft(); elem.TrimRight(); }
		dest.Add( elem );

		startPos = pos+sepLen;
		pos      = source.Find( sep, startPos );
	}
	elem = source.Mid( startPos );
    if ( trim ) { elem.TrimLeft(); elem.TrimRight(); }
	dest.Add( elem );

	return dest.GetSize();
}
Ejemplo n.º 13
0
void CIniFile::WriteStrings(const tchar* pszSection, const tchar* pszEntry, tchar cSep, const CStrArray& astrValues)
{
	ASSERT(pszSection != nullptr);
	ASSERT(pszEntry   != nullptr);

	CString str;

	// Build string list.
	for (size_t i = 0; i < astrValues.Size(); ++i)
	{
		if (i != 0)
			str += cSep;

		str += astrValues[i];
	}

	WriteString(pszSection, pszEntry, str);
}
Ejemplo n.º 14
0
void CODBCSource::InstalledDrivers(CStrArray& astrDrivers)
{
	size_t nChars    = 1024;
	WORD   wRetSize  = 0;
	tchar* pszBuffer = nullptr;

	// Until buffer big enough.
	for(;;)
	{
		size_t nBytes = Core::numBytes<tchar>(nChars);

		pszBuffer = static_cast<tchar*>(alloca(nBytes));

		// Try query.
		if (!::SQLGetInstalledDrivers(pszBuffer, static_cast<WORD>(nBytes), &wRetSize))
			throw CODBCException(CODBCException::E_ENUMINFO_FAILED, TXT("SQLGetInstalledDrivers"), NULL, 0);

		// Buffer big enough?
		if (wRetSize < nChars)
			break;

		// Double buffer size, and try again.
		nChars *= 2;
	}

	ASSERT(pszBuffer != nullptr);

	tchar* pszName = pszBuffer;

	// Extract drivers.
	while (*pszName != TXT('\0'))
	{
		astrDrivers.Add(pszName);

		while (*pszName++ != TXT('\0'));
	}
}
Ejemplo n.º 15
0
void CDlgInput::OnSize(UINT nType, int cx, int cy) 
{
	CDialog::OnSize(nType, cx, cy);

	if ( GetDlgItem(IDC_LABEL) == NULL || ! ::IsWindow( GetDlgItem(IDC_LABEL)->m_hWnd ) ) return;

	CRect pos;
    int   infoBottom;

	GetDlgItem( IDC_LABEL )->GetWindowRect( &pos );
	ScreenToClient( pos );
	if ( m_Label.IsEmpty() )
	{
		GetDlgItem( IDC_LABEL )->ShowWindow( SW_HIDE );
		infoBottom = 0;
	}
	else
	{
		HDC dc = ::GetWindowDC( GetDlgItem(IDC_LABEL)->m_hWnd );
		CStrArray lines;
		if ( m_Label.IsEmpty() )
		{
			lines.Add( L"" );
			GetDlgItem( IDC_LABEL )->SetWindowText(L"");
		}
		else
		{
			SplitLines( dc, (LPCTSTR)m_Label, lines, pos.Width() );
			CString infoWrapped;
			for ( int i=0; i<lines.GetSize(); i++ )
			{
				infoWrapped += lines[i];
				if ( i<lines.GetSize()-1 ) infoWrapped += "\n";
			}
			GetDlgItem( IDC_LABEL )->SetWindowText(infoWrapped);
			//UpdateData(FALSE);
		}
		SIZE size;
		GetTextExtentPoint( dc, L"W", 1, &size );
		pos.top   = 4;
		pos.left  = 4;
		pos.right = cx - 8;
		infoBottom = pos.bottom = pos.top + size.cy * lines.GetSize();
		::ReleaseDC( GetDlgItem(IDC_LABEL)->m_hWnd, dc );
		GetDlgItem( IDC_LABEL )->MoveWindow( pos );
		GetDlgItem( IDC_LABEL )->Invalidate();
	}


	//GetDlgItem(IDC_LABEL)->GetWindowRect( &pos );
	//ScreenToClient( &pos );
	//GetDlgItem(IDC_LABEL)->MoveWindow( &pos );

	if ( Multiline )
	{
		int height;

		GetDlgItem(IDOK)->GetWindowRect( &pos );
		ScreenToClient( &pos );
		height = pos.bottom-pos.top;
		pos.top = cy - height - 4;
		pos.bottom = cy - 4;
		pos.left  = 4;
		pos.right = cx/2 - 4;
		GetDlgItem(IDOK)->MoveWindow( &pos );

		GetDlgItem(IDCANCEL)->GetWindowRect( &pos );
		ScreenToClient( &pos );
		pos.top = cy - height - 4;
		pos.bottom = cy - 4;
		pos.left = cx/2 + 4;
		pos.right = cx - 8;
		GetDlgItem(IDCANCEL)->MoveWindow( &pos );

		GetDlgItem(IDC_EDIT)->GetWindowRect( &pos );
		ScreenToClient( &pos );
		pos.top   = infoBottom + 4;
		pos.bottom = cy - height - 8;
		pos.left  = 4;
		pos.right = cx - 8;
		GetDlgItem(IDC_EDIT)->MoveWindow( &pos );
	}
	else
	{
		int height;

		GetDlgItem(IDC_EDIT)->GetWindowRect( &pos );
		ScreenToClient( &pos );
		height = pos.Height();
		pos.top   = infoBottom + 4;
		pos.bottom = pos.top + height;
		pos.left  = 4;
		pos.right = cx - 8;
		GetDlgItem(IDC_EDIT)->MoveWindow( &pos );
		infoBottom = pos.bottom;

		((CEdit*)GetDlgItem(IDC_EDIT))->ModifyStyle( ES_MULTILINE|ES_WANTRETURN|WS_VSCROLL, 0 );
#ifndef DESKTOP
		OSVERSIONINFO ver;
		GetVersionEx( &ver );
		if ( ver.dwMajorVersion < 5 )
		{
#endif
		GetDlgItem(IDOK)->GetWindowRect( &pos );
		ScreenToClient( &pos );
		height = pos.bottom-pos.top;
		pos.top   = infoBottom + 4;
		pos.bottom = pos.top + height;
		pos.left  = 4;
		pos.right = cx/2 - 4;
		GetDlgItem(IDOK)->MoveWindow( &pos );

		GetDlgItem(IDCANCEL)->GetWindowRect( &pos );
		ScreenToClient( &pos );
		pos.top   = infoBottom + 4;
		pos.bottom = pos.top + height;
		pos.left = cx/2 + 4;
		pos.right = cx - 8;
		GetDlgItem(IDCANCEL)->MoveWindow( &pos );
#ifndef DESKTOP
		}
		else
		{
			GetDlgItem( IDOK )->ShowWindow( SW_HIDE );
			GetDlgItem( IDCANCEL )->ShowWindow( SW_HIDE );
		}
#endif


		//((CEdit*)GetDlgItem(IDC_EDIT))->ModifyStyleEx( 0, ES_MULTILINE|ES_WANTRETURN|WS_VSCROLL );

#ifdef DESKTOP
		CRect client;
		GetWindowRect( &pos );
		GetClientRect( &client );
		if ( infoBottom + height + 8 != client.Height() )
		{
			pos.bottom = pos.top + pos.Height() - client.Height() + infoBottom + height + 8;
			MoveWindow( &pos );
		}
#endif
	}
	
	((CEdit*)GetDlgItem(IDC_EDIT))->SetSel(0,0,FALSE);	
}
Ejemplo n.º 16
0
BOOL CMortScriptApp::InitInstance()
{
    SetRegistryKey( L"Mort" );

	CParseCmdLine myCmdLine;
	myCmdLine.ParseCmdLine( m_lpCmdLine );

	TCHAR exe[MAX_PATH];
   ::GetModuleFileName(NULL,exe,MAX_PATH);

	ExecuteFileName = exe;

    CStr file = myCmdLine.Filename; // m_lpCmdLine;
    // file.TrimLeft( '\"' ); file.TrimRight( '\"' );

    if ( myCmdLine.RegOnly || file.IsEmpty() )
    {
        RegisterFileClass();
        RegisterFileType( L".jscr", L"JScripts" );
        RegisterFileType( L".mscr", L"JScripts" );
		if ( !myCmdLine.RegOnly )
		{
			MessageBox( NULL
					  , L".jscr and .mscr extensions registered.\nPlease run any .jscr/.mscr file or read the manual.\n\n"
						L"(c) Mirko Schenk 2005-2007"
					  , L"JScripts V" + CStr( VERSION_INFO )
					  , MB_OK|MB_SETFOREGROUND );
		}
    }
	else
	{
		if (   file.GetLength() >= 4
			/* file.GetLength() >= 8 && file.Right(8).CompareNoCase( L".mortrun" ) == 0
			|| file.GetLength() >= 5 && file.Right(5).CompareNoCase( L".mscr" ) == 0 */
		   )
		{
            CStr mutexName = file;
            mutexName.MakeLower();
#ifdef DESKTOP
			// Windows XP doesn't like some path characters in the mutex' name
			mutexName.Replace( ':', '_' );
			mutexName.Replace( '\\', '/' );
#endif
          MutexName = (LPCTSTR)mutexName;

	        HANDLE mutex = ::CreateMutex(NULL, FALSE, MutexName);

            if ( mutex!=NULL )
	        {
				int exists = ::GetLastError();
		        if ( exists == ERROR_ALREADY_EXISTS) 
		        {
					DWORD procId = GetRunningScriptProcId( file );

					if ( procId != NULL )
					{
						/*
						 CString msg;
						 msg.Format( L"Process ID: %08x", procId );
						 MessageBox( NULL, msg, L"Debug", MB_SETFOREGROUND );
						*/

						FindAppT findApp;
						findApp.procId = procId;
						findApp.hWnd   = NULL;
						::EnumWindows( FindApplicationWindowProc, (LPARAM)&findApp );
						if ( findApp.hWnd != NULL )
						{
							// msg.Format( L"Set foreground window: %08x", findApp.hWnd );
							// MessageBox( NULL, msg, L"Debug", MB_SETFOREGROUND );
							::SetForegroundWindow( findApp.hWnd );
						}
					}
					else
						exists = 0;

					//MessageBox( NULL, L"Process opened", L"Debug", MB_SETFOREGROUND );
					/*
						TCHAR procName[256];
				    	::GetModuleFileName((HMODULE)procId,procName,256);
						//MessageBox( NULL, procName, L"Debug", MB_SETFOREGROUND );
						if ( CString(procName).Right(14).CompareNoCase( L"MortScript.exe" ) == 0 )
						{
							int aw = MessageBox( NULL
											   , L"Script seems to be running. Cancel old script?"
											   , L"Script already running"
											   , MB_YESNO|MB_SETFOREGROUND
											   );
							if ( aw == IDYES )
							{
								RegWriteDW( HKEY_CURRENT_USER, L"Software\\JScripts\\Abort", MutexName, 1 );
								DWORD exitCode = 0;
								SetCursor(LoadStandardCursor(IDC_WAIT));
								for ( int i=0; i<=10; i++ )
								{
									Sleep(1000);
									if ( GetExitCodeProcess( hProc, &exitCode ) == FALSE )
									{
										//MessageBox( NULL, L"GetExitCode failed", L"Debug", MB_SETFOREGROUND );
										exitCode = 0;
										break;
									}
									else
									{
										if ( exitCode != STILL_ACTIVE )
										{
											//MessageBox( NULL, L"No longer active", L"Debug", MB_SETFOREGROUND );
											break;
										}
									}
								}
								SetCursor(LoadStandardCursor(IDC_ARROW)); 
								if ( exitCode == STILL_ACTIVE )
								{
									int aw = MessageBox( NULL
													   , L"Script seems to be hanging or busy. Terminate old script?"
													   , L"Script still running"
													   , MB_YESNO|MB_SETFOREGROUND
													   );
									if ( aw == IDYES )
									{
										TerminateProcess( hProc, 0 );
									}
								}
							}
						}
						else
						{
							exists = 0;
						}

						CloseHandle( hProc );
					}
					else
					{
						exists = 0;
					}
					*/
                }

                if ( exists != ERROR_ALREADY_EXISTS )
                {
	                HKEY    key;
	                if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, 0, &key ) == ERROR_SUCCESS )
	                {
                        RegDeleteValue( key, MutexName );
		                RegCloseKey( key );
	                }

					DWORD currProcId = GetCurrentProcessId();
					//CString dbg;
					//dbg.Format(L"ProcId: %d", GetCurrentProcessId());
					//MessageBox( NULL, dbg, L"Debug", MB_SETFOREGROUND );

					// Remove old script entries with same process id
	                if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, 0, &key ) == ERROR_SUCCESS )
	                {
						int   idx;
						TCHAR valName[MAX_PATH];
						DWORD valSize = MAX_PATH, type;
						DWORD value, valueSize = sizeof(DWORD);

						CStrArray oldProcesses;
						for ( idx = 0; RegEnumValue( key, idx, valName, &valSize, NULL, &type, (BYTE*)&value, &valueSize ) == ERROR_SUCCESS; idx++ )
						{
							if ( type == REG_DWORD && value == currProcId )
							{
								oldProcesses.Add( valName );
							}
							valSize = MAX_PATH;
							valueSize = sizeof(DWORD);
						}

						for ( idx = 0; idx < oldProcesses.GetSize(); idx++ )
						{
							RegDeleteValue( key, oldProcesses.GetAt(idx) );
						}

		                RegCloseKey( key );
					}

					RegWriteDW( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", MutexName, currProcId );

					if ( myCmdLine.WaitForFile > 0 && (long)FileOrDirExists( file, 0 ) == 0 )
					{
						CDlgWait wait(NULL);
						wait.AllowOK = TRUE;
						wait.Countdown = myCmdLine.WaitForFile;
						wait.Expression = L"FileExists(\"" + file + L"\")";
						wait.Title = L"JScripts";
						wait.m_Label = L"Script " + file + " not found, waiting for existance (storage card not initialized?)";
						wait.DoModal();
					}

					if ( myCmdLine.WaitForFile <= 0 || (long)FileOrDirExists( file, 0 ) == 1 )
					{
						g_hInst = theApp.m_hInstance;

						SYSTEMTIME now;
						GetLocalTime( &now );
						long seed = SystemTimeToUnixTime( now );
						srand( seed );

						AppPath = m_pszHelpFilePath;
						AppPath = AppPath.Left( AppPath.ReverseFind('\\') );

						ScriptAborted = CreateEvent( 0, TRUE, FALSE, CStr(MutexName)+L"ScriptAborted" );
					    StatusDialog = new CDlgStatus( file );
						//status.DoModal();

						CInterpreter interpreter;
						//Debug(file);

						//jwz:modi
						interpreter.RunFile( file );
						interpreter.Parser();
						//jwz:modi end

						if ( StatusWindow != NULL )
						{
							StatusDialog->ScriptFinished = TRUE;
							SendMessage( StatusWindow, WM_EXIT_STATUS, 0, 0 );
							WaitForSingleObject( StatusDialogFinished, INFINITE );
							CloseHandle( StatusDialogFinished );
						}

						delete StatusDialog;

						UnloadToolhelp();

						CloseHandle( ScriptAborted );
						ReleaseMutex( mutex );

						Variables.RemoveAll();
						for ( int i = 0; i<LocalVariablesStack.GetSize(); i++ )
							delete (CMapStrToValue*)LocalVariablesStack.GetAt(i);

						POSITION pos = FileHandles.GetStartPosition();
						CStr key; void *value;
						while ( pos != 0 )
						{
							FileHandles.GetNextAssoc( pos, key, value );
							if ( value != NULL )
							{
								delete (CFileInfo*)value;
							}
						}
						FileHandles.RemoveAll();

						if ( ChoiceFont != NULL ) DeleteObject( ChoiceFont );
						if ( StatusListFont != NULL ) DeleteObject( StatusListFont );
					}

					if ( RegOpenKeyEx( HKEY_CURRENT_USER, L"Software\\JScripts\\Processes", 0, KEY_WRITE, &key ) == ERROR_SUCCESS )
	                {
                        RegDeleteValue( key, MutexName );
		                RegCloseKey( key );
	                }
                }
                CloseHandle( mutex );
            }
			else
			{
				int error = ::GetLastError();
				MessageBox( NULL
						  , L"Error creating mutex"
						  , L"JScripts"
						  , MB_OK|MB_SETFOREGROUND );
			}
		}
		else
		{
			MessageBox( NULL
					  , L"Invalid file type for MortScript"
					  , L"JScripts"
					  , MB_OK|MB_SETFOREGROUND );
		}
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}