From 6ef744cd818a03d61dcbd92f25bee258db8ad7b5 Mon Sep 17 00:00:00 2001 From: Xinyu Hou Date: Tue, 11 Nov 2014 14:08:55 +0000 Subject: [PATCH] Added code to check if Bonjour service is running --- src/gui/src/MainWindow.cpp | 45 ++++++++++++++++++++++++++++++++++++-- src/gui/src/MainWindow.h | 2 ++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/gui/src/MainWindow.cpp b/src/gui/src/MainWindow.cpp index 4c3a33a7..c416c25c 100644 --- a/src/gui/src/MainWindow.cpp +++ b/src/gui/src/MainWindow.cpp @@ -75,7 +75,8 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) : m_pMenuEdit(NULL), m_pMenuWindow(NULL), m_pMenuHelp(NULL), - m_pZeroconfService(NULL) + m_pZeroconfService(NULL), + m_BonjourRunning(true) { setupUi(this); @@ -105,7 +106,13 @@ MainWindow::MainWindow(QSettings& settings, AppConfig& appConfig) : setMinimumSize(size()); #endif - updateZeroconfService(); +#if defined(Q_OS_WIN) + m_BonjourRunning = isServiceRunning("Bonjour Service"); +#endif + + if (m_BonjourRunning) { + updateZeroconfService(); + } m_pAutoConnectCheckBox->setChecked(appConfig.autoConnect()); } @@ -896,3 +903,37 @@ void MainWindow::on_m_pAutoConnectCheckBox_toggled(bool checked) appConfig().setAutoConnect(checked); updateZeroconfService(); } + +bool MainWindow::isServiceRunning(QString name) +{ +#if defined(Q_OS_WIN) + SC_HANDLE hSCManager; + hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); + if (hSCManager == NULL) { + appendLogNote("failed to open a service controller manager, error: " + + GetLastError()); + return false; + } + + SC_HANDLE hService; + int length = name.length(); + wchar_t* array = new wchar_t[length + 1]; + name.toWCharArray(array); + array[length] = '\0'; + + hService = OpenService(hSCManager, array, SERVICE_QUERY_STATUS); + if (hService == NULL) { + appendLogNote("failed to open " + name + "service, error: " + + GetLastError()); + return false; + } + + SERVICE_STATUS status; + if (QueryServiceStatus(hService, &status)) { + if (status.dwCurrentState == SERVICE_RUNNING) { + return true; + } + } +#endif + return false; +} diff --git a/src/gui/src/MainWindow.h b/src/gui/src/MainWindow.h index 20cdd180..ad581021 100644 --- a/src/gui/src/MainWindow.h +++ b/src/gui/src/MainWindow.h @@ -146,6 +146,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase void stopDesktop(); void changeEvent(QEvent* event); void retranslateMenuBar(); + bool isServiceRunning(QString name); private: QSettings& m_Settings; @@ -165,6 +166,7 @@ class MainWindow : public QMainWindow, public Ui::MainWindowBase QMenu* m_pMenuWindow; QMenu* m_pMenuHelp; ZeroconfService* m_pZeroconfService; + bool m_BonjourRunning; private slots: void on_m_pAutoConnectCheckBox_toggled(bool checked);