Home > Back-end >  The service start another program unbounded problems below.
The service start another program unbounded problems below.

Time:09-16

I recently doing a service, the service start another exe program, but there is no interface starts, to find a lot of materials, but the compiler is not successful, please directly,
 {Function GetProcessHandleAsName (Name: String) : THandle; 
Var
Hd, Hs: THandle;
DExit: Cardinal;
Tmp, Tmp1: String;
Lp: TProcessEntry32;
The begin
Result:=0;
Lp. DwSize:=sizeof (TProcessEntry32);
Hd:=CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
If Process32First (Hd, Lp) then
Repeat
Tmp:=UpperCase (Trim (Name));
Tmp1:=Trim (UpperCase (Lp) szExeFile));
If AnsiPos (Tmp, Tmp1) & gt; 0 then
The begin
Result:=OpenProcess (PROCESS_ALL_ACCESS, true, Lp. Th32ProcessID);
break;
End
Until Process32Next (Hd, Lp)=False;
end;

The Function CreateProc (ProcessName: String) : Boolean;
Var
SiStartupInfo: STARTUPINFO;
SaProcess saThread: SECURITY_ATTRIBUTES;
PiProcInfo: PROCESS_INFORMATION;
Hd: NativeInt;
ProcessHd: THandle;
Hds: THandle;
Str: String;
The begin
Result:=false;
ProcessHd:=GetProcessHandleAsName (" Explorer ");
If ProcessHd=0 then the Exit;
Then if OpenProcessToken (ProcessHd TOKEN_ALL_ACCESS, Hds)
If DuplicateTokenEx (Hds, TOKEN_ALL_ACCESS, nil, SecurityIdentification, TokenPrimary, hd) then
The begin
ZeroMemory (@ siStartupInfo, sizeof (siStartupInfo));
SiStartupInfo. Cb:=sizeof (siStartupInfo);
SaProcess. NLength:=sizeof (saProcess);
SaProcess. LpSecurityDescriptor:=nil;
SaProcess. BInheritHandle:=false;
SaThread. NLength:=sizeof (saThread);
SaThread. LpSecurityDescriptor:=nil;
SaThread. BInheritHandle:=false;
If CreateProcessAsUser (Hd, nil, PChar (ProcessName), nil, nil, false,
CREATE_DEFAULT_ERROR_MODE, nil nil, siStartupInfo piProcInfo) then
Result:=true;
end;
end; }


If DuplicateTokenEx (Hds, TOKEN_ALL_ACCESS, nil, SecurityIdentification, TokenPrimary, hd) then

[dcc32 Error] Unit1. Pas (113) : E2033 Types of actual and formal var parameters must be identical

CodePudding user response:

A second method, the invalid

 function CreateProcessWithLogonW (
LpUsername,//user 's name
LpDomain,//user 's domain
LpPassword: PWideChar;//user 's password
DwLogonFlags: dword;//logon option
The lpApplicationName: PWideChar;
The lpCommandLine: PWideChar;
DwCreationFlags: DWORD;
LpEnvironment: Pointer;
LpCurrentDirectory: PWideChar;
Const lpStartupInfo: tSTARTUPINFO;
Var lpProcessInformation: TProcessInformation
) : BOOL; Stdcall; External 'advapi32. DLL';

The Function RunAsUser (const filename, username, password: string) : Boolean;
Var
StartupInfo: tStartupInfo;
ProcessInfo: TProcessInformation;
Wfilename wusername, wpassword: pwidechar;
The begin
FillChar (StartupInfo, SizeOf (StartupInfo), # 0);
StartupInfo. Cb:=SizeOf (StartupInfo);
StartupInfo. DwFlags:=STARTF_USESHOWWINDOW;
StartupInfo. WShowWindow:=SW_SHOWNORMAL;
GetMem (wfilename, Length (filename) * SizeOf (WideChar) + SizeOf (
WideChar));
GetMem (wusername, length (username) * SizeOf (WideChar) + SizeOf (
WideChar));
GetMem (wpassword, length (password) * SizeOf (WideChar) + SizeOf (
WideChar));
StringToWideChar (filename, wfilename, Length (filename) * SizeOf (
WideChar) + SizeOf (WideChar));
StringToWideChar (username, wusername, Length (username) * SizeOf (
WideChar) + SizeOf (WideChar));
StringToWideChar (password, wpassword, Length (password) * SizeOf (
WideChar) + SizeOf (WideChar));
Result:=CreateProcessWithLogonW (wusername, nil, wpassword, 0, wfilename, nil,
0, nil nil, StartupInfo, ProcessInfo);
Freemem (wfilename);
Freemem (wusername);
Freemem (wpassword);
end;


RunAsUser (strPath, 'user', 'pass');//here fill in the operating system user name and password

This method doesn't work, can't start the program,

CodePudding user response:

The function RunProcess (const ProcessName: String) : Boolean;
The function CheckProcessHd (const aFileName: string) : THandle;
Var
HSnapshot: THandle;
Lppe: TProcessEntry32;
Found: Boolean;
The begin
Result:=0;
HSnapshot:=CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
Lppe. DwSize:=sizeof (TProcessEntry32);
Found:=Process32First (hSnapshot lppe);
While the Found do
The begin
If ((UpperCase (ExtractFileName (lppe szExeFile))=UpperCase (aFileName)) or
(UpperCase (lppe szExeFile)=UpperCase (aFileName))) then
The begin
Result:=OpenProcess (PROCESS_ALL_ACCESS, true, lppe th32ProcessID);
The CloseHandle (hSnapshot);
The Exit;
end;
Found:=Process32Next (hSnapshot lppe);
end;
The CloseHandle (hSnapshot);
end;

Var
SiStartupInfo: STARTUPINFO;
SaProcess saThread: SECURITY_ATTRIBUTES;
PiProcInfo: PROCESS_INFORMATION;
//Hd: Cardinal;
HD: THandle;
ProcessHd: THandle;
Hds: THandle;
//Str: String;
The begin
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related