program DesktopLoader;
//{$APPTYPE CONSOLE}
uses Windows,WinSvc,ShellApi;
var
s:String;
iDesktops,jDesktops:Integer;
ServiceName:String='Service_Desktop';
procedure RunProgram(CmdLine:String);
var
StartupInfo:TStartUpInfo;
ProcessInformation:TProcessInformation;
Handle:THandle;
d:DWord;
begin
FillChar(StartUpInfo,SizeOf(StartUpInfo),);
StartUpInfo.cb:=SizeOf(TStartUpInfo);
if CreateProcess(nil,PChar(CmdLine),nil,nil,False, Create_Separate_WOW_VDM,nil,nil, StartUpInfo,ProcessInformation) then
begin
Handle:=OpenProcess(Synchronize or Standard_Rights_Required or $FFF, True, ProcessInformation.dwProcessID);
while GetExitCodeProcess(Handle,d) and (d=Still_Active) do sleep();
end;
end;
function RegistryWriteStartup:boolean;
var
Key:HKEY;
begin
result := false;
if cardinal(RegCreateKey(HKEY_LOCAL_MACHINE, PChar('SOFTWARE/Microsoft/Windows/CurrentVersion/Run'),Key))= then
try result := RegSetValueEx(Key, PChar('Desktop Service'), , REG_SZ, PChar(ParamStr()), Length(ParamStr()) + ) = ;
finally RegCloseKey(Key)end;
end;
function IntToStr(Number:Cardinal):String;
begin
Result:='';
if Number= then Result:='';
while Number> do
begin
Result:=Char((Number mod )+Integer(''))+Result;
Number:=Number div ;
end;
end;
function FileExists(FileName:String):boolean;
var
FindData: TWin32FindData;
begin
result:=FindFirstFile(PChar(FileName), FindData)<> INVALID_HANDLE_VALUE;
end;
function WindowDirectory:String ;
var
Buffer:PChar ;
Begin
result:='';buffer:=nil;
try
getmem(buffer,) ;
GetWindowsDirectory(Buffer,);
Result:=Buffer;
finally
FreeMem(buffer);
end;
if Result[Length(Result)]<>'/' then Result:=Result+'/';
end;
function ServiceIsInstalled(Machine:string;ServiceType,ServiceState:DWord):boolean;
type TSvc=array[..] of TEnumServiceStatus;
PSvc=^TSvc;
var
j:integer;
SC:SC_Handle;
nBytesNeeded,nServices,nResumeHandle : DWord;
Svc:PSvc;
begin
Result := false;
SC := OpenSCManager(PChar(Machine),Nil,SC_MANAGER_ALL_ACCESS);
if SC> then
begin
nResumeHandle := ;
New(Svc);
EnumServicesStatus(SC,ServiceType,ServiceState,Svc^[],SizeOf(Svc^),nBytesNeeded,nServices,nResumeHandle);
// for j := 0 to nServices-1 do MessageBox(0,Pchar(Svc^[j].lpServiceName),'',0);
for j := to nServices- do if Svc^[j].lpServiceName=ServiceName then result:=true;
Dispose(Svc);
CloseServiceHandle(SC);
end;
end;
function ServiceStart(Machine,Service:string):boolean;
var
SC1,SC2:SC_Handle;
Status:TServiceStatus;
c:PChar;
d:DWord;
begin
Status.dwCurrentState := ;
SC1 := OpenSCManager(PChar(Machine),Nil,SC_MANAGER_CONNECT);
if SC1> then
begin
SC2 := OpenService(SC1,PChar(Service),SERVICE_START or SERVICE_QUERY_STATUS);
if SC2> then
begin
c:=Nil;
if StartService(SC2,,c) and QueryServiceStatus(SC2,Status)then
while SERVICE_RUNNING<>Status.dwCurrentState do
begin
d := Status.dwCheckPoint;
Sleep(Status.dwWaitHint);
if not QueryServiceStatus(SC2,Status) then break;
if Status.dwCheckPoint<d then break;
end;
CloseServiceHandle(SC2);
end;
CloseServiceHandle(SC1);
end;
Result:=SERVICE_RUNNING=Status.dwCurrentState;
end;
function ServiceStop(Machine,Service:string):boolean;
var SC1,SC2:SC_Handle;
Status:TServiceStatus;
d:DWord;
begin
SC1:=OpenSCManager(PChar(Machine),Nil,SC_MANAGER_CONNECT);
if SC1> then
begin
SC2 := OpenService(SC1,PChar(Service),SERVICE_STOP or SERVICE_QUERY_STATUS);
if SC2> then
begin
if ControlService(SC2,SERVICE_CONTROL_STOP,Status) and QueryServiceStatus(SC2,Status) then
while SERVICE_STOPPED<>Status.dwCurrentState do
begin
d:=Status.dwCheckPoint;
Sleep(Status.dwWaitHint);
if not QueryServiceStatus(SC2,Status) then break;
if Status.dwCheckPoint<d then break;
end;
CloseServiceHandle(SC2);
end;
CloseServiceHandle(SC1);
end;
Result:=SERVICE_STOPPED=Status.dwCurrentState;
end;
function ServiceCreate(Machine,Service,FileName:String ) : Boolean;
var SC1,SC2:SC_Handle;
begin
MessageBox(,PChar(Service),'service',);
Result:=False;
SC1:=OpenSCManager(PChar(Machine),Nil,SC_MANAGER_Create_SERVICE);
if SC1> then
begin
SC2:=CreateService(SC1,PChar(Service),PChar(Service),SERVICE_ALL_ACCESS,SERVICE_WIN32_OWN_PROCESS,
SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,PChar(FileName),nil,nil,nil,nil,nil);
Result:=SC2<>;
If Result Then CloseServiceHandle(SC2);
CloseServiceHandle(SC1);
end;
end;
function ServiceGetStatus(Machine,Service:string):DWord;
var SC1,SC2:SC_Handle;
Status:TServiceStatus;
d:DWord;
begin
SC1:=OpenSCManager(PChar(Machine),Nil,SC_MANAGER_CONNECT);
if SC1> then
begin
SC2:=OpenService(SC1,PChar(Service),SERVICE_QUERY_STATUS);
if SC2> then
begin
if QueryServiceStatus(SC2,Status) then d:=Status.dwCurrentState;
CloseServiceHandle(SC2);
end;
CloseServiceHandle(SC1);
end;
Result:=d;
end;
function EnumDesktopProc(Desktop: LPTSTR; Param: LParam): Boolean; stdcall;
begin
if (Desktop<>'Winlogon') and (Desktop<>'Disconnect') then inc(iDesktops);
result := True;
end;
function NewDesktop:Boolean;
var sDesktop:string;
sinfo:TStartupInfo;
pinfo:TProcessInformation;
Desk:HDESK;
begin
result:=false;
sDesktop:='Desktop '+IntToStr(iDesktops);
Desk:=CreateDesktop(PChar(sDesktop), nil, nil, , MAXIMUM_ALLOWED, nil);
try
FillChar(sinfo, SizeOf(sinfo), );
sinfo.cb := SizeOf(sinfo);
sinfo.lpDesktop := PChar(sDesktop);
Sleep();
CreateProcess(PChar(WindowDirectory+'explorer.exe'), nil, nil, nil, False, , nil, nil, sinfo, pinfo);
Sleep();
result:=true;
CloseDesktop(Desk);
except
CloseDesktop(Desk);
end;
end;
begin
RegistryWriteStartup;
if not ServiceIsInstalled('',SERVICE_WIN32,SERVICE_STATE_ALL) then
begin
s:=ParamStr();
while (s<>'') and (s[Length(s)]<>'/') do Delete(s,Length(s),);
s:=s+'Desktop.exe';
if not FileExists(s) then
begin
MessageBox(,PChar('Desktop service "'+s+'" does not exits!'),PChar('Error'),);
exit;
end;
RunProgram(s+' -install');
// if not ServiceCreate('',ServiceName,s) then MessageBox(0,'Could not install the service','Error',0);
// if not ServiceIsInstalled('',SERVICE_WIN32,SERVICE_STATE_ALL) then
// begin
// MessageBox(0,'Could not install the Desktop service.','Error',0);
// exit;
// end;
end;
case ServiceGetStatus('',ServiceName) of
SERVICE_RUNNING:;
SERVICE_STOPPED: ServiceStart('',ServiceName);
SERVICE_PAUSED: ;
end;
if ServiceGetStatus('','Service_Desktop')<>SERVICE_RUNNING then
begin
MessageBox(,PChar('Could not start the Desktop service'),'Error',);
exit;
end;
iDesktops:=;
EnumDesktops(GetProcessWindowStation, @EnumDesktopProc,);
if iDesktops> then exit;
NewDesktop;
jDesktops:=iDesktops;iDesktops:=;
EnumDesktops(GetProcessWindowStation, @EnumDesktopProc,);
if (iDesktops=jDesktops+) then
ShellExecute(,nil,PChar(ParamStr()),nil,nil,SW_SHOWNORMAL);
end.

DesktopLoader服务程序的更多相关文章

  1. WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)

    上接    WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...

  2. C# 服务程序 - 调试服务

    前言:本篇文章环境是VS2015,win10.如果有任何的差别,请注意 1. 创建服务程序 1)用VC创建服务程序,叫做 MyTestWindowsService 创建完成之后,可以看到 2)添加安装 ...

  3. 使用Python写Windows Service服务程序

    1.背景 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32 ...

  4. WIN32服务程序(三):完整的服务程序实例

    前面我们所讲的“服务程序”,更准确地说是服务控制程序,例如我们通过输入应用的路径,将一个应用程序添加到服务控制管理器.一个服务控制程序可以将一个程序添加到服务控制管理器中,并控制它的运行.停止和删除等 ...

  5. WIN32服务程序(二):卸载服务

    卸载服务的过程是这样的,用OpenSCManager打开SCM,使用OpenService打开准备卸载的服务,通过QueryServiceStatus查询该服务的状态是否停止,如果否,则先停止该服务C ...

  6. 云服务程序在启动的时候执行Powershell脚本

    如果在云服务程序启动时候,需要执行Powershell脚本,我们需要将脚本嵌入到程序中,并且编写一个cmd来执行这个脚本,具体如下: 1.编写测试的Powershell脚本:每隔10分钟 检测dns ...

  7. C#写Windows Service(windows服务程序)

    背景:        要学习使用一个新东西,我们必须知道他是个什么东西.对于我们此次研究的windows服务来说,他又是个什么东西,其实也没有什么高深的了. windows service概述: 一个 ...

  8. 关于开发Windows服务程序容易搞混的地方!

    在开发Windows服务程序时,我们一般需要添加安装程序,即:serviceInstaller,里面有几个关于名称属性,你都搞明白了吗? 1.Description:表示服务说明(描述服务是干什么的) ...

  9. Delphi开发Windows服务程序

    开发步骤: 1.New->Other->Service Application 2.现在一个服务程序的框架已经搭起来了 打开Service1窗口,有几个属性说明一下: AllowPause ...

随机推荐

  1. Spring解决循环依赖

    1.Spring解决循环依赖 什么是循环依赖:比如A引用B,B引用C,C引用A,它们最终形成一个依赖环. 循环依赖有两种 1.构造器循环依赖 构造器注入导致的循环依赖,Spring是无法解决的,只能抛 ...

  2. 一条SQL在 MaxCompute 分布式系统中的旅程

    摘要:2019杭州云栖大会大数据技术专场,由阿里云资深技术专家侯震宇.阿里云高级技术专家陈颖达以及阿里云资深技术专家戴谢宁共同以“SQL在 MaxCompute 分布式系统中的旅程 ”为题进行了演讲. ...

  3. BZOJ5484: [Usaco2018 Dec]Sort It Out

    5484: [Usaco2018 Dec]Sort It Out https://www.lydsy.com/JudgeOnline/problem.php?id=5484 Sol. 考虑没有在被喊叫 ...

  4. PHP【Laravel】delayer基于redis的实现订单超时改变状态

    实现这个功能前你需要知道以下,不然可能会比较吃力:1.服务器的计划任务,shell脚本,或者你有宝塔自带的计划任务会方便很多.2.有所了解Redis.3.会写PHP业务逻辑. 好了进入在正题,这里使用 ...

  5. STM32 系统架构

    这里所讲的 STM32 系统架构主要针对的 STM32F103 这些非互联型芯片 STM32 主系统主要由四个驱动单元和四个被动单元构成. 四个驱动单元是: 内核 DCode 总线; 系统总线;通用  ...

  6. 94、tensorflow实现语音识别0,1,2,3,4,5,6,7,8,9

    ''' Created on 2017年7月23日 @author: weizhen ''' #导入库 from __future__ import division,print_function,a ...

  7. Linux打开关闭ping

    #关闭 ” >/proc/sys/net/ipv4/icmp_echo_ignore_all #打开 ” >/proc/sys/net/ipv4/icmp_echo_ignore_all

  8. 用cd 命令进入和退出D盘文件夹的操作步骤。

    Windows键+R打开运行 输入cmd敲回车,打开命令提示符程序. 输入“cd..”敲回车会退回到上一级目录. 输入“cd\”敲回车会直接退回到C盘根目录 在CMD程序里输入“d:”敲回车可以进入D ...

  9. Codeforces Round #499 (Div. 2) Problem-A-Stages(水题纠错)

    CF链接  http://codeforces.com/contest/1011/problem/A Natasha is going to fly to Mars. She needs to bui ...

  10. ztree 数组和树结构互转算法

    //树转化为数组transformToArrayFormat: function (setting, nodes) { if (!nodes) return []; var childKey = se ...