用VS中C++语言的windows服务模版写一个windows服务程序

jiangzhucao1989 jiangzhucao1989 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
#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 个参数
我看见网上别人代码没参数,可是我那样写就有问题,可带参数又该带什么?
希望懂的人能教教我,谢谢了
...全文

相关推荐

VS2008下C++开发Windows服务程序

使用VS2008开发环境C++编写的Windows服务程序,安装、启动、暂停、恢复、停止、重新启动服务 使用VS2008开发环境C++编写的Windows服务程序,安装、启动、暂停、恢复、停止、重新启

qq_43002944

VS2017创建运行Windows服务程序

如果我们看到我们的服务已经成功安装和运行,那么我们就已经成功创建了一个 Windows 服务程序。使用 VS2017 创建和运行 Windows 服务程序非常简单。

MQ1024

PB105 + VS2008写的服务程序

标题中的“PB105 + VS2008写的服务程序”指的是使用PowerBuilder 10.5(PB105)与Visual Studio 2008(VS2008)结合开发的一个服务应用程序。

hlj1013

vs2015 c++ windows Form模版.rar

总之,"vs2015 c++ windows Form模版.rar"文件提供了一个方便的方法,让C++开发者能够在Visual Studio 2015中利用Windows Forms技术构建GUI应用。

netman2012

【汇编】VS2022 Windows X64 下C/C++与汇编(asm)混写并且调用lib学习笔记

之前学习了liunx下的shellcode,之后分析了Windows下msf的shellcode汇编实现,然后听了队内逆向大佬讲的x86内联汇编以及各种利用方法,于是思考能不能用x64写汇编,踩了很多坑,于是写此文记录学习过程。C/C++AMD64 汇编(下文简称asm)ctf二进制这种方法的优点是不用从头写,本体既是加载器,方便嵌入其中,有很多妙妙操作也可以实现(比方说手动实现printf里面藏一点好东西之类的),缺点也很明显,过度依赖vs2022,迁移性很差。

伐尘
评论
收藏 已收藏
分享
返回顶部
jiangzhucao1989
C++ 语言 65,214 成员 250,514 内容