Esempio n. 1
0
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPTSTR lpCmdLine, int nCmdShow)
{
  LogWriter preLog(0);

  // Life time of the sysLog must be greater than a TvnService object
  // because the crashHook uses it but fully functional usage possible
  // only after the TvnService object start.
  WinEventLogWriter winEventLogWriter(&preLog);
  CrashHook crashHook(&winEventLogWriter);

  ResourceLoader resourceLoaderSingleton(hInstance);

  CommandLineFormat format[] = {
    { TvnService::SERVICE_COMMAND_LINE_KEY, NO_ARG },

    { ControlCommandLine::CONFIG_APPLICATION, NO_ARG },
    { ControlCommandLine::CONFIG_SERVICE, NO_ARG },
    { ControlCommandLine::SET_CONTROL_PASSWORD, NO_ARG },
    { ControlCommandLine::SET_PRIMARY_VNC_PASSWORD, NO_ARG },
    { ControlCommandLine::CHECK_SERVICE_PASSWORDS, NO_ARG },
    { ControlCommandLine::CONTROL_SERVICE, NO_ARG },
    { ControlCommandLine::CONTROL_APPLICATION, NO_ARG },

    { DesktopServerCommandLine::DESKTOP_SERVER_KEY, NO_ARG },
    { QueryConnectionCommandLine::QUERY_CONNECTION, NO_ARG },

    { AdditionalActionApplication::LOCK_WORKSTATION_KEY, NO_ARG },
    { AdditionalActionApplication::LOGOUT_KEY, NO_ARG },

    { ServiceControlCommandLine::INSTALL_SERVICE },
    { ServiceControlCommandLine::REMOVE_SERVICE },
    { ServiceControlCommandLine::REINSTALL_SERVICE },
    { ServiceControlCommandLine::START_SERVICE },
    { ServiceControlCommandLine::STOP_SERVICE }
  };

  CommandLine parser;

  // We really don't care about parsing result, we only need the first specified key.

  StringStorage firstKey(_T(""));

  try {
    WinCommandLineArgs args(lpCmdLine);
    parser.parse(format,  sizeof(format) / sizeof(CommandLineFormat), &args);
  } 
  catch (...) {
  }
  parser.getOption(0, &firstKey);

  // Check if need to start additional application that packed into tvnserver.exe.

	if (firstKey.isEqualTo(ControlCommandLine::CONFIG_APPLICATION) ||
             firstKey.isEqualTo(ControlCommandLine::CONFIG_SERVICE) ||
             firstKey.isEqualTo(ControlCommandLine::SET_CONTROL_PASSWORD) ||
             firstKey.isEqualTo(ControlCommandLine::SET_PRIMARY_VNC_PASSWORD) ||
             firstKey.isEqualTo(ControlCommandLine::CONTROL_SERVICE) ||
             firstKey.isEqualTo(ControlCommandLine::CONTROL_APPLICATION) ||
             firstKey.isEqualTo(ControlCommandLine::CHECK_SERVICE_PASSWORDS)) {
    crashHook.setGuiEnabled();
    try {
      ControlApplication tvnControl(hInstance,
        WindowNames::WINDOW_CLASS_NAME,
        lpCmdLine);
      return tvnControl.run();
    } 
	catch (Exception &fatalException) {
      MessageBox(0,
        fatalException.getMessage(),
        StringTable::getString(IDS_MBC_TVNCONTROL),
        MB_OK | MB_ICONERROR);
      return 1;
    }
  } else if (firstKey.isEqualTo(AdditionalActionApplication::LOCK_WORKSTATION_KEY) ||
    firstKey.isEqualTo(AdditionalActionApplication::LOGOUT_KEY)) {
    crashHook.setGuiEnabled();
    try {
      AdditionalActionApplication actionApp(hInstance,
        WindowNames::WINDOW_CLASS_NAME,
        lpCmdLine);
      return actionApp.run();
    } catch (SystemException &ex) {
      return ex.getErrorCode();
    }
  } else if (firstKey.isEqualTo(DesktopServerCommandLine::DESKTOP_SERVER_KEY)) {
    try {
      crashHook.setHklmRoot();
      WinCommandLineArgs args(lpCmdLine);
      DesktopServerApplication desktopServerApp(hInstance,
        WindowNames::WINDOW_CLASS_NAME,
        &args);

      int retCode = desktopServerApp.run();
      return retCode;
    } catch (...) {
      return 1;
    }
  } else if (firstKey.isEqualTo(QueryConnectionCommandLine::QUERY_CONNECTION)) {
    crashHook.setGuiEnabled();
    try {
      QueryConnectionApplication app(hInstance,
        WindowNames::WINDOW_CLASS_NAME,
        lpCmdLine);
      return app.run();
    } catch (...) {
      return 1;
    }
  } else if (firstKey.isEqualTo(ServiceControlCommandLine::INSTALL_SERVICE) ||
             firstKey.isEqualTo(ServiceControlCommandLine::REMOVE_SERVICE) ||
             firstKey.isEqualTo(ServiceControlCommandLine::REINSTALL_SERVICE) ||
             firstKey.isEqualTo(ServiceControlCommandLine::START_SERVICE) ||
             firstKey.isEqualTo(ServiceControlCommandLine::STOP_SERVICE)) {
    crashHook.setGuiEnabled();
    ServiceControlApplication tvnsc(hInstance,
      WindowNames::WINDOW_CLASS_NAME,
      lpCmdLine);
    return tvnsc.run();
  }

	// No additional applications, run TightVNC server as single application.
	TvnService tvnService(&winEventLogWriter, &winEventLogWriter);
	try {
		crashHook.setHklmRoot();
		tvnService.run();
	} catch (Exception &) {
		return 1;
	}
	return 0;
}
Esempio n. 2
0
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                       LPTSTR lpCmdLine, int nCmdShow)
{
  ResourceLoader resourceLoaderSingleton(hInstance);

  CommandLineFormat format[] = {
    { TvnService::SERVICE_COMMAND_LINE_KEY, NO_ARG },

    { ControlCommandLine::CONFIG_APPLICATION, NO_ARG },
    { ControlCommandLine::CONFIG_SERVICE, NO_ARG },
    { ControlCommandLine::SET_CONTROL_PASSWORD, NO_ARG },
    { ControlCommandLine::SET_PRIMARY_VNC_PASSWORD, NO_ARG },
    { ControlCommandLine::CONTROL_SERVICE, NO_ARG },
    { ControlCommandLine::CONTROL_APPLICATION, NO_ARG },

    { DesktopServerCommandLine::DESKTOP_SERVER_KEY, NO_ARG },
    { QueryConnectionCommandLine::QUERY_CONNECTION, NO_ARG },

    { AdditionalActionApplication::LOCK_WORKSTATION_KEY, NO_ARG },
    { AdditionalActionApplication::LOGOUT_KEY, NO_ARG },

    { ServiceControlCommandLine::INSTALL_SERVICE },
    { ServiceControlCommandLine::REMOVE_SERVICE },
    { ServiceControlCommandLine::REINSTALL_SERVICE },
    { ServiceControlCommandLine::START_SERVICE },
    { ServiceControlCommandLine::STOP_SERVICE }
  };

  CommandLine parser;

  StringStorage firstKey(_T(""));

  parser.parse(format,  sizeof(format) / sizeof(CommandLineFormat), lpCmdLine);
  parser.getOption(0, &firstKey);

  if (firstKey.isEqualTo(TvnService::SERVICE_COMMAND_LINE_KEY)) {
    TvnService tvnService;
    try {
      tvnService.run();
    } catch (Exception &) {
      return 1;
    }
    return 0;
  } else if (firstKey.isEqualTo(ControlCommandLine::CONFIG_APPLICATION) ||
    firstKey.isEqualTo(ControlCommandLine::CONFIG_SERVICE) ||
    firstKey.isEqualTo(ControlCommandLine::SET_CONTROL_PASSWORD) ||
    firstKey.isEqualTo(ControlCommandLine::SET_PRIMARY_VNC_PASSWORD) ||
    firstKey.isEqualTo(ControlCommandLine::CONTROL_SERVICE) ||
    firstKey.isEqualTo(ControlCommandLine::CONTROL_APPLICATION)) {
    try {
      ControlApplication tvnControl(hInstance, lpCmdLine);

      return tvnControl.run();
    } catch (Exception &fatalException) {
      MessageBox(0,
        fatalException.getMessage(),
        StringTable::getString(IDS_MBC_TVNCONTROL),
        MB_OK | MB_ICONERROR);
      return 1;
    }
  } else if (firstKey.isEqualTo(AdditionalActionApplication::LOCK_WORKSTATION_KEY) ||
    firstKey.isEqualTo(AdditionalActionApplication::LOGOUT_KEY)) {
    try {
      AdditionalActionApplication actionApp(hInstance, lpCmdLine);

      return actionApp.run();
    } catch (SystemException &ex) {
      return ex.getErrorCode();
    }
  } else if (firstKey.isEqualTo(DesktopServerCommandLine::DESKTOP_SERVER_KEY)) {
    try {
      DesktopServerApplication desktopServerApp(hInstance, lpCmdLine);

      return desktopServerApp.run();
    } catch (...) {
      return 1;
    }
  } else if (firstKey.isEqualTo(QueryConnectionCommandLine::QUERY_CONNECTION)) {
    try {
      QueryConnectionApplication app(hInstance, lpCmdLine);

      return app.run();
    } catch (...) {
      return 1;
    }
  } else if (firstKey.isEqualTo(ServiceControlCommandLine::INSTALL_SERVICE) ||
    firstKey.isEqualTo(ServiceControlCommandLine::REMOVE_SERVICE) ||
    firstKey.isEqualTo(ServiceControlCommandLine::REINSTALL_SERVICE) ||
    firstKey.isEqualTo(ServiceControlCommandLine::START_SERVICE) ||
    firstKey.isEqualTo(ServiceControlCommandLine::STOP_SERVICE)) {
    ServiceControlApplication tvnsc(hInstance, lpCmdLine);

    return tvnsc.run();
  }

  TvnServerApplication tvnServer(hInstance, lpCmdLine);

  return tvnServer.run();
}