📄 service.cxx.svn-base
字号:
/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
// -=- Service.cxx
#include <rfb_wince/Service.h>
#include <rfb_wince/MsgWindow.h>
#include <rfb_wince/DynamicFn.h>
#include <rfb_wince/ModuleFileName.h>
#include <rfb_wince/Registry.h>
#include <rfb_wince/OSVersion.h>
#include <rfb/Threading.h>
#include <logmessages/messages.h>
#include <rdr/Exception.h>
#include <rfb/LogWriter.h>
using namespace rdr;
using namespace rfb;
using namespace win32;
static LogWriter vlog("Service");
#ifndef _EFON_VNC_NOT_RUNAS_SERVICE
// - Internal service implementation functions
Service* service = 0;
VOID WINAPI serviceHandler(DWORD control) {
switch (control) {
case SERVICE_CONTROL_INTERROGATE:
vlog.info("cmd: report status");
service->setStatus();
return;
case SERVICE_CONTROL_PARAMCHANGE:
vlog.info("cmd: param change");
service->readParams();
return;
case SERVICE_CONTROL_SHUTDOWN:
vlog.info("cmd: OS shutdown");
service->osShuttingDown();
return;
case SERVICE_CONTROL_STOP:
vlog.info("cmd: stop");
service->setStatus(SERVICE_STOP_PENDING);
service->stop();
return;
};
vlog.debug("cmd: unknown %lu", control);
}
// -=- Message window derived class used under Win9x to implement stopService
#define WM_SMSG_SERVICE_STOP WM_USER
class ServiceMsgWindow : public MsgWindow {
public:
ServiceMsgWindow(const TCHAR* name) : MsgWindow(name) {}
LRESULT processMessage(UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_SMSG_SERVICE_STOP:
service->stop();
return TRUE;
}
return MsgWindow::processMessage(msg, wParam, lParam);
}
static const TCHAR* baseName;
};
const TCHAR* ServiceMsgWindow::baseName = _T("ServiceWindow:");
// -=- Service main procedure, used under WinNT/2K/XP by the SCM
VOID WINAPI serviceProc(DWORD dwArgc, LPTSTR* lpszArgv) {
vlog.debug("entering %s serviceProc", service->getName());
vlog.info("registering handler...");
service->status_handle = RegisterServiceCtrlHandler(service->getName(), serviceHandler);
if (!service->status_handle) {
DWORD err = GetLastError();
vlog.error("failed to register handler: %lu", err);
ExitProcess(err);
}
vlog.debug("registered handler (%lx)", service->status_handle);
service->setStatus(SERVICE_START_PENDING);
vlog.debug("entering %s serviceMain", service->getName());
service->status.dwWin32ExitCode = service->serviceMain(dwArgc, lpszArgv);
vlog.debug("leaving %s serviceMain", service->getName());
service->setStatus(SERVICE_STOPPED);
}
// -=- Service
Service::Service(const TCHAR* name_) : name(name_) {
status_handle = 0;
status.dwControlsAccepted = SERVICE_CONTROL_INTERROGATE | SERVICE_ACCEPT_SHUTDOWN | SERVICE_ACCEPT_STOP;
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS;
status.dwWin32ExitCode = NO_ERROR;
status.dwServiceSpecificExitCode = 0;
status.dwCheckPoint = 0;
status.dwWaitHint = 30000;
status.dwCurrentState = SERVICE_STOPPED;
}
void
Service::start() {
if (osVersion.isPlatformNT) {
SERVICE_TABLE_ENTRY entry[2];
entry[0].lpServiceName = (TCHAR*)name;
entry[0].lpServiceProc = serviceProc;
entry[1].lpServiceName = NULL;
entry[1].lpServiceProc = NULL;
vlog.debug("entering dispatcher");
if (!SetProcessShutdownParameters(0x100, 0))
vlog.error("unable to set shutdown parameters: %d", GetLastError());
service = this;
if (!StartServiceCtrlDispatcher(entry))
throw SystemException("unable to start service", GetLastError());
} else {
// - Create the service window, so the service can be stopped
TCharArray wndName(_tcslen(getName()) + _tcslen(ServiceMsgWindow::baseName) + 1);
_tcscpy(wndName.buf, ServiceMsgWindow::baseName);
_tcscat(wndName.buf, getName());
ServiceMsgWindow service_window(wndName.buf);
// - Locate the RegisterServiceProcess function
typedef DWORD (WINAPI * _RegisterServiceProcess_proto)(DWORD, DWORD);
DynamicFn<_RegisterServiceProcess_proto> _RegisterServiceProcess(_T("kernel32.dll"), "RegisterServiceProcess");
if (!_RegisterServiceProcess.isValid())
throw Exception("unable to find RegisterServiceProcess");
// - Run the service
(*_RegisterServiceProcess)(NULL, 1);
service = this;
serviceMain(0, 0);
(*_RegisterServiceProcess)(NULL, 0);
}
}
void
Service::setStatus() {
setStatus(status.dwCurrentState);
}
void
Service::setStatus(DWORD state) {
if (!osVersion.isPlatformNT)
return;
if (status_handle == 0) {
vlog.debug("warning - cannot setStatus");
return;
}
status.dwCurrentState = state;
status.dwCheckPoint++;
if (!SetServiceStatus(status_handle, &status)) {
status.dwCurrentState = SERVICE_STOPPED;
status.dwWin32ExitCode = GetLastError();
vlog.error("unable to set service status:%u", status.dwWin32ExitCode);
}
vlog.debug("set status to %u(%u)", state, status.dwCheckPoint);\
}
Service::~Service() {
vlog.debug("~Service");
service = 0;
}
// Find out whether this process is running as the WinVNC service
bool thisIsService() {
return service && (service->status.dwCurrentState != SERVICE_STOPPED);
}
#endif
// -=- Desktop handling code
// Switch the current thread to the specified desktop
static bool
switchToDesktop(HDESK desktop) {
/*
Efon Cut
HDESK old_desktop = GetThreadDesktop(GetCurrentThreadId());
if (!SetThreadDesktop(desktop)) {
vlog.debug("switchToDesktop failed:%u", GetLastError());
return false;
}
if (!CloseDesktop(old_desktop))
vlog.debug("unable to close old desktop:%u", GetLastError());
return true;
*/
return false;
}
// Determine whether the thread's current desktop is the input one
static bool
inputDesktopSelected() {
/*
Efon Cut
HDESK current = GetThreadDesktop(GetCurrentThreadId());
HDESK input = OpenInputDesktop(0, FALSE,
DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL |
DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS |
DESKTOP_SWITCHDESKTOP | GENERIC_WRITE);
if (!input) {
vlog.debug("unable to OpenInputDesktop(1):%u", GetLastError());
return false;
}
DWORD size;
char currentname[256];
char inputname[256];
if (!GetUserObjectInformation(current, UOI_NAME, currentname, 256, &size)) {
vlog.debug("unable to GetUserObjectInformation(1):%u", GetLastError());
CloseDesktop(input);
return false;
}
if (!GetUserObjectInformation(input, UOI_NAME, inputname, 256, &size)) {
vlog.debug("unable to GetUserObjectInformation(2):%u", GetLastError());
CloseDesktop(input);
return false;
}
if (!CloseDesktop(input))
vlog.debug("unable to close input desktop:%u", GetLastError());
// *** vlog.debug("current=%s, input=%s", currentname, inputname);
bool result = strcmp(currentname, inputname) == 0;
return result;
*/
return false;
}
// Switch the current thread into the input desktop
static bool
selectInputDesktop() {
// - Open the input desktop
/*
Efon Cut
HDESK desktop = OpenInputDesktop(0, FALSE,
DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL |
DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS |
DESKTOP_SWITCHDESKTOP | GENERIC_WRITE);
if (!desktop) {
vlog.debug("unable to OpenInputDesktop(2):%u", GetLastError());
return false;
}
// - Switch into it
if (!switchToDesktop(desktop)) {
CloseDesktop(desktop);
return false;
}
// ***
DWORD size = 256;
char currentname[256];
if (GetUserObjectInformation(desktop, UOI_NAME, currentname, 256, &size)) {
vlog.debug("switched to %s", currentname);
}
// ***
vlog.debug("switched to input desktop");
return true;
*/
return false;
}
// -=- Access points to desktop-switching routines
bool
rfb::win32::desktopChangeRequired() {
/*
Efon Cut
if (!osVersion.isPlatformNT)
return false;
return !inputDesktopSelected();
*/
return false;
}
bool
rfb::win32::changeDesktop() {
/*
Efon Cut
if (!osVersion.isPlatformNT)
return true;
if (osVersion.cannotSwitchDesktop)
return false;
return selectInputDesktop();
*/
return false;
}
// -=- Ctrl-Alt-Del emulation
class CADThread : public Thread {
public:
CADThread() : Thread("CtrlAltDel Emulator"), result(false) {}
virtual void run() {
/*
Efon Cut
HDESK old_desktop = GetThreadDesktop(GetCurrentThreadId());
if (switchToDesktop(OpenDesktop(_T("Winlogon"), 0, FALSE, DESKTOP_CREATEMENU | DESKTOP_CREATEWINDOW |
DESKTOP_ENUMERATE | DESKTOP_HOOKCONTROL |
DESKTOP_WRITEOBJECTS | DESKTOP_READOBJECTS |
DESKTOP_SWITCHDESKTOP | GENERIC_WRITE))) {
PostMessage(HWND_BROADCAST, WM_HOTKEY, 0, MAKELONG(MOD_ALT | MOD_CONTROL, VK_DELETE));
switchToDesktop(old_desktop);
result = true;
}
*/
}
bool result;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -