📄 service.cxx.svn-base
字号:
};
bool
rfb::win32::emulateCtrlAltDel() {
/*
Efon Cut
if (!osVersion.isPlatformNT)
return false;
CADThread* cad_thread = new CADThread();
vlog.debug("emulate Ctrl-Alt-Del");
if (cad_thread) {
cad_thread->start();
cad_thread->join();
bool result = cad_thread->result;
delete cad_thread;
return result;
}
return false;
*/
return false;
}
#ifndef _EFON_VNC_NO_EVENTLOG
// Windows CE does not contain the Event Logging service
// by Efon Team
// -=- Application Event Log target Logger class
class Logger_EventLog : public Logger {
public:
Logger_EventLog(const TCHAR* srcname) : Logger("EventLog") {
eventlog = RegisterEventSource(NULL, srcname);
if (!eventlog) {
printf("Unable to open event log:%ld\n", GetLastError());
}
}
~Logger_EventLog() {
if (eventlog) {
DeregisterEventSource(eventlog);
}
}
virtual void write(int level, const char *logname, const char *message) {
if (!eventlog) return;
TStr log(logname), msg(message);
const TCHAR* strings[] = {log, msg};
WORD type = EVENTLOG_INFORMATION_TYPE;
if (level == 0) type = EVENTLOG_ERROR_TYPE;
if (!ReportEvent(eventlog, type, 0, VNC4LogMessage, NULL, 2, 0, strings, NULL)) {
// *** It's not at all clear what is the correct behaviour if this fails...
printf("ReportEvent failed:%ld\n", GetLastError());
}
}
protected:
HANDLE eventlog;
};
static Logger_EventLog* logger = 0;
bool rfb::win32::initEventLogLogger(const TCHAR* srcname) {
if (logger)
return false;
if (osVersion.isPlatformNT) {
logger = new Logger_EventLog(srcname);
logger->registerLogger();
return true;
} else {
return false;
}
}
#endif
#ifndef _EFON_VNC_NOT_RUNAS_SERVICE
// -=- Registering and unregistering the service
bool rfb::win32::registerService(const TCHAR* name, const TCHAR* desc,
int argc, const char* argv[]) {
// - Initialise the default service parameters
const TCHAR* defaultcmdline;
if (osVersion.isPlatformNT)
defaultcmdline = _T("-service");
else
defaultcmdline = _T("-noconsole -service");
// endf
// - Get the full pathname of our executable
ModuleFileName buffer;
// - Calculate the command-line length
int cmdline_len = _tcslen(buffer.buf) + 4;
int i;
for (i=0; i<argc; i++) {
cmdline_len += strlen(argv[i]) + 3;
}
// - Add the supplied extra parameters to the command line
TCharArray cmdline(cmdline_len+_tcslen(defaultcmdline));
_stprintf(cmdline.buf, _T("\"%s\" %s"), buffer.buf, defaultcmdline);
for (i=0; i<argc; i++) {
_tcscat(cmdline.buf, _T(" \""));
_tcscat(cmdline.buf, TStr(argv[i]));
_tcscat(cmdline.buf, _T("\""));
}
// - Register the service
if (osVersion.isPlatformNT) {
// - Open the SCM
ServiceHandle scm = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (!scm)
throw rdr::SystemException("unable to open Service Control Manager", GetLastError());
ServiceHandle service = CreateService(scm,
name, desc, SC_MANAGER_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
SERVICE_AUTO_START, SERVICE_ERROR_IGNORE,
cmdline.buf, NULL, NULL, NULL, NULL, NULL);
if (!service)
throw rdr::SystemException("unable to create service", GetLastError());
// - Register the event log source
RegKey hk, hk2;
hk2.createKey(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application"));
hk.createKey(hk2, name);
for (i=_tcslen(buffer.buf); i>0; i--) {
if (buffer.buf[i] == _T('\\')) {
buffer.buf[i+1] = 0;
break;
}
}
const TCHAR* dllFilename = _T("logmessages.dll");
TCharArray dllPath(_tcslen(buffer.buf) + _tcslen(dllFilename) + 1);
_tcscpy(dllPath.buf, buffer.buf);
_tcscat(dllPath.buf, dllFilename);
hk.setExpandString(_T("EventMessageFile"), dllPath.buf);
hk.setInt(_T("TypesSupported"), EVENTLOG_ERROR_TYPE | EVENTLOG_INFORMATION_TYPE);
} else {
RegKey services;
services.createKey(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows\\CurrentVersion\\RunServices"));
services.setString(name, cmdline.buf);
}
Sleep(500);
return true;
}
bool rfb::win32::unregisterService(const TCHAR* name) {
if (osVersion.isPlatformNT) {
// - Open the SCM
ServiceHandle scm = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (!scm)
throw rdr::SystemException("unable to open Service Control Manager", GetLastError());
// - Create the service
ServiceHandle service = OpenService(scm, name, SC_MANAGER_ALL_ACCESS);
if (!service)
throw rdr::SystemException("unable to locate the service", GetLastError());
if (!DeleteService(service))
throw rdr::SystemException("unable to remove the service", GetLastError());
// - Register the event log source
RegKey hk;
hk.openKey(HKEY_LOCAL_MACHINE, _T("SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application"));
hk.deleteKey(name);
} else {
RegKey services;
services.openKey(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows\\CurrentVersion\\RunServices"));
services.deleteValue(name);
}
Sleep(500);
return true;
}
// -=- Starting and stopping the service
HWND findServiceWindow(const TCHAR* name) {
TCharArray wndName(_tcslen(ServiceMsgWindow::baseName)+_tcslen(name)+1);
_tcscpy(wndName.buf, ServiceMsgWindow::baseName);
_tcscat(wndName.buf, name);
vlog.debug("searching for %s window", CStr(wndName.buf));
return FindWindow(0, wndName.buf);
}
bool rfb::win32::startService(const TCHAR* name) {
if (osVersion.isPlatformNT) {
// - Open the SCM
ServiceHandle scm = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (!scm)
throw rdr::SystemException("unable to open Service Control Manager", GetLastError());
// - Locate the service
ServiceHandle service = OpenService(scm, name, SERVICE_START);
if (!service)
throw rdr::SystemException("unable to open the service", GetLastError());
// - Start the service
if (!StartService(service, 0, NULL))
throw rdr::SystemException("unable to start the service", GetLastError());
} else {
// - Check there is no service window
if (findServiceWindow(name))
throw rdr::Exception("the service is already running");
// - Find the RunServices registry key
RegKey services;
services.openKey(HKEY_LOCAL_MACHINE, _T("Software\\Microsoft\\Windows\\CurrentVersion\\RunServices"));
// - Read the command-line from it
TCharArray cmdLine = services.getString(name);
// - Start the service
PROCESS_INFORMATION proc_info;
STARTUPINFO startup_info;
ZeroMemory(&startup_info, sizeof(startup_info));
startup_info.cb = sizeof(startup_info);
if (!CreateProcess(0, cmdLine.buf, 0, 0, FALSE, CREATE_NEW_CONSOLE, 0, 0, &startup_info, &proc_info)) {
throw SystemException("unable to start service", GetLastError());
}
}
Sleep(500);
return true;
}
bool rfb::win32::stopService(const TCHAR* name) {
if (osVersion.isPlatformNT) {
// - Open the SCM
ServiceHandle scm = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (!scm)
throw rdr::SystemException("unable to open Service Control Manager", GetLastError());
// - Locate the service
ServiceHandle service = OpenService(scm, name, SERVICE_STOP);
if (!service)
throw rdr::SystemException("unable to open the service", GetLastError());
// - Start the service
SERVICE_STATUS status;
if (!ControlService(service, SERVICE_CONTROL_STOP, &status))
throw rdr::SystemException("unable to stop the service", GetLastError());
} else {
// - Find the service window
HWND service_window = findServiceWindow(name);
if (!service_window)
throw Exception("unable to locate running service");
// Tell it to quit
vlog.debug("sending service stop request");
if (!SendMessage(service_window, WM_SMSG_SERVICE_STOP, 0, 0))
throw Exception("unable to stop service");
// Check it's quitting...
DWORD process_id = 0;
HANDLE process = 0;
if (!GetWindowThreadProcessId(service_window, &process_id))
throw SystemException("unable to verify service has quit", GetLastError());
process = OpenProcess(SYNCHRONIZE | PROCESS_TERMINATE, FALSE, process_id);
if (!process)
throw SystemException("unable to obtain service handle", GetLastError());
int retries = 5;
vlog.debug("checking status");
while (retries-- && (WaitForSingleObject(process, 1000) != WAIT_OBJECT_0)) {}
if (!retries) {
vlog.debug("failed to quit - terminating");
// May not have quit because of silly Win9x registry watching bug..
if (!TerminateProcess(process, 1))
throw SystemException("unable to terminate process!", GetLastError());
throw Exception("service failed to quit - called TerminateProcess");
}
}
Sleep(500);
return true;
}
DWORD rfb::win32::getServiceState(const TCHAR* name) {
if (osVersion.isPlatformNT) {
// - Open the SCM
ServiceHandle scm = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (!scm)
throw rdr::SystemException("unable to open Service Control Manager", GetLastError());
// - Locate the service
ServiceHandle service = OpenService(scm, name, SERVICE_INTERROGATE);
if (!service)
throw rdr::SystemException("unable to open the service", GetLastError());
// - Get the service status
SERVICE_STATUS status;
if (!ControlService(service, SERVICE_CONTROL_INTERROGATE, (SERVICE_STATUS*)&status))
throw rdr::SystemException("unable to query the service", GetLastError());
return status.dwCurrentState;
} else {
HWND service_window = findServiceWindow(name);
return service_window ? SERVICE_RUNNING : SERVICE_STOPPED;
}
return 0;
}
char* rfb::win32::serviceStateName(DWORD state) {
switch (state) {
case SERVICE_RUNNING: return strDup("Running");
case SERVICE_STOPPED: return strDup("Stopped");
case SERVICE_STOP_PENDING: return strDup("Stopping");
};
CharArray tmp(32);
sprintf(tmp.buf, "Unknown (%lu)", state);
return tmp.takeBuf();
}
bool rfb::win32::isServiceProcess() {
return service != 0;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -