大家好,我是一个程序的初学者,刚开始学习编写程序,我自己为了练手写了一个日志清除器,就是清除掉WEB日志的程序,先停止服务,然后清除日志,然后开启服务。
程序很简单,可是我怎么也编译不过去,老是报错,我又找不到哪里错了.那位兄弟能帮我调试一下我的程序,看看是哪里的问题,我自己看了半天也找不出毛病,谢谢各位朋友!!//**********************************************************************
// Version: V1.0
// Coder: Error1
// Date Release: Only For Practice and Learning
// Purpose: Cleaning the WEB log on local
// Test PlatForm: Win 2K Pro And Server SP4
// Compiled On: Compile On VC++ 6.0
//**********************************************************************
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <basetsd.h>
#include <winsvc.h>//函数原型
void StopSvc(LPCTSTR lpServiceName)
void DelFile(LPCTSTR lpFileName)
void StartSvc(LPCTSTR lpServiceName)void Start()
void Usage()//主函数开始
int main(int argc, char* argv[])
{
if (argc!=3) //如果命令的参数不等则输出使用说明
{
Start();
Usage();
}
else
{
if (!stricmp(argv[1]."W3SVC"))//比对服务
{
StopSvc(argv[1]); //停止服务
DelFile(argv[2]); //删除文件
StartSvc(argv[1]); //启动服务
}
else
{
"Sorry, the Service parameter you input is invalid!"
}
}
}void StopSvc(LPCTSTR lpServiceName)
{
BOOL hControl;
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ServiceStatus;
schSCManager = OpenScManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);//连接服务管理
if (schSCManager==NULL) //如果连接失败
{
printf("Open Service Control Manager Database Failure !\n");
return ;
}
printf("creating......"); //用来判断程序是否执行
schService = OpenService(schSCManager,lpServiceName,SERVICE_ALL_ACCESS);
//设置服务访问权限
if (schService==NULL)
{
printf("Opening Service .... Failure !\n");
CloseServiceHandle(schSCManager);
return ;
} printf("Opening Service. . . . . .");
hControl= ControlService(schService,SERVICE_CONTROL_STOP,&ServiceStatus);
//关闭服务
if (hControl)
{
printf("STOP SERVICE failed:(");
return;
}
else
{
printf("W3SVC die |:)");
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
//删除日志文件
void DelFile(LPCTSTR lpFileName)
{
BOOL bDel;
char *tcSystemDirectory[1024];
LPCTSTR FileToDel;
GetSystemDirectory(tcSystemDirectory,1024);
strcat(tcSystemDirectory,"\\logfiles\\W3SVC1\\");
strcat(tcSystemDirectory,lpFileName);
FileToDel = tcSystemDirectory;
bDel=DeleteFile(FileToDel);//取得删除文件句柄
if (!bDel)
{
print("Delete file success!");
}
else
{
print("Delete file Failed\n");
}
return;
}void StartSvc(LPCTSTR ServiceName)
{
BOOL hcontrol;
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ServiceStatus; schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(schSCManager=NULL)
{
printf("Open Service Control Manager Database Failure !\n");
return ;
}
schService = OpenService(schSCManager,lpServiceName,SERVICE_Start);
if(schService=NULL)
{
printf("Opening Service .... Failure !\n");
CloseServiceHandle(schSCManager);
return ;
}
hControl = ControlService(schService,1,&ServiceStatus); if(!hControl)
{
printf("Yeah!Service Start ");
}
else
{
printf("Woo...Start Error..");
}
return;
}
void Start()
{
printf("\n");
printf("\t\t---[ Practice, by Error1]---\n");
return ;
}void Usage()
{
printf("Usage:\n");
printf(" Cleaning W3SVC exyymmdd\n");
printf("yy--year,04--2004");
printf("mm--month,01--一月");
prtntf("dd--day,12--12日");
printf("Example:\n");
printf(" 2004/01/05");
printf(" Cleaning W3SVC ex040105.log");
return ;
}
程序很简单,可是我怎么也编译不过去,老是报错,我又找不到哪里错了.那位兄弟能帮我调试一下我的程序,看看是哪里的问题,我自己看了半天也找不出毛病,谢谢各位朋友!!//**********************************************************************
// Version: V1.0
// Coder: Error1
// Date Release: Only For Practice and Learning
// Purpose: Cleaning the WEB log on local
// Test PlatForm: Win 2K Pro And Server SP4
// Compiled On: Compile On VC++ 6.0
//**********************************************************************
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <basetsd.h>
#include <winsvc.h>//函数原型
void StopSvc(LPCTSTR lpServiceName)
void DelFile(LPCTSTR lpFileName)
void StartSvc(LPCTSTR lpServiceName)void Start()
void Usage()//主函数开始
int main(int argc, char* argv[])
{
if (argc!=3) //如果命令的参数不等则输出使用说明
{
Start();
Usage();
}
else
{
if (!stricmp(argv[1]."W3SVC"))//比对服务
{
StopSvc(argv[1]); //停止服务
DelFile(argv[2]); //删除文件
StartSvc(argv[1]); //启动服务
}
else
{
"Sorry, the Service parameter you input is invalid!"
}
}
}void StopSvc(LPCTSTR lpServiceName)
{
BOOL hControl;
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ServiceStatus;
schSCManager = OpenScManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);//连接服务管理
if (schSCManager==NULL) //如果连接失败
{
printf("Open Service Control Manager Database Failure !\n");
return ;
}
printf("creating......"); //用来判断程序是否执行
schService = OpenService(schSCManager,lpServiceName,SERVICE_ALL_ACCESS);
//设置服务访问权限
if (schService==NULL)
{
printf("Opening Service .... Failure !\n");
CloseServiceHandle(schSCManager);
return ;
} printf("Opening Service. . . . . .");
hControl= ControlService(schService,SERVICE_CONTROL_STOP,&ServiceStatus);
//关闭服务
if (hControl)
{
printf("STOP SERVICE failed:(");
return;
}
else
{
printf("W3SVC die |:)");
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
}
//删除日志文件
void DelFile(LPCTSTR lpFileName)
{
BOOL bDel;
char *tcSystemDirectory[1024];
LPCTSTR FileToDel;
GetSystemDirectory(tcSystemDirectory,1024);
strcat(tcSystemDirectory,"\\logfiles\\W3SVC1\\");
strcat(tcSystemDirectory,lpFileName);
FileToDel = tcSystemDirectory;
bDel=DeleteFile(FileToDel);//取得删除文件句柄
if (!bDel)
{
print("Delete file success!");
}
else
{
print("Delete file Failed\n");
}
return;
}void StartSvc(LPCTSTR ServiceName)
{
BOOL hcontrol;
SC_HANDLE schSCManager;
SC_HANDLE schService;
SERVICE_STATUS ServiceStatus; schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(schSCManager=NULL)
{
printf("Open Service Control Manager Database Failure !\n");
return ;
}
schService = OpenService(schSCManager,lpServiceName,SERVICE_Start);
if(schService=NULL)
{
printf("Opening Service .... Failure !\n");
CloseServiceHandle(schSCManager);
return ;
}
hControl = ControlService(schService,1,&ServiceStatus); if(!hControl)
{
printf("Yeah!Service Start ");
}
else
{
printf("Woo...Start Error..");
}
return;
}
void Start()
{
printf("\n");
printf("\t\t---[ Practice, by Error1]---\n");
return ;
}void Usage()
{
printf("Usage:\n");
printf(" Cleaning W3SVC exyymmdd\n");
printf("yy--year,04--2004");
printf("mm--month,01--一月");
prtntf("dd--day,12--12日");
printf("Example:\n");
printf(" 2004/01/05");
printf(" Cleaning W3SVC ex040105.log");
return ;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货