用VS中C++语言的windows服务模版写一个windows服务程序
#pragma once
#include <iostream>
#include <Windows.h>
#using <system.dll>
using namespace std;
using namespace System;
using namespace System::Collections;
using namespace System::ServiceProcess;
using namespace System::ComponentModel;
using namespace System::Timers;
//using namespace System::Timers::Timer;
STARTUPINFO si,si1;
PROCESS_INFORMATION pi,pi1;//进程信息
namespace TTest {
/// <summary>
/// TTestWinService 摘要
/// </summary>
///
/// 警告: 如果更改此类的名称,则需要更改
/// 与此类所依赖的所有 .resx 文件关联的托管资源编译器工具的
/// “资源文件名”属性。否则,
/// 设计器将不能与此窗体的关联
/// 本地化资源正确交互。
public ref class TTestWinService : public System::ServiceProcess::ServiceBase
{
public:
TTestWinService()
{
InitializeComponent();
//
//TODO: 在此处添加构造函数代码
//
Sexe=TEXT("G:\\QTProjects\\AVC_FILE\\debug\\AVC_FILE.exe");
Sexe1=TEXT("G:\\QTProjects\\Query\\debug\\Query.exe");
}
System::Timers::Timer^ timer;
protected:
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
~TTestWinService()
{
if (components)
{
delete components;
}
}
/// <summary>
/// 设置具体的操作,以便服务可以执行它的工作。
/// </summary>
virtual void OnStart(array<String^>^ args) override
{
// TODO: 在此处添加代码以启动服务。
timer = gcnew System::Timers::Timer(10000);
timer->Elapsed +=gcnew ElapsedEventHandler(TTestWinService::timer_Elapsed); timer->Interval=5000;
timer->Enabled=true;
}
private :
void timer_Elapsed(Object^ source, ElapsedEventArgs^ e )
{
ZeroMemory(&::si,sizeof(::si));
::si.cb = sizeof(::si);
ZeroMemory(&::pi,sizeof (::pi));
ZeroMemory(&::si1,sizeof(::si1));
::si.cb = sizeof(::si1);
ZeroMemory(&::pi1,sizeof (::pi1));
// dosomethting();
if (!CreateProcess(Sexe,NULL,NULL,NULL,FALSE,0,NULL,NULL,&si,&pi))
{
cout<<"创建进程失败..."<<GetLastError()<<endl;
//system("pause");//用于测试
return ;
}
if (!CreateProcess(Sexe1,NULL,NULL,NULL,FALSE,0,NULL,NULL,&si1,&pi1))
{
//cout<<"创建进程失败..."<<GetLastError()<<endl;
//system("pause");//用于测试
return ;
}
}
protected:
/// <summary>
/// 停止此服务。
/// </summary>
virtual void OnStop() override
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
timer->Enabled = false;
TerminateProcess( pi.hProcess, 0);
TerminateProcess( pi1.hProcess, 0);
}
private:
/// <summary>
/// 必需的设计器变量。
/// </summary>
TCHAR *Sexe; //存储可执行文件路径
TCHAR *Sexe1;
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
void InitializeComponent(void)
{
this->components = gcnew System::ComponentModel::Container();
this->CanStop = true;
this->CanPauseAndContinue = true;
this->AutoLog = true;
this->ServiceName = L"TTestWinService";
}
#pragma endregion
};
}
编译器提示:(红色字体处)
1>g:\qtprojects\ttest\ttest\TTestWinService.h(62) : error C3867: “TTest::TTestWinService::timer_Elapsed”: 函数调用缺少参数列表;请使用“&TTest::TTestWinService::timer_Elapsed”创建指向成员的指针
1>g:\qtprojects\ttest\ttest\TTestWinService.h(62) : error C3350: “System::Timers::ElapsedEventHandler”: 委托构造函数需要 2 个参数
我看见网上别人代码没参数,可是我那样写就有问题,可带参数又该带什么?
希望懂的人能教教我,谢谢了