一个操作本地或者远程主机服务的小程序
2008-04-09 04:00:13来源:互联网 阅读 ()
//
// Enum Service
//
// File : EnumService.cpp
//
//
// Create by : refdom
// Email : refdom@263.net
// Home Page : www.opengram.com
//
// If you modify the code, or add more functions, please email me a copy.
//
////////////////////////////////////////////////////////////////////////////////
#include "ConnectServer.h"
#include <stdio.h>
#include <windows.h>
#include <Winnetwk.h>
#include <Shlwapi.h>
#pragma comment(lib, "Advapi32.lib")
#pragma comment(lib, "Mpr.lib")
DWORD g_dwManagerAccess = SC_MANAGER_ALL_ACCESS;
DWORD g_dwServiceAccess = SERVICE_ALL_ACCESS;
void ListServices (SC_HANDLE, DWORD);
void MyStartService (SC_HANDLE, LPSTR);
void MyStopService (SC_HANDLE, LPSTR);
void MyViewService (SC_HANDLE, LPSTR);
void Usage(char*);
int main(int argc, char* argv[])
{
int nRetCode = 0;
char szIpc[50] = "";
DWORD dwServiceType;
SC_HANDLE schSCManager;
LPSTR lpUserName, lpPassword, lpServiceName;
NETRESOURCE NET;
if (argc < 2)
{
Usage(argv[0]);
return 0;
}
if (argv[1][0] == '/' && strlen(argv[1]) == 2)
{
switch (tolower(argv[1][1]))
{
case 'l':
dwServiceType = SERVICE_WIN32 | SERVICE_DRIVER;
break;
case 'd':
dwServiceType = SERVICE_DRIVER;
break;
case 'w':
dwServiceType = SERVICE_WIN32;
break;
case 'k':
dwServiceType = SERVICE_KERNEL_DRIVER;
break;
case 'f':
dwServiceType = SERVICE_FILE_SYSTEM_DRIVER;
break;
case 'o':
dwServiceType = SERVICE_WIN32_OWN_PROCESS;
break;
case 'h':
dwServiceType = SERVICE_WIN32_SHARE_PROCESS;
break;
case 'r':
dwServiceType = -1;
break;
case 's':
case 'p':
case 'v':
if (argc < 6 && (argc != 3))
{
printf ("Error! Input a service name!\n");
return 0;
}
else if (argc >6)
{
printf ("Error! A wrong service name!\n");
return 0;
}
break;
case '?':
Usage(argv[0]);
return 0;
default:
printf ("Error: Unknow option!\n");
return 0;;
}
}
else
{
printf ("Error: Unknow option!\n");
return 0;
}
//get service name
if (argc == 6)
{
lpServiceName = argv[5];
}
else if (argc == 3)
{
// if ((argv[2][0] != '\\') && (argv[2][1] != '\\'))
// {
if (argv[1][0] == '/' && strlen(argv[1]) == 2)
{
switch (tolower(argv[1][1]))
{
case 'l':
case 'd':
case 'w':
case 'k':
case 'f':
case 'o':
case 'h':
case 'r':
printf ("Error arguments!\n");
return 0;
default:
lpServiceName = argv[2];
break;
}
}
// lpServiceName = argv[2];
// }
}
if (argc > 3)
{
/* if ((argv[2][0] != '\\') && (argv[2][1] != '\\'))
{
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
}
else
{
if (argc == 3)
{
lpUserName = "";
lpPassword = "";
//change access
g_dwManagerAccess = SC_MANAGER_CONNECT;
g_dwServiceAccess = GENERIC_READ;
sprintf (szIpc, "%s", argv[2]);
}
else
{
*/
lpUserName = argv[3];
lpPassword = argv[4];
sprintf (szIpc, "%s\\ipc$", argv[2]);
// }
NET.lpLocalName = NULL;
NET.lpRemoteName = szIpc;
NET.dwType = RESOURCETYPE_ANY;
NET.lpProvider = NULL;
printf ("Connecting %s...",argv[2]);
nRetCode = MyConnectServer(&NET, lpPassword, lpUserName, CONNECT_INTERACTIVE, szIpc);
if (!nRetCode)
printf ("Successfully!\n");
else
{
printf ("Failed!\n");
}
schSCManager = OpenSCManager(argv[2], NULL, SC_MANAGER_ALL_ACCESS);
// }
}
else
{
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
}
if (schSCManager == NULL)
{
printf ("%d\n", argc);
printf ("%d\n", GetLastError());
printf ("Open SCManager failed!\n");
return 0;
}
//
switch (tolower(argv[1][1]))
{
case 'l':
case 'd':
case 'w':
case 'k':
case 'f':
case 'o':
case 'h':
case 'r':
ListServices(schSCManager, dwServiceType);
break;
case 's':
MyStartService(schSCManager, lpServiceName);
break;
case 'p':
MyStopService(schSCManager, lpServiceName);
break;
case 'v':
MyViewService(schSCManager, lpServiceName);
break;
}
CloseServiceHandle(schSCManager);
if (argc >3 )
{
//close the session with remote server
printf ("\nDisconnecting server...");
nRetCode = MyDisConnectServer(szIpc, CONNECT_UPDATE_PROFILE, TRUE);
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
下一篇:透视木马程序开发技术(中)
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash