📄 server.c
字号:
{
printf ("Open service failed!\n");
return;
}
if (!QueryServiceStatus(schService, &ssStatus))
{
CloseServiceHandle(schService);
printf ("Query service status failed!\n");
return;
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING
|| ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
CloseServiceHandle(schService);
printf ("The service is running or starting!\n");
return;
}
lpConfig = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 1024);
if (!QueryServiceConfig(schService, lpConfig, 1024, &dwSize))
{
CloseServiceHandle(schService);
printf ("Query service config failed!\n");
return;
}
if (lpConfig->dwStartType == SERVICE_DISABLED)
{
printf ("The service is disabled!\n");
printf ("Try to change the service's start type...");
if (!ChangeServiceConfig(schService,
SERVICE_NO_CHANGE,
SERVICE_DEMAND_START,
SERVICE_NO_CHANGE,
NULL, NULL, NULL, NULL, NULL, NULL,NULL))
{
CloseServiceHandle(schService);
printf ("Failed!\n");
return;
}
else
printf ("Successfully!\n");
}
printf ("starting the service <%s>...\n", lpServiceName);
if (!StartService(schService, 0, NULL))
{
CloseServiceHandle(schService);
printf ("Starting the service failed!\n");
return;
}
if (!(QueryServiceStatus(schService, &ssStatus)))
{
CloseServiceHandle(schService);
printf ("Query service status failed!\n");
return;
}
while ( ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep(dwWaitTime);
// Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of structure
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint
break;
}
}
}
if ( ssStatus.dwCurrentState == SERVICE_RUNNING )
{
printf ("Service is started successfully!\n");
}
else
{
printf ("Start service failed!\n");
}
return;
}
void MyStopService(SC_HANDLE schSCManager, LPSTR lpServiceName)
{
SC_HANDLE schService;
DWORD dwWaitTime;
SERVICE_STATUS ssStatus;
schService = OpenService(schSCManager, lpServiceName, SERVICE_ALL_ACCESS);
if (schService == NULL)
{
printf ("Open service failed!\n");
return;
}
if (!QueryServiceStatus(schService, &ssStatus))
{
printf ("Query service failed!\n");
CloseServiceHandle(schService);
return;
}
if (ssStatus.dwCurrentState == SERVICE_STOPPED
|| ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
CloseServiceHandle(schService);
printf ("service is stopped!\n");
return;
}
printf ("stopping the service <%s>...\n", lpServiceName);
if (!(ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus)))
{
CloseServiceHandle(schService);
printf ("Control service status failed!\n");
return;
}
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep(dwWaitTime);
if (!QueryServiceStatus(schService, &ssStatus))
{
printf ("Query service status failed!\n");
return;
}
if ( ssStatus.dwCurrentState == SERVICE_STOPPED || ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
printf ("Service is stopped successfully!\n");
}
else
{
printf ("%d\n", ssStatus.dwCurrentState );
printf ("Service is not started!\n");
}
return;
}
void MyViewService(SC_HANDLE schSCManager, LPSTR lpServiceName)
{
SC_HANDLE schService;
LPSERVICE_STATUS_PROCESS lpStatus;
LPQUERY_SERVICE_CONFIG lpConfig;
LPSERVICE_DESCRIPTION lpDescription;
LPSERVICE_FAILURE_ACTIONS lpAction;
DWORD dwByteNeeded = 0;
lpStatus = (LPSERVICE_STATUS_PROCESS) LocalAlloc(LPTR, 1024);
lpConfig = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 1024);
lpDescription = (LPSERVICE_DESCRIPTION) LocalAlloc(LPTR, 1024);
lpAction = (LPSERVICE_FAILURE_ACTIONS) LocalAlloc(LPTR, 2048);
schService = OpenService(schSCManager, lpServiceName, g_dwServiceAccess);
if (schService == NULL)
{
printf ("Open service failed!\n");
return;
}
if (!QueryServiceStatusEx(schService, SC_STATUS_PROCESS_INFO,(LPBYTE) lpStatus, 1024, &dwByteNeeded))
{
CloseServiceHandle(schService);
printf ("Query service status failed!\n");
return;
}
printf ("\nView service: %s...",lpServiceName);
printf ("\n\tPID:%d", lpStatus->dwProcessId);
printf ("\n\tStatus:");
switch (lpStatus->dwCurrentState)
{
case SERVICE_STOPPED:
printf ("The service is not running.");
break;
case SERVICE_START_PENDING:
printf ("The service is starting.");
break;
case SERVICE_STOP_PENDING:
printf ("The service is stopping.");
break;
case SERVICE_RUNNING:
printf ("The service is running.");
break;
case SERVICE_CONTINUE_PENDING:
printf ("The service continue is pending.");
break;
case SERVICE_PAUSE_PENDING:
printf ("The service pause is pending.");
break;
case SERVICE_PAUSED:
printf ("The service is paused.");
break;
}
printf ("\n\tServiceType:");
switch (lpStatus->dwServiceType)
{
case SERVICE_FILE_SYSTEM_DRIVER:
printf ("The service is a file system driver.");
break;
case SERVICE_KERNEL_DRIVER:
printf ("The service is a kernel driver.");
break;
case SERVICE_WIN32_OWN_PROCESS:
printf ("The service runs in its own process.");
break;
case SERVICE_WIN32_SHARE_PROCESS:
printf ("The service shares a process with other services.");
break;
}
if(!QueryServiceConfig(schService, lpConfig, 1024, &dwByteNeeded))
{
CloseServiceHandle(schService);
printf ("Query service config failed!\n");
return;
}
printf ("\n\tDisplayName:%s", lpConfig->lpDisplayName );
printf ("\n\tPath:%s", lpConfig->lpBinaryPathName);
printf ("\n\tDependency:%s", lpConfig->lpDependencies );
printf ("\n\tLoadOrderGroup:%s", lpConfig->lpLoadOrderGroup );
printf ("\n\tStartType:");
switch (lpConfig->dwStartType)
{
case SERVICE_AUTO_START:
printf ("The service started automatically during system startup.");
break;
case SERVICE_BOOT_START:
printf ("The device driver started by the system loader. ");
break;
case SERVICE_DEMAND_START:
printf ("The service started need demand by a process.");
break;
case SERVICE_DISABLED:
printf ("The service that cannot be started.");
break;
case SERVICE_SYSTEM_START:
printf ("The device driver started by the IoInitSystem function.");
break;
}
if (!QueryServiceConfig2(schService, SERVICE_CONFIG_DESCRIPTION, (LPBYTE) lpDescription, 1024, &dwByteNeeded))
{
CloseServiceHandle(schService);
printf ("Query service's description failed!\n");
return;
}
printf ("\n\tDescription:%s", lpDescription->lpDescription);
if (!QueryServiceConfig2(schService, SERVICE_CONFIG_FAILURE_ACTIONS, (LPBYTE) lpAction, 2048, &dwByteNeeded))
{
CloseServiceHandle(schService);
printf ("Query service's action failed!\n");
return;
}
printf ("\n\tResetPeriod Time:%ds", lpAction->dwResetPeriod);
printf ("\n\tRebootMSG:%s", lpAction->lpRebootMsg);
printf ("\n\tCommand:%s", lpAction->lpCommand );
printf ("\n\tActions in the array:%d", lpAction->cActions );
printf ("\n");
LocalFree(lpStatus);
LocalFree(lpConfig);
LocalFree(lpDescription);
LocalFree(lpAction);
CloseServiceHandle(schService);
return;
}
void Usage(char* pchAppName)
{
printf ("**************************************************\n");
printf ("Service Application, by Refdom\n");
printf ("Email:refdom@263.net\n");
printf ("%s\n\n", pchAppName);
printf ("Description:ServiceApp.exe is used to enum services,start a service,stop a service.\n");
printf ("\tIt can be used with both local or remote machine.\n");
printf ("\tIf it's used with remote server, the username must be a administrator!\n");
printf ("Usage: ServiceApp.exe [option] {\\\\server} {username} {password} {argument}\n");
printf ("option:\n");
printf ("\t/l\tList all services!\n");
printf ("\t/d\tList all DRVIERs!\n");
printf ("\t/w\tList all WIN32 services!\n");
printf ("\t/k\tList all KERNEL drivers!\n");
printf ("\t/f\tList all FILE drivers!\n");
printf ("\t/o\tList all OWN_PROCESS services!\n");
printf ("\t/h\tList all SHARE_PROCESS services!\n");
printf ("\t/r\tList all win32 running services!\n");
printf ("\t/s\tStart a service.The [argument] is the service's name\n");
printf ("\t/p\tStop a service.The [argument] is the service's name\n");
printf ("\t/v\tView a service status.The [argument] is the service's name\n");
printf ("Example:\n");
printf ("\tRemote Example:ServiceApp.exe /s \\\\server username password \"service name\"\n");
printf ("\tLocal Example:ServiceApp.exe /s \"service name\"\n");
printf ("\tLocal Example:ServiceApp.exe /l\n");
printf ("**************************************************\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -