📄 vncservice.cpp
字号:
bool vncService::RemoveAutostart()
{
HRESULT hres;
IShellLink* psl;
if SUCCEEDED (CoInitialize(NULL))
{
// Get a pointer to the IShellLink interface.
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER,
IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres))
{
IPersistFile* ppf;
//strcat (szPathObj," -a");
// Set the path to the shortcut target and add the description.
string sPath;
if (!GetSpecialFolderPath(CSIDL_STARTUP,sPath))
{
/*
some paths are not available so we get here.
Reasons :
1. path was removed from registry
2. running as service that does not host the path
- GetSpecialFolderPath tries to impersonate, maybe it failed?
*/
}
sPath += "\\TeamViewer.lnk";
//psl->Resolve
//psl->SetPath(szPathObj);
//psl->SetArguments (roVNCAutoStart);
// Query IShellLink for the IPersistFile interface for saving the
// shortcut in persistent storage.
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres))
{
//WCHAR wsz[MAX_PATH];
if (FAILED(hres) == 0)
{
::DeleteFile(sPath.c_str());
}
// Ensure that the string is Unicode.
//MultiByteToWideChar(CP_ACP, 0, szPath, -1, wsz, MAX_PATH);
// TODO: Check return value from MultiByteWideChar to ensure success.
// Save the link by calling IPersistFile::Save.
//hres = ppf->Save(wsz, TRUE);
ppf->Release();
}
psl->Release();
}
CoUninitialize();
}
return true;
}
//////////// Staudenmeyer
bool vncService::IsInAutostart()
{
string linkPath;
GetSpecialFolderPath(CSIDL_STARTUP,linkPath);
//strcat(linkPath,"\\TeamViewer.lnk");
linkPath += "\\TeamViewer.lnk";
WIN32_FIND_DATA FindFileData2;
HANDLE hFind2;
hFind2 = FindFirstFile(linkPath.c_str(), &FindFileData2);
if (hFind2!=INVALID_HANDLE_VALUE)
{
FindClose(hFind2);
return true;
}
else
{
SetLastError(0); // we don't want the error code to be logged later
return false;
}
}
int vncService::StartService()
{
return false;
/* if (IsWin95())
return false;
if (!getAllowanceServiceStart())
return false;
if (vncService::RunningAsService)
return false;
if (!vncService::InstalledAsService())
{
if (vncService::InstallService(1) != S_OK)
return false;
}
SC_HANDLE hservice;
SC_HANDLE hsrvmanager;
hsrvmanager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hsrvmanager == NULL)
return false;
SERVICE_STATUS_PROCESS ssStatus;
DWORD dwOldCheckPoint;
DWORD dwStartTickCount;
DWORD dwWaitTime;
DWORD dwBytesNeeded;
hservice = OpenService(
hsrvmanager, // SCM database
VNCSERVICENAME, // service name
SERVICE_ALL_ACCESS);
if (hservice == NULL)
{
return 0;
}
if (!::StartService(
hservice, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
return 0;
}
else
{
printf("Service start pending.\n");
}
// Check the status until the service is no longer start pending.
if (!QueryServiceStatusEx(
hservice, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE)&ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
{
return 0;
}
// Save the tick count and initial checkpoint.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
{
// Do not wait longer than the wait hint. A good interval is
// one tenth the wait hint, but no less than 1 second and no
// more than 10 seconds.
dwWaitTime = ssStatus.dwWaitHint / 10;
if( dwWaitTime < 1000 )
dwWaitTime = 1000;
else if ( dwWaitTime > 10000 )
dwWaitTime = 10000;
Sleep( dwWaitTime );
// Check the status again.
if (!QueryServiceStatusEx(
hservice, // handle to service
SC_STATUS_PROCESS_INFO, // info level
(LPBYTE)&ssStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // if buffer too small
break;
if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
{
// The service is making progress.
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ssStatus.dwCheckPoint;
}
else
{
if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
{
// No progress made within the wait hint
break;
}
}
}
CloseServiceHandle(hservice);
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
}
else
{
// printf("\nService not started. \n");
// printf(" Current State: %d\n", ssStatus.dwCurrentState);
// printf(" Exit Code: %d\n", ssStatus.dwWin32ExitCode);
// printf(" Service Specific Exit Code: %d\n",
// ssStatus.dwServiceSpecificExitCode);
// printf(" Check Point: %d\n", ssStatus.dwCheckPoint);
// printf(" Wait Hint: %d\n", ssStatus.dwWaitHint);
// return 0;
return true;
}
CloseServiceHandle(hsrvmanager);
return true;*/
}
bool vncService::getAllowanceServiceStart()
{
if (IsWin95())
return true;
SC_HANDLE hsrvmanager;
hsrvmanager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hsrvmanager == NULL)
return false;
CloseServiceHandle(hsrvmanager);
return true;
}
bool vncService::InstalledAsService()
{
if(IsWinNT())
{
SC_HANDLE schSCManager=OpenSCManager(
NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ENUMERATE_SERVICE);
// SC_MANAGER_ALL_ACCESS); // full access rights
if(schSCManager==NULL)
{
return false;
}
SC_HANDLE schService = OpenService(
schSCManager, // SCM database
VNCSERVICENAME, // service name
SERVICE_QUERY_CONFIG);
//SERVICE_ALL_ACCESS);
if (schService == NULL)
{
CloseServiceHandle(schSCManager);
SetLastError(0); // don't log error code in next vnclog.Print()
return false;
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return true;
}
else
{
HKEY hkey;
if (ERROR_SUCCESS==RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",0,KEY_QUERY_VALUE,&hkey))
{
TCHAR* executablePath=vncProperties::LoadString(hkey,szAppName);
if(vncProperties::LoadString(hkey,szAppName)!=NULL)
{
delete executablePath;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
//if(DynGateService::IsWindowsNT())
//{
// SC_HANDLE schSCManager=DynGateService::GetSCManager();
// SC_HANDLE schService = OpenService(
// schSCManager, // SCM database
// "TeamViewer", // service name
// SERVICE_ALL_ACCESS);
//
// if (schService == NULL)
// {
// return false;
// }
// CloseServiceHandle(schService);
// return true;
//}
//else
//{
// HKEY hkey;
// if (ERROR_SUCCESS==RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Windows\\CurrentVersion\\RunServices",0,KEY_QUERY_VALUE,&hkey))
// {
// if(GC.Load_Reg_String(hkey,"TeamViewer")!="")
// {
// return true;
// }
// else
// {
// return false;
// }
// }
// else
// {
// return false;
// }
//}
}
//bool vncServer::OldServiceInstalled()
//{
// bool result=false;
// char* serviceExecutablePath=GetServiceExecutablePath("winvnc");
// if(strstr(serviceExecutablePath,"TeamViewer")!=NULL)
// {
// result=true;
// delete serviceExecutablePath;
// }
// return result;
//}
//
//bool vncServer::InstalledAsService(char* serviceName)
//{
// bool result=false;
// char* serviceExecutablePath=GetServiceExecutablePath("TeamViewer");
// if(serviceExecutablePath!=NULL)
// {
// result=true;
// delete serviceExecutablePath;
// }
// return result;
//}
//char* vncServer::GetServiceExecutablePath(char* serviceName)
//{
//
//}
//void vncServer::RemoveOldService()
//{
//}
//
//bool vncServer::RemoveAnyService(char* serviceName)
//{
//}
////////////
// USEFUL SERVICE SUPPORT ROUTINES
// Service control routine
void WINAPI ServiceCtrl(DWORD ctrlcode)
{
// What control code have we been sent?
// Beep(1000,10);
// vnclog.Print(LL_INTINFO, VNCLOG("ctrlcode %d "),ctrlcode);
switch(ctrlcode)
{
case SERVICE_CONTROL_SHUTDOWN:
g_srvstatus.dwCurrentState = SERVICE_STOP_PENDING;
// ServiceStop();
serviceshutdown=true;
vnclog.Print(LL_INTERR, VNCLOG("SERVICE_CONTROL_SHUTDOWN"));
break;
case SERVICE_CONTROL_STOP:
// STOP : The service must stop
g_srvstatus.dwCurrentState = SERVICE_STOP_PENDING;
vnclog.Print(LL_INTERR, VNCLOG("SERVICE_CONTROL_STOP"));
ServiceStop();
break;
case SERVICE_CONTROL_INTERROGATE:
// QUERY : Service control manager just wants to know our state
break;
default:
// Control code not recognised
break;
}
// Tell the control manager what we're up to.
ReportStatus(g_srvstatus.dwCurrentState, NO_ERROR, 0);
}
// Service manager status reporting
BOOL ReportStatus(DWORD state,
DWORD exitcode,
DWORD waithint)
{
static DWORD checkpoint = 1;
BOOL result = TRUE;
// If we're in the start state then we don't want the control manager
// sending us control messages because they'll confuse us.
if (state == SERVICE_START_PENDING)
g_srvstatus.dwControlsAccepted = 0;
else
g_srvstatus.dwControlsAccepted = SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN;
// Save the new status we've been given
g_srvstatus.dwCurrentState = state;
g_srvstatus.dwWin32ExitCode = exitcode;
g_srvstatus.dwWaitHint = waithint;
// Update the checkpoint variable to let the SCM know that we
// haven't died if requests take a long time
if ((state == SERVICE_RUNNING) || (state == SERVICE_STOPPED))
g_srvstatus.dwCheckPoint = 0;
else
g_srvstatus.dwCheckPoint = checkpoint++;
// Tell the SCM our new status
if (!(result = SetServiceStatus(g_hstatus, &g_srvstatus)))
LogErrorMsg("SetServiceStatus failed");
return result;
}
// Error reporting
void LogErrorMsg(char *message)
{
char msgbuff[256];
HANDLE heventsrc;
char * strings[2];
// Save the error code
g_error = GetLastError();
// Use event logging to log the error
heventsrc = RegisterEventSource(NULL, VNCSERVICENAME);
_snprintf(msgbuff, 256, "%s error: %d", VNCSERVICENAME, g_error);
strings[0] = msgbuff;
strings[1] = message;
if (heventsrc != NULL)
{
MessageBeep(MB_OK);
ReportEvent(
heventsrc, // handle of event source
EVENTLOG_ERROR_TYPE, // event type
0, // event category
0, // event ID
NULL, // current user's SID
2, // strings in 'strings'
0, // no bytes of raw data
(const char **)strings, // array of error strings
NULL); // no raw data
DeregisterEventSource(heventsrc);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -