📄 vncservice.cpp
字号:
vnclog.Print(LL_INTERR, VNCLOG("unable to locate LockWorkStation - requires Windows 2000 or above (%u)\n"), GetLastError());
FreeLibrary(user32);
return FALSE;
}
// Attempt to lock the workstation
BOOL result = (lockworkstation)();
if (!result) {
vnclog.Print(LL_INTERR, VNCLOG("call to LockWorkstation failed\n"));
FreeLibrary(user32);
return FALSE;
}
FreeLibrary(user32);
return result;
}
// Static routine to show the Properties dialog for a currently-running
// copy of WinVNC, (usually a servicified version.)
BOOL
vncService::ShowProperties()
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_PROPERTIES_SHOW, 0, 0))
{
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
// Static routine to find a window by its title substring (case-insensitive).
// Window titles and search substrings will be truncated at length 255.
HWND
vncService::FindWindowByTitle(char *substr)
{
char l_substr[256];
strncpy(l_substr, substr, 255);
l_substr[255] = 0;
int i;
for (i = 0; i < (int)strlen(substr); i++) {
l_substr[i] = tolower(l_substr[i]);
}
char title[256];
HWND hWindow = GetForegroundWindow();
while (hWindow != NULL) {
int len = GetWindowText(hWindow, title, 256);
for (i = 0; i < len; i++) {
title[i] = tolower(title[i]);
}
DWORD style = GetWindowLong(hWindow, GWL_STYLE);
if ((style & WS_VISIBLE) != 0 && strstr(title, l_substr) != NULL) {
if (IsIconic(hWindow))
SendMessage(hWindow, WM_SYSCOMMAND, SC_RESTORE, 0);
SetForegroundWindow(hWindow);
break;
}
hWindow = GetNextWindow(hWindow, GW_HWNDNEXT);
}
if (hWindow == NULL) {
MessageBox(NULL, "Unable to find a window with the specified title.",
szAppName, MB_ICONEXCLAMATION | MB_OK);
}
return hWindow;
}
BOOL
vncService::PostShareAll()
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_SERVER_SHAREALL, 0, 0))
{
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
BOOL
vncService::PostSharePrimary()
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_SERVER_SHAREPRIMARY, 0, 0))
{
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
BOOL
vncService::PostShareArea(unsigned short x, unsigned short y,
unsigned short w, unsigned short h)
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_SERVER_SHAREAREA,
MAKEWPARAM(x,y), MAKELPARAM(w,h))) {
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
BOOL
vncService::PostShareWindow(HWND hwnd)
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_SERVER_SHAREWINDOW, (WPARAM)hwnd, 0))
{
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
// Static routine to show the Default Properties dialog for a currently-running
// copy of WinVNC, (usually a servicified version.)
BOOL
vncService::ShowDefaultProperties()
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_DEFAULT_PROPERTIES_SHOW, 0, 0))
{
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
// Static routine to show the About dialog for a currently-running
// copy of WinVNC, (usually a servicified version.)
BOOL
vncService::ShowAboutBox()
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_ABOUTBOX_SHOW, 0, 0))
{
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
// Static routine to tell a locally-running instance of the server
// to connect out to a new client
BOOL
vncService::PostAddNewClient(unsigned long ipaddress, unsigned short port)
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_ADD_CLIENT_MSG, (WPARAM)port, (LPARAM)ipaddress))
{
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
// Static routine to tell a locally-running instance of the server
// to disconnect all connected clients.
BOOL
vncService::KillAllClients()
{
// Post to the WinVNC menu window
if (!PostToWinVNC(MENU_KILL_ALL_CLIENTS_MSG, 0, 0))
{
MessageBox(NULL, "No existing instance of WinVNC could be contacted", szAppName, MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
return TRUE;
}
// SERVICE-MODE ROUTINES
// Service-mode defines:
// Executable name
#define VNCAPPNAME "winvnc"
// Internal service name
#define VNCSERVICENAME "winvnc"
// Displayed service name
#define VNCSERVICEDISPLAYNAME "VNC Server"
// List of other required services ("dependency 1\0dependency 2\0\0")
// *** These need filling in properly
#define VNCDEPENDENCIES ""
// Internal service state
SERVICE_STATUS g_srvstatus; // current status of the service
SERVICE_STATUS_HANDLE g_hstatus;
DWORD g_error = 0;
DWORD g_servicethread = NULL;
char* g_errortext[256];
// Forward defines of internal service functions
void WINAPI ServiceMain(DWORD argc, char **argv);
void ServiceWorkThread(void *arg);
void ServiceStop();
void WINAPI ServiceCtrl(DWORD ctrlcode);
bool WINAPI CtrlHandler (DWORD ctrltype);
BOOL ReportStatus(DWORD state, DWORD exitcode, DWORD waithint);
// ROUTINE TO QUERY WHETHER THIS PROCESS IS RUNNING AS A SERVICE OR NOT
BOOL g_servicemode = FALSE;
BOOL
vncService::RunningAsService()
{
return g_servicemode;
}
BOOL
vncService::KillRunningCopy()
{
// Locate the hidden WinVNC menu window
HWND hservwnd;
while ((hservwnd = FindWindow(MENU_CLASS_NAME, NULL)) != NULL)
{
// Post the message to WinVNC
PostMessage(hservwnd, WM_CLOSE, 0, 0);
omni_thread::sleep(1);
}
return TRUE;
}
// ROUTINE TO POST THE HANDLE OF THE CURRENT USER TO THE RUNNING WINVNC, IN ORDER
// THAT IT CAN LOAD THE APPROPRIATE SETTINGS. THIS IS USED ONLY BY THE SVCHELPER
// OPTION, WHEN RUNNING UNDER NT
BOOL
vncService::PostUserHelperMessage()
{
// - Check the platform type
if (!IsWinNT())
return TRUE;
// - Get the current process ID
DWORD processId = GetCurrentProcessId();
// - Post it to the existing WinVNC
int retries = 6;
while (!PostToWinVNC(MENU_SERVICEHELPER_MSG, 0, (LPARAM)processId) && retries--)
omni_thread::sleep(10);
// - Wait until it's been used
omni_thread::sleep(5);
return retries;
}
BOOL
vncService::PostReloadMessage()
{
// - Check the platform type
if (!IsWinNT())
return TRUE;
// - Get the current process ID
DWORD processId = GetCurrentProcessId();
// - Post it to the existing WinVNC
// FIXME: Code duplication, see PostUserHelperMessage().
int retries = 6;
while (!PostToWinVNC(MENU_RELOAD_MSG, 0, (LPARAM)processId) && retries--)
omni_thread::sleep(10);
// - Wait until it's been used
omni_thread::sleep(5);
return retries;
}
// ROUTINE TO PROCESS AN INCOMING INSTANCE OF THE ABOVE MESSAGE
BOOL
vncService::ProcessUserHelperMessage(WPARAM wParam, LPARAM lParam) {
// - Check the platform type
if (!IsWinNT() || !vncService::RunningAsService())
return TRUE;
// - Close the HKEY_CURRENT_USER key, to force NT to reload it for the new user
// NB: Note that this is _really_ dodgy if ANY other thread is accessing the key!
if (RegCloseKey(HKEY_CURRENT_USER) != ERROR_SUCCESS) {
vnclog.Print(LL_INTERR, VNCLOG("failed to close current registry hive\n"));
return FALSE;
}
// - Revert to our own identity
RevertToSelf();
g_impersonating_user = FALSE;
// - Open the specified process
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, (DWORD)lParam);
if (processHandle == NULL) {
vnclog.Print(LL_INTERR, VNCLOG("failed to open specified process, error=%d\n"),
GetLastError());
return FALSE;
}
// - Get the token for the given process
HANDLE userToken = NULL;
if (!OpenProcessToken(processHandle, TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE, &userToken)) {
vnclog.Print(LL_INTERR, VNCLOG("failed to get user token, error=%d\n"),
GetLastError());
CloseHandle(processHandle);
return FALSE;
}
CloseHandle(processHandle);
// - Set this thread to impersonate them
if (!ImpersonateLoggedOnUser(userToken)) {
vnclog.Print(LL_INTERR, VNCLOG("failed to impersonate user, error=%d\n"),
GetLastError());
CloseHandle(userToken);
return FALSE;
}
CloseHandle(userToken);
g_impersonating_user = TRUE;
vnclog.Print(LL_INTINFO, VNCLOG("impersonating logged on user\n"));
return TRUE;
}
// SERVICE MAIN ROUTINE
int
vncService::WinVNCServiceMain()
{
typedef DWORD (WINAPI * RegisterServiceProc)(DWORD, DWORD);
const ULONG RSP_SIMPLE_SERVICE = 0x00000001;
const ULONG RSP_UNREGISTER_SERVICE = 0x00000000;
g_servicemode = TRUE;
// How to run as a service depends upon the OS being used
switch (g_platform_id)
{
// Windows 95/98
case VER_PLATFORM_WIN32_WINDOWS:
{
// Obtain a handle to the kernel library
HINSTANCE kerneldll = LoadLibrary("KERNEL32.DLL");
if (kerneldll == NULL)
break;
// And find the RegisterServiceProcess function
RegisterServiceProc RegisterService;
RegisterService = (RegisterServiceProc) GetProcAddress(kerneldll, "RegisterServiceProcess");
if (RegisterService == NULL)
break;
// Register this process with the OS as a service!
RegisterService(NULL, RSP_SIMPLE_SERVICE);
// Run the service itself
WinVNCAppMain();
// Then remove the service from the system service table
RegisterService(NULL, RSP_UNREGISTER_SERVICE);
// Free the kernel library
FreeLibrary(kerneldll);
// *** If we don't kill the process directly here, then
// for some reason, WinVNC crashes...
// *** Is this now fixed (with the stdcall patch above)?
//ExitProcess(0);
}
break;
// Windows NT
case VER_PLATFORM_WIN32_NT:
{
// Create a service entry table
SERVICE_TABLE_ENTRY dispatchTable[] =
{
{VNCSERVICENAME, (LPSERVICE_MAIN_FUNCTION)ServiceMain},
{NULL, NULL}
};
// Call the service control dispatcher with our entry table
if (!StartServiceCtrlDispatcher(dispatchTable))
LogErrorMsg("StartServiceCtrlDispatcher failed.");
}
break;
};
return 0;
}
// SERVICE MAIN ROUTINE
void WINAPI ServiceMain(DWORD argc, char**argv)
{
// Register the service control handler
g_hstatus = RegisterServiceCtrlHandler(VNCSERVICENAME, ServiceCtrl);
if (g_hstatus == 0)
return;
// Set up some standard service state values
g_srvstatus.dwServiceType = SERVICE_WIN32 | SERVICE_INTERACTIVE_PROCESS;
g_srvstatus.dwServiceSpecificExitCode = 0;
// Give this status to the SCM
if (!ReportStatus(
SERVICE_START_PENDING, // Service state
NO_ERROR, // Exit code type
15000)) // Hint as to how long WinVNC should have hung before you assume error
{
ReportStatus(
SERVICE_STOPPED,
g_error,
0);
return;
}
// Now start the service for real
omni_thread *workthread = omni_thread::create(ServiceWorkThread);
return;
}
// SERVICE START ROUTINE - thread that calls WinVNCAppMain
void ServiceWorkThread(void *arg)
{
// Save the current thread identifier
g_servicethread = GetCurrentThreadId();
// report the status to the service control manager.
//
if (!ReportStatus(
SERVICE_RUNNING, // service state
NO_ERROR, // exit code
0)) // wait hint
return;
// RUN!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -