📄 winvnc.cpp
字号:
// Copyright (C) 2006 Teamviewer GmbH. All Rights Reserved.
// Copyright (C) 1999 AT&T Laboratories Cambridge. All Rights Reserved.
//
// This file is part of TeamViewer.
//
// TeamViewer 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 program 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 program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.
//
// If the source code for TeamViewer is not available from the place
// whence you received this file, check http://www.teamviewer.com
// for information on obtaining it.
// WinVNC.cpp
// 24/11/97 WEZ
// WinMain and main WndProc for the new version of WinVNC
////////////////////////////
// System headers
#include "stdhdrs.h"
////////////////////////////
// Custom headers
#include "VSocket.h"
#include "vncServer.h"
#include "vncMenu.h"
#include "vncInstHandler.h"
#include "vncService.h"
#include "vncOSVersion.h"
#include "videodriver.h"
#include "global.h"
#include "..\..\vncviewer\ConnectionLog.h"
#include "..\..\vncviewer\vncViewer.h"
#include "..\..\vncviewer\SessionDialog.h"
#define LOCALIZATION_MESSAGES // ACT: full declaration instead on extern ones
#include "localization.h" // Act : add localization on messages
// Application instance and name
HINSTANCE hAppInstance;
const char *szAppName = "TeamViewer";
DWORD mainthreadId;
BOOL AllowMulti=false;
BOOL DisableMultiWarning=false;
extern bool isRemoteControl;
HBITMAP bmpBackgroundDialog;
HBRUSH menuBrush;
HBRUSH menuSelectedBrush;
vncInstHandler *instancehan;
bool DisableScreenSaver();
void EnableScreenSaver();
void RemoveMutex()
{
if (instancehan != NULL)
instancehan->RemoveMutex();
}
// WinMain parses the command line and either calls the main App
// routine or, under NT, the main service routine.
int ServerMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
setbuf(stderr, 0);
//ACT: Load all messages from ressource file
Load_Localization(hInstance) ;
// Save the application instance and main thread id
hAppInstance = hInstance;
mainthreadId = GetCurrentThreadId();
// Initialise the VSocket system
VSocketSystem socksys;
if (!socksys.Initialised())
{
MessageBox(NULL, sz_ID_FAILED_INIT, szAppName, MB_OK);
return 0;
}
vnclog.Print(LL_STATE, VNCLOG("sockets initialised"));
/////////////////// Staudenmeyer
// Make the command-line lowercase and parse it
int i=0;
//for (i = 0; i < strlen(szCmdLine); i++)
//{
// szCmdLine[i] = tolower(szCmdLine[i]);
//}
//////////////////
BOOL argfound = FALSE;
//string argument=szCmdLine;
vector<string> args;
Tokenize(szCmdLine,args);
map<string,string> map;
string firstKey="";
int autokeys=0;
for(i=0;i<args.size();i++)
{
string key;
string value;
string arg=args[i];
if(arg[0]=='-')
{
key=arg.substr(1);
if(i+1<args.size())
{
string nextArg=args[i+1];
if(nextArg.find("-")!=0)
{
i++;
value=nextArg;
}
else
{
value="";
}
}
else
{
value="";
}
}
else
{
autokeys++;
key=IntegerToString(autokeys);
value=arg;
}
if(map.size() == 0)
{
firstKey=key;
}
map[key]=value;
}
if(firstKey=="")
{
return WinVNCAppMain(&firstKey,&map);
}
else if(firstKey=="installation")
{
return WinVNCAppMain(&firstKey,&map);
}
else if(firstKey=="noInstallation")
{
isNoInstallation=true;
return WinVNCAppMain();
}
else if(firstKey==winvncRunServiceHelper)
{
// Run the WinVNC Service Helper app
vncService::PostUserHelperMessage();
return 0;
}
else if (firstKey==winvncRunService)
{
// Run WinVNC as a service
return vncService::WinVNCServiceMain();
}
else if (firstKey == winvncQuickSupport)
{
isQuickSupport=true;
string explanation=map["explanation"];
if(explanation!="")
{
strncpy(SessionDialog::supportExplanation,explanation.c_str(),500);
isQSCustom = true;
}
string title=map["title"];
if(title!="")
{
strncpy(SessionDialog::quickSupportTitle,title.c_str(),100);
isQSCustom = true;
}
string pass = map["password"];
if(pass != "")
{
strncpy(SessionDialog::quickSupportPassword, pass.c_str(), 9);
SessionDialog::quickSupportPassword[8] = '\0';
}
return WinVNCAppMain();
}
else if(firstKey==winvncRequestConnect)
{
return WinVNCAppMain(NULL,NULL,NULL,true);
}
//TODO: the install/remove routines should modify the registry so we later know which option was chosen in properties window
else if (firstKey==winvncInstallService)
{
// Install WinVNC as a service
vncService::InstallService();
i+=strlen(winvncInstallService);
//continue;
}
else if (firstKey==winvncInstallServices)
{
// Install WinVNC as a service
vncService::InstallService(1);
i+=strlen(winvncInstallServices);
//continue;
}
else if (firstKey==winvncRemoveService)
{
// Remove the WinVNC service
vncService::RemoveService(true);
i+=strlen(winvncRemoveService);
}
else if (firstKey==winvncKillRunningCopy)
{
// Kill any already running copy of WinVNC
vncService::KillRunningCopy();
i+=strlen(winvncKillRunningCopy);
}
else if(firstKey=="config")
{
WinVNCAppMain(NULL,&map,szCmdLine,true);
}
else if(firstKey=="1")
{
WinVNCAppMain(&firstKey,&map,szCmdLine,true);
}
else if(firstKey=="rc")
{
isRemoteControl=true;
return WinVNCAppMain(&firstKey,&map,szCmdLine,true);
}
else
{
// Either the user gave the -help option or there is something odd on the cmd-line!
// Show the usage dialog
MessageBox(NULL, winvncUsageText, sz_ID_WINVNC_USAGE, MB_OK | MB_ICONINFORMATION);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
#ifdef _DEBUG
{
// Get current flag
int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
// Turn on leak-checking bit
tmpFlag |= _CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF;
// Set flag to the new value
_CrtSetDbgFlag( tmpFlag );
// In Debug mode always write a log file for startup
//char path[256];
char buffer[256];
ZeroMemory(buffer,sizeof(buffer));
DWORD length = 256;
GetUserName(buffer, &length);
string sFolderPath;
if(strncmp(buffer, "SYSTEM", 256) == 0)
GetApplicationDirectory(sFolderPath);
else
{
GetSpecialFolderPath(CSIDL_DESKTOP,sFolderPath);
}
//strcat(path,"\\TeamViewer_Logfile.log");
sFolderPath += "\\TeamViewer_Logfile.log";
vnclog.SetFile(sFolderPath.c_str(), true);
vnclog.SetMode(VNCLog::ToDebug|VNCLog::ToFile);
vnclog.SetLevel(10);
}
#endif
// Fallback try..catch if something goes wrong...
try
{
return ServerMain(hInstance, hPrevInstance, szCmdLine, iCmdShow);
}
catch (...)
{
return 0;
}
}
// This is the main routine for WinVNC when running as an application
// (under Windows 95 or Windows NT)
// Under NT, WinVNC can also run as a service. The WinVNCServerMain routine,
// defined in the vncService header, is used instead when running as a service.
int WinVNCAppMain(string *command,map<string,string>* args,char* commandLine,bool requestConnection)
{
bool screenSaverDisabled;
string sPath;
GetApplicationDirectory(sPath);
//strncat(path, "TeamViewer.ini", 255-strlen(path));
sPath += "TeamViewer.ini";
if (isQuickSupport)
isNoSave = true;
else
isNoSave = (bool)GetPrivateProfileInt("Settings", "nosave", 0, sPath.c_str());
DisableCaptureBlt = (bool)GetPrivateProfileInt("Settings", "DisableCaptureBlt", 0, sPath.c_str());
// Set this process to be the last application to be shut down.
// Check for previous instances of WinVNC!
instancehan=new vncInstHandler;
if (!instancehan->TeamViewer3())
{
MessageBox(NULL,sz_IDS_TEAMVIEWER3_RUNNING,"TeamViewer",MB_OK);
return 0;
}
if (!instancehan->Init())
{
if(command!=NULL && *command=="1")
{
PCOPYDATASTRUCT data=new COPYDATASTRUCT();
data->lpData=(PVOID)commandLine;
data->cbData=strlen((char*)data->lpData)+1;
HWND menuHandle=FindWindow("TeamViewer Tray Icon","TeamViewer Tray Icon");
SendMessage(menuHandle,WM_COPYDATA,NULL,(LPARAM)data);
Sleep(5000);
return 0;
}
else if(command!=NULL && *command=="rc")
{
HWND menuHandle=FindWindow("TeamViewer Tray Icon","TeamViewer Tray Icon");
SendMessage(menuHandle,WM_ENTER_REMOTE_CONTROL_MODE,NULL,(LPARAM)0);
Sleep(5000);
return 0;
}
else if (!AllowMulti)
{
// We don't allow multiple instances!
// TR@2003: Request other instance to show Request-Connection-Window
HWND otherVNC=FindWindow("TeamViewer Tray Icon","TeamViewer Tray Icon");
if (otherVNC)
PostMessage(otherVNC,WM_COMMAND,(WPARAM)ID_OUTGOING_CONN,(LPARAM)0);
//MessageBox(NULL, "Another instance of WinVNC is already running", szAppName, MB_OK);
return 0;
}
}
// set global Teamviewer infos
TeamviewerInfo::setLocalInfo(tvVersion, TEAMVIEWER_VERSION);
// CREATE SERVER
vncServer server;
// Set the name and port number
server.SetName(szAppName);
vnclog.Print(LL_STATE, VNCLOG("server created ok"));
///uninstall driver before cont
bmpBackgroundDialog = (HBITMAP)LoadImage(GetModuleHandle(NULL),
MAKEINTRESOURCE(IDB_BITMAP_BACKGROUND_DIALOG),
IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
menuBrush = CreateSolidBrush(MENU_NORMAL); // define brushes to paint main menu
menuSelectedBrush = CreateSolidBrush(MENU_SELECTED);
// Create tray icon & menu if we're running as an app
menu= new vncMenu(&server);
if (menu == NULL)
{
vnclog.Print(LL_INTERR, VNCLOG("failed to create tray menu"));
PostQuitMessage(0);
return 0;
}
menu->Init();
VNCviewerApp32::Init(hAppInstance, "", &server);
if(command!=NULL && (*command)=="installation")
{
int autostart;
string clear, crypt;
if((*args)["service"] == "1")
autostart = 2;
else
autostart = 1;
if((*args).find("password") != (*args).end())
{
string clear=(*args)["password"];
crypt = vncPasswd::FromText((char*)clear.c_str());
}
menu->m_properties.SaveHostMode(autostart, crypt.c_str());
server.SetServerMode(true);
server.SetAutostartSetting(autostart);
server.SetPassword(crypt.c_str());
}
InitScaling();
menu->Show(server.GetServerMode()||isRemoteControl);
SessionDialog sessionDialog(&server);
if(command!=NULL && *command=="1")
{
SessionDialog::Hide();
Sleep(3000);
sessionDialog.DoDialog();
if(menu)
{
PCOPYDATASTRUCT data=new COPYDATASTRUCT();
data->lpData=(PVOID)commandLine;
data->cbData=strlen((char*)data->lpData);
SendMessage(menu->GetHWND(),WM_COPYDATA,NULL,(LPARAM)data);
}
}
else
{
sessionDialog.DoDialog();
}
screenSaverDisabled = DisableScreenSaver();
// Now enter the message handling loop until told to quit!
MSG msg;
while (GetMessage(&msg, NULL, 0,0) && (!server.Shutdown()/*||ViewerAppCounter>0*/) ) {
//vnclog.Print(LL_INTINFO, VNCLOG("Message %d received"), msg.message);
if(!IsDialogMessage(SessionDialog::Hwnd(), &msg)) // Required for keyboard control of dialog
{
TranslateMessage(&msg); // convert key ups and downs to chars
DispatchMessage(&msg);
}
}
vnclog.Print(LL_STATE, VNCLOG("shutting down server"));
if(screenSaverDisabled)
EnableScreenSaver();
if (menu != NULL)
{
delete menu;
menu = NULL;
}
// Shut down DynGate
if (server.UseDynGate())
{
CroGateway::Instance()->Shutdown();
}
delete CroGateway::Instance();
delete VNCviewerApp32::Instance();
if (instancehan!=NULL)
delete instancehan;
if(bmpBackgroundDialog)
DeleteObject(bmpBackgroundDialog);
if(menuBrush)
DeleteObject(menuBrush);
if(menuSelectedBrush)
DeleteObject(menuSelectedBrush);
return msg.wParam;
};
char* FindXmlOption(char* text,char* tagName)
{
char* startTag=new char[strlen(tagName)+5];
char* endTag=new char[strlen(tagName)+5];
strcpy(startTag,"<");
strcat(startTag,tagName);
strcat(startTag,">");
strcpy(endTag,"</");
strcat(endTag,tagName);
strcat(endTag,">");
char* option;
char* start=strstr(text,startTag);
char* end=strstr(text,endTag);
if(start!=NULL && end!=NULL)
{
start+=strlen(startTag);
int length=end-start;
option=new char[length+5];
memcpy(option,start,length);
option[length]=NULL;
}
else
{
option=NULL;
}
delete startTag;
delete endTag;
return option;
}
void Tokenize(const string& str,
vector<string>& tokens)
{
if(str!="")
{
int start=0;
int end=0;
int first=str.find_first_not_of(" ");
first= first==string::npos?first:0;
int last=str.find_last_not_of(" ");
last= last==string::npos?last:str.length();
string text=" "+str.substr(first,last)+" ";
while(true)
{
int stringStart=text.find(" \"",end);
int normalStart=text.find(" ",end);
if(normalStart==stringStart)
{
start=stringStart+2;
end=text.find("\" ",start);
}
else
{
start=normalStart+1;
end=text.find(" ",start+1);
}
if(start!=string::npos && end!=string::npos)
{
tokens.push_back(text.substr(start,end-start));
}
else
{
break;
}
}
}
}
// disable screensaver if required (screensaver is active, password protected and TV is not running as service)
// returns: true: screensaver has been disabled, false otherwise
bool DisableScreenSaver()
{
if(vncService::RunningAsService()) // running as service: no need to disable screensaver
return false;
// test if screensaver is active
BOOL isActive;
if(!SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &isActive, 0))
return false;
if(!isActive) // not active -> no need to disable it
return false;
// test if screensaver is password protected
HKEY hKey;
int err;
if ((err=RegOpenKeyEx(HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, KEY_READ, &hKey)) == ERROR_SUCCESS)
{
char szValue[80];
DWORD dwBufLen=80;
LONG lRet;
lRet = RegQueryValueEx( hKey, "ScreenSaverIsSecure", NULL, NULL, (LPBYTE) szValue, &dwBufLen);
if(lRet == ERROR_SUCCESS)
{
if(atoi(szValue) == 0) // not password protected -> do not disable screensaver
return false;
}
// if there was an error when reading registry disable screensaver
}
return SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, FALSE, 0, 0) != 0;
}
// enable screensaver
void EnableScreenSaver()
{
SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, TRUE, 0, 0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -