unit winntService;
interface
uses
Windows,WinSvc,WinSvcEx;
function InstallService(const strServiceName,strDisplayName,strDescription,strFilename: string):Boolean;
procedure UninstallService(strServiceName:string);
implementation
function StrLCopy(Dest: PChar; const Source: PChar; MaxLen: Cardinal): PChar; assembler;
asm
PUSH EDI
PUSH ESI
PUSH EBX
MOV ESI,EAX
MOV EDI,EDX
MOV EBX,ECX
XOR AL,AL
TEST ECX,ECX
JZ @@1
REPNE SCASB
JNE @@1
INC ECX
@@1: SUB EBX,ECX
MOV EDI,ESI
MOV ESI,EDX
MOV EDX,EDI
MOV ECX,EBX
SHR ECX,2
REP MOVSD
MOV ECX,EBX
AND ECX,3
REP MOVSB
STOSB
MOV EAX,EDX
POP EBX
POP ESI
POP EDI
end;
function StrPCopy(Dest: PChar; const Source: string): PChar;
begin
Result := StrLCopy(Dest, PChar(Source), Length(Source));
end;
function InstallService(const strServiceName,strDisplayName,strDescription,strFilename: string):Boolean;
var
//ss : TServiceStatus;
//psTemp : PChar;
hSCM,hSCS:THandle;
Service: SC_HANDLE;
srvdesc : PServiceDescription;
desc : string;
//SrvType : DWord;
lpServiceArgVectors:pchar;
begin
Result:=False;
hSCM:=OpenSCManager(nil,nil,SC_MANAGER_ALL_ACCESS);
if hSCM=0 then Exit;
Service := OpenService(hSCM, Pchar(strServiceName), SERVICE_ALL_ACCESS);
if Service<=0 then
begin
hSCS:=CreateService(
hSCM,
Pchar(strServiceName),
Pchar(strDisplayName),
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS or SERVICE_INTERACTIVE_PROCESS,
SERVICE_AUTO_START,
SERVICE_ERROR_IGNORE,
Pchar(strFilename),
nil,
nil,
nil,
nil,
nil);
if hSCS=0 then Exit;//MessageBox(hHandle,Pchar(SysErrorMessage(GetLastError)),Pchar(Application.Title),MB_ICONERROR+MB_TOPMOST);
if Assigned(ChangeServiceConfig2) then
begin
desc := Copy(strDescription,1,1024);
GetMem(srvdesc,SizeOf(TServiceDescription));
GetMem(srvdesc^.lpDescription,Length(desc) + 1);
try
StrPCopy(srvdesc^.lpDescription, desc);
ChangeServiceConfig2(hSCS,SERVICE_CONFIG_DESCRIPTION,srvdesc);
finally
FreeMe
临界区是一段代码,一次只允许一个线程执行这段代码。当把一段代码放入一个临界区, 线程执行到临界区时就独占了, 其他线程如果要访问这段代码,一定要等前一个访问的线程结束才行。借用前面的比喻比作图书馆,临界区就像把图书馆设计成只有一个位置(现实中好像不太合理,不管了,反正这么个意思),当有一个人正在办 ......
上文中,多线程同步主窗体的Label的Caption属性值,发现一个问题:使用Synchronize用于同步的时候,主窗体好像死掉一样;而直接用子程序为Label的引用赋值,则有时会出现“Canvas does not allow drawing”错误。书上说VCL同步一定要用Synchronize,而不能直接访问。
测试:
{主窗体} ......