📄 monitorservice.cpp
字号:
//
// FreeSid( pAdminSid );
// free( ptg );
// CloseHandle( htok );
//
// return bIsAdmin;
//}
//
//bool IsLocalSystem(b
int main(int argc, char* argv[])
{
//g_hQuitServiceEvent=CreateEvent(0,TRUE,FALSE,0);
//
//if(!g_hQuitServiceEvent)return 1;
//
//InitRecv();
//
////if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus)) return 1;
//WaitForSingleObject(g_hQuitServiceEvent,INFINITE);
//
//return 0;
//InitRecv();
if(argc>1)
{
if(stricmp(argv[1],"-i")==0)
{
CHAR achClass[MAX_PATH] = ""; // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD j;
DWORD retValue;
CHAR achValue[1024];
DWORD cchValue = 1024;
CHAR achBuff[80];
HKEY hKey;
// RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",);
LRESULT lRet= RegOpenKeyEx( HKEY_CURRENT_USER,
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
0, KEY_QUERY_VALUE, &hKey );
if( lRet != ERROR_SUCCESS )
return FALSE;
// Get the class name and the value count.
RegQueryInfoKey(hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
// for (i = 0, retCode = ERROR_SUCCESS;
// retCode == ERROR_SUCCESS; i++)
//{
// retCode = RegEnumKeyEx(hKey,
// i,
// achKey,
// MAX_PATH,
// NULL,
// NULL,
// NULL,
// &ftLastWriteTime);
// if (retCode == (DWORD)ERROR_SUCCESS)
// {
// SendMessage(GetDlgItem(hDlg, IDL_LISTBOX),
// LB_ADDSTRING, 0, (LONG) achKey);
// }
//}
//SetCursor(LoadCursor (NULL, IDC_ARROW));
//
//// Enumerate the key values.
//SetCursor(LoadCursor(NULL, IDC_WAIT));
//
if (cValues)
{
char szData[1024];
for (j = 0, retValue = ERROR_SUCCESS;
j < cValues; j++)
{
cchValue = 1024;
achValue[0] = '\0';
retValue = RegEnumValue(hKey, j, achValue,
&cchValue,
NULL,
NULL, // &dwType,
(PBYTE)szData, // &bData,
&cchValue); // &bcData
if (retValue == (DWORD) ERROR_SUCCESS )
{
achBuff[0] = '\0';
// Add each value to a list box.
if (!lstrlen(achValue))
lstrcpy(achValue, "<NO NAME>");
//wsprintf(achBuff, "%d) %s ", j, achValue);
WritePrivateProfileString("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",achValue,szData,"ShellFolder.ini");
/*SendMessage(GetDlgItem(hDlg,IDL_LISTBOX2),
LB_ADDSTRING, 0, (LONG) achBuff); */
}
}
}
if(InstallService())
printf("\n\nService Installed Sucessfully\n");
else
printf("\n\nError Installing Service\n");
}
if(stricmp(argv[1],"-d")==0)
{
if(DeleteService())
printf("\n\nService UnInstalled Sucessfully\n");
else
printf("\n\nError UnInstalling Service\n");
}
else
{
printf("\n\nUnknown Switch Usage\n\nFor Install use MonitorService -i\n\nFor UnInstall use MonitorService -d\n");
}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[]={{"RecvPacket",ServiceMain},{NULL,NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
return 0;
}
void WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
m_ServiceStatus.dwServiceType = SERVICE_WIN32;
m_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
m_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwServiceSpecificExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
m_ServiceStatusHandle = RegisterServiceCtrlHandler("RecvPacket",ServiceCtrlHandler);
if (m_ServiceStatusHandle == (SERVICE_STATUS_HANDLE)0) return ;
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
g_hQuitServiceEvent=CreateEvent(0,TRUE,FALSE,0);
if(!g_hQuitServiceEvent)return;
InitRecv();
if (!SetServiceStatus (m_ServiceStatusHandle, &m_ServiceStatus)) return;
WaitForSingleObject(g_hQuitServiceEvent,INFINITE);
return;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
switch(Opcode)
{
case SERVICE_CONTROL_PAUSE:
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCurrentState = SERVICE_PAUSED;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
Suspend();
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
break;
case SERVICE_CONTROL_CONTINUE:
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
Resume();
m_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
break;
case SERVICE_CONTROL_STOP:
m_ServiceStatus.dwWin32ExitCode = 0;
m_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
m_ServiceStatus.dwCheckPoint = 0;
m_ServiceStatus.dwWaitHint = 0;
SetServiceStatus (m_ServiceStatusHandle,&m_ServiceStatus);
SetEvent(g_hQuitServiceEvent);
CloseHandle(g_hQuitServiceEvent);
End();
break;
case SERVICE_CONTROL_INTERROGATE:
break;
}
return;
}
BOOL InstallService()
{
char strDir[1024];
SC_HANDLE schSCManager,schService;
GetModuleFileName(NULL,strDir,sizeof strDir);
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL) return false;
LPCTSTR lpszBinaryPathName=strDir;
schService = CreateService(schSCManager,"RecvPacket","RecvPacket", // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_WIN32_OWN_PROCESS, // service type
SERVICE_AUTO_START, // start type
SERVICE_ERROR_NORMAL, // error control type
lpszBinaryPathName, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no Packet
if (schService == NULL) return false;
CloseServiceHandle(schService);
return true;
}
BOOL DeleteService()
{
SC_HANDLE schSCManager;
SC_HANDLE hService;
schSCManager = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL) return false;
hService=OpenService(schSCManager,"RecvPacket",SERVICE_ALL_ACCESS);
if (hService == NULL) return false;
if(DeleteService(hService)==0) return false;
if(CloseServiceHandle(hService)==0)
return false;
else
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -