Added code to check if Bonjour service is running

This commit is contained in:
Xinyu Hou 2014-11-11 14:08:55 +00:00
parent c51ce29954
commit 6ef744cd81
2 changed files with 45 additions and 2 deletions

View File

@ -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;
}

View File

@ -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);