Home > Back-end >  20 qb for code to close the progress of the specified port
20 qb for code to close the progress of the specified port

Time:10-08

My application need to use port 80, but some order to take up the port, such as IIS - mine and so on have no way to turn off the 80 port directly, my program to use!

Strives for the code,,,,,

CodePudding user response:

The original poster can be invoked by a DOS command netstat -a access to port usage and processes, and then find the process should be killed can
Call DOS window to get the return content function; NeedClose can remove
The function getCMD (CMD: String; NeedClose: Boolean=true) : String;
Var
Readhdl writehdl: dwords.
Sa: TSecurityAttributes;
Stifo: _STARTUPINFOA;
Pinfo: _PROCESS_INFORMATION;
Tmbuff: an array of char [0.. 254];
Readlen: DWORD;
ExitCode: dword;
NCount, MaxCount: integer;
The begin
Result:=';
Sa. NLength:=sizeof (TSecurityAttributes);
Sa. BInheritHandle:=TRUE;
Sa. LpSecurityDescriptor:=nil;
If not CreatePipe (readhdl writehdl, @ sa, 0) then the exit;
GetStartupInfo (stifo);
Stifo. HStdOutput:=writehdl;
Stifo. HStdError:=writehdl;
If needClose then stifo. WShowWindow:=SW_Hide
The else stifo. WShowWindow:=SW_SHOWNORMAL;
Stifo. DwFlags:=STARTF_USESHOWWINDOW + STARTF_USESTDHANDLES;
MaxCount:=120;
NCount:=0;
If CreateProcess (nil, PChar (CMD + CMD. Exe/C), nil, nil, true, 0, nil, nil, stifo, pinfo) then
The begin
While (GetExitCodeProcess (pinfo hProcess, ExitCode)) and (ExitCode=STILL_ACTIVE) and (nCount & lt; MaxCount) do
The begin
Sleep (500);
Inc (nCount);
Application. ProcessMessages;
end;

If needClose then
The begin
If not (GetExitCodeProcess (pinfo hProcess, ExitCode)) or (ExitCode=STILL_ACTIVE) then//time out, mannualy close process
The TerminateProcess (pinfo. HProcess, 0);
end;

WriteFile (writehdl, '@ # @ # @', 5, readlen, nil);
The CloseHandle (writehdl);
Readlen:=254;
While readlen=254 do
The begin
ReadFile (readhdl tmbuff, 254, readlen, nil);
If readlen & lt;> 254 then tmbuff [readlen - 5] :=CRH (0)
The else tmbuff [] readlen - 1:=CRH (0);
Result: the Result=+ String (tmbuff);
end;
end;
The CloseHandle (readhdl);
end;

Kill the process function:
The function FindAndKillTask (aName: String; AFlag: Boolean=false) : Integer;
Const
PROCESS_TERMINATE=$0001;
Var
ContinueLoop: BOOLean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
LName1 lName2: String;
The begin
Result:=1;
FSnapshotHandle:=CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
FProcessEntry32. DwSize:=SizeOf (FProcessEntry32);

Try
ContinueLoop:=Process32First (FSnapshotHandle FProcessEntry32);
While the Integer (ContinueLoop) & lt;> 0 do
The begin
LName1:=UpperCase (ExtractFileName (FProcessEntry32 szExeFile));
LName2:=UpperCase (FProcessEntry32. SzExeFile);
If ((pos (UpperCase (aName), lName1) & gt; 0) or (pos (UpperCase (aName), lName2) & gt; 0)) then
The begin
If aFlag then
The begin
If messagebox (application. Handle, 'confirmed to exit the program? ', 'confirmed', MB_OKCANCEL + + MB_SYSTEMMODAL MB_ICONQUESTION)=IDOK then
The begin
Result:=Integer (TerminateProcess (
The OpenProcess (PROCESS_TERMINATE,
BOOL (0),
FProcessEntry32 th32ProcessID),
0));
end;
End
The else
Result:=Integer (TerminateProcess (
The OpenProcess (PROCESS_TERMINATE,
BOOL (0),
FProcessEntry32 th32ProcessID),
0));
sleep(1000);
end;
ContinueLoop:=Process32Next (FSnapshotHandle FProcessEntry32);
end;
The finally
The CloseHandle (FSnapshotHandle);
end;
end;

The function KillTaskByPId (pId: integer) : integer;
Const
PROCESS_TERMINATE=$0001;
Var
ContinueLoop: BOOLean;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
The begin
Result:=1;
FSnapshotHandle:=CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
FProcessEntry32. DwSize:=SizeOf (FProcessEntry32);

Try
ContinueLoop:=Process32First (FSnapshotHandle FProcessEntry32);
While the Integer (ContinueLoop) & lt;> 0 do
The begin
If (FProcessEntry32. Th32ProcessID=pId) then
The begin
Result:=Integer (TerminateProcess (
The OpenProcess (PROCESS_TERMINATE,
BOOL (0),
FProcessEntry32 th32ProcessID),
0));
If the result=0 then FLog. The Add (+ inttostr 'kill the process' (pId) + 'success')
Else FLog. The Add (+ inttostr 'process' (pId) + 'failed to kill');
sleep(1000);
break;
end;
ContinueLoop:=Process32Next (FSnapshotHandle FProcessEntry32);
end;
The finally
The CloseHandle (FSnapshotHandle);
end;
end;

CodePudding user response:

To learn!

CodePudding user response:

reference 1st floor beiguofengguang response:
the original poster can be invoked by a DOS command netstat -a access to port usage and processes, and then find the process should be killed can
Call DOS window to get the return content function; NeedClose can remove
The function getCMD (CMD: String; NeedClose: Boolean=true) : String;
Var
Readhdl writehdl: dwords.
Sa: TSecurityAttributes;
Stifo: _STARTUPINFOA;
Pinfo: _PROCESS_INFORMATION;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related