Exemple #1
0
bool CApp::Open()
{
	// If not set yet, default title to application filename.
	if (m_strTitle.Empty())
		m_strTitle = CPath::Application().FileName().ToUpper();

	const DWORD dwMinMajor = 4;
	const DWORD dwMinMinor = 71;

	DWORD dwMajor, dwMinor;

	// Get COMCTL32.DLL version.
	if (!m_oComCtl32.IsLoaded() || !m_oComCtl32.GetVersion(dwMajor, dwMinor))
	{
		FatalMsg(TXT("This application requires at least v%u.%u of COMCTL32.DLL."), dwMinMajor, dwMinMinor);
		return false;
	}

	// Check COMCTL32.DLL version.
	if ( (dwMajor < dwMinMajor) || ((dwMajor == dwMinMajor) && (dwMinor < dwMinMinor)) )
	{
		FatalMsg(TXT("This application requires at least v%u.%u of COMCTL32.DLL."), dwMinMajor, dwMinMinor);
		return false;
	}

	DWORD dwICC = ICC_DATE_CLASSES | ICC_LISTVIEW_CLASSES | ICC_PROGRESS_CLASS | ICC_TAB_CLASSES;

	// Initialise COMCTL32.DLL window classes.
	if (!m_oComCtl32.Initialise(dwICC))
	{
		FatalMsg(TXT("Failed to initialise COMCTL32.DLL"));
		return false;
	}

	// Call application method.
	if (!OnOpen())
		return false;

	return true;
}
void CNetDDESvrApp::OnClosed(CSocket* pSocket, int /*nReason*/)
{
	// NetDDE connection?
	if (pSocket != &m_oSvrSocket)
	{
		CNetDDESvrSocket* pConnection = static_cast<CNetDDESvrSocket*>(pSocket);

		if (App.m_bTraceNetConns)
			App.Trace(TXT("SOCKET_STATUS: Connection closed from %s"), pConnection->Host());
	}
	// Listening socket.
	else // (pSocket == &m_oSvrSocket)
	{
		FatalMsg(TXT("Server listening socket closed."));

		m_AppWnd.Destroy();
	}

	// Cleanup later.
	m_MainThread.PostMessage(WM_POLL_SOCKETS);
}
bool CNetDDESvrApp::OnOpen()
{
	HWND hPrevWnd = NULL;

	// Only allow a single instance.
	if ((hPrevWnd = ::FindWindow(CAppWnd::WNDCLASS_NAME, NULL)) != NULL)
	{
		// If not visible OR minimised, restore it.
		if (!::IsWindowVisible(hPrevWnd) || ::IsIconic(hPrevWnd))
		{
			::ShowWindow(hPrevWnd, SW_RESTORE);
			::SetForegroundWindow(hPrevWnd);
		}

		return false;
	}

	// Set the app title.
	m_strTitle = TXT("NetDDE Server");

	// Load settings.
	LoadConfig();

	// Create the full trace file path.
	m_strTracePath = CPath(CPath::ApplicationDir(), m_strTraceFile);

	// Clear the trace file.
	if (m_bTraceToFile)
	{
		try
		{
			m_fTraceFile.Create(m_strTracePath);
			m_fTraceFile.Close();
		}
		catch (CFileException& e)
		{
			AlertMsg(TXT("Failed to truncate trace file:\n\n%s"), e.twhat());

			m_bTraceToFile = false;
		}
	}

	try
	{
		// Initialise WinSock.
		int nResult = CWinSock::Startup(1, 1);

		if (nResult != 0)
		{
			FatalMsg(TXT("Failed to initialise WinSock layer: %d."), nResult);
			return false;
		}

		// Initialise the DDE client.
		m_pDDEClient = DDE::ClientPtr(new CDDEClient);
		m_pDDEClient->AddListener(this);

		// Open the listening socket.
		m_oSvrSocket.Listen(m_nServerPort);

		// Attach event handler.
		m_oSvrSocket.AddServerListener(this);
	}
	catch (Core::Exception& e)
	{
		FatalMsg(TXT("%s"), e.twhat());
		return false;
	}

	// Create the main window.
	if (!m_AppWnd.Create())
		return false;

	// Show it.
	if (!m_rcLastPos.Empty())
		m_AppWnd.Move(m_rcLastPos);

	m_AppWnd.Show(m_iCmdShow);

	// Update UI.
	m_AppCmds.UpdateUI();

	// Start the background timer.
	m_nTimerID = StartTimer(BG_TIMER_FREQ);

	App.Trace(TXT("SERVER_STATUS: Server started"));
	App.Trace(TXT("SERVER_STATUS: Server listening on port: %d"), m_nServerPort);

	return true;
}