首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

使用 Managed C++ 開發 Windows 服務完整指南

     編輯:關於C++

前言:從傳統 C++ 到 Managed C++ 開發服務

早期僅 Visual C++ 能寫 Windows 服務(NT 服務),現 .NET 時代 VB.NET、C# 皆可。但資深 VC++ 開發者若想用 Managed C++,本文提供完整實作指南。

建立服務專案

  1. VS.NET → 新專案 → Visual C++ → Windows Service (.NET)
  2. 命名服務(如 CGNotifier),向導產生繼承 System::ServiceProcess::ServiceBase 的類別
  3. 設計檢視可拖放 Timer、Database 連線等非視覺元件

核心程式碼結構

public __gc class CGNotifier : public System::ServiceProcess::ServiceBase
{
private:
    bool stopping;
    int loopsleep; // 毫秒
    Threading::Thread* servicethread;

protected:
    void OnStart(String* args[]) 
    {
        Threading::ThreadStart* threadStart = new Threading::ThreadStart(this, &CGNotifier::mainLoop);
        servicethread = new Threading::Thread(threadStart);
        servicethread->Start();
    }

    void mainLoop()
    {
        loopsleep = 1000; stopping = false;
        while (!stopping) { Threading::Thread::Sleep(loopsleep); }
    }

    void OnStop() { stopping = true; }
};

關鍵點:OnStart 必須快速返回,長時間工作需另闢執行緒(servicethread),OnStop 設旗標通知執行緒結束。

安裝與除錯

  • 設計檢視右鍵 → Add Installer,產生 ServiceInstaller、ServiceProcessInstaller
  • ServiceProcessInstaller.Account:LocalSystem(預設)、LocalService(Win2003+ 低權限)、NetworkService(需跨機器驗證)
  • ServiceInstaller.StartType:Manual/Automatic/Disabled
  • 安裝命令:CGNotifier.exe -Install(而非 InstallUtil,因 C++ 無法產生可驗證代碼)
  • 「電腦管理」→「服務」中啟動/停止,伺服器總管中查看狀態(綠色三角形=執行中)
  • 移除:CGNotifier.exe -Install /u

實作「定時喚醒執行任務」服務

1. 組態檔 app.config

<configuration>
  <appSettings>
    <add key="runhour" value="22" /> <!-- 22 點執行 -->
  </appSettings>
</configuration>

2. 建置後複製組態檔

copy app.config $(ConfigurationName)\$(TargetFileName).config

3. 讀取組態與主迴邏輯

void mainLoop()
{
    String* sHour = Configuration::ConfigurationSettings::AppSettings->get_Item("runhour");
    int runHour = System::Int32::Parse(sHour);
    bool rantoday = false;
    stopping = false;
    
    while (!stopping) {
        if (DateTime::Now.Hour == runHour && !rantoday) {
            // 執行任務:查詢資料庫、刪除過期檔案等
            rantoday = true;
        } else if (DateTime::Now.Hour != runHour) {
            rantoday = false;
        }
        Threading::Thread::Sleep(loopsleep);
    }
}

事件日誌記錄

Diagnostics::EventLog* log;
if (!Diagnostics::EventLog::SourceExists("CGNotifierService"))
    Diagnostics::EventLog::CreateEventSource("CGNotifierService", "CGNotifierLog");
log = new Diagnostics::EventLog("CGNotifierLog");
log->Source = "CGNotifierService";

// 任務完成後記錄
log->WriteEntry("服務的運行時間到了。", Diagnostics::EventLogEntryType::Information);

服務無 UI,事件日誌為最佳回報管道,會在事件檢視器建立專屬節點。

總結

Managed C++ 開發 Windows 服務具備完整 .NET 功能,且不受其他 .NET 語言限制,僅受想像力限制。注意 C++/VB/C# 專案模板細微差異即可順利開發。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved