mirror of
https://gitlab.com/freepascal.org/lazarus/lazarus.git
synced 2025-08-25 03:01:42 +02:00
simplewebservergui: fixed run on older windows, loading GetTcpTable2 via GetProcAddress
This commit is contained in:
parent
79893d2cbe
commit
bb83fdd273
@ -75,8 +75,8 @@ type
|
|||||||
dwNumEntries: DWORD;
|
dwNumEntries: DWORD;
|
||||||
table: array [0..ANY_SIZE - 1] of MIB_TCPROW2;
|
table: array [0..ANY_SIZE - 1] of MIB_TCPROW2;
|
||||||
end;
|
end;
|
||||||
function GetTcpTable2(pTcpTable: PMIB_TCPTABLE2; var pdwSize: DWORD; bOrder: BOOL): DWORD; stdcall; external 'iphlpapi' name 'GetTcpTable2';
|
function GetTcpTable2(pTcpTable: PMIB_TCPTABLE2; var pdwSize: DWORD; bOrder: BOOL; out aResult: DWord): boolean;
|
||||||
function GetModuleFilenameExW(hndProcess: HANDLE; hndModule: HMODULE; lpFilename: LPWSTR; nSize: DWord): DWord; stdcall; external 'psapi' name 'GetModuleFileNameExW';
|
function GetModuleFilenameExW(hndProcess: HANDLE; hndModule: HMODULE; lpFilename: LPWSTR; nSize: DWord; out aResult: DWord): boolean;
|
||||||
{$ENDIF}
|
{$ENDIF}
|
||||||
|
|
||||||
function MaybeQuote(S : String) : String;
|
function MaybeQuote(S : String) : String;
|
||||||
@ -85,6 +85,48 @@ function GetNextIPPort(Port: word): word;
|
|||||||
|
|
||||||
implementation
|
implementation
|
||||||
|
|
||||||
|
{$IFDEF MSWindows}
|
||||||
|
type
|
||||||
|
TGetTcpTable2 = function(pTcpTable: PMIB_TCPTABLE2; var pdwSize: DWORD; bOrder: BOOL): DWORD; stdcall;
|
||||||
|
var
|
||||||
|
IPHlpApiLibLoaded: boolean = false;
|
||||||
|
IPHlpApiLibHandle: HMODULE = 0;
|
||||||
|
GetTcpTable2Func: TGetTcpTable2 = nil;
|
||||||
|
function GetTcpTable2(pTcpTable: PMIB_TCPTABLE2; var pdwSize: DWORD; bOrder: BOOL; out aResult: DWord): boolean;
|
||||||
|
begin
|
||||||
|
if not IPHlpApiLibLoaded then
|
||||||
|
begin
|
||||||
|
IPHlpApiLibLoaded:=true;
|
||||||
|
IPHlpApiLibHandle := SafeLoadLibrary('iphlpapi.dll');
|
||||||
|
if IPHlpApiLibHandle=0 then exit(false);
|
||||||
|
GetTcpTable2Func := TGetTcpTable2(GetProcAddress(IPHlpApiLibHandle, PChar('GetTcpTable2')));
|
||||||
|
end;
|
||||||
|
if GetTcpTable2Func=nil then exit(false);
|
||||||
|
Result:=true;
|
||||||
|
aResult:=GetTcpTable2Func(pTcpTable,pdwSize,bOrder);
|
||||||
|
end;
|
||||||
|
|
||||||
|
type
|
||||||
|
TGetModuleFilenameExW = function(hndProcess: HANDLE; hndModule: HMODULE; lpFilename: LPWSTR; nSize: DWord): DWord; stdcall;
|
||||||
|
var
|
||||||
|
psapiLibLoaded: boolean = false;
|
||||||
|
psapiLibHandle: HMODULE = 0;
|
||||||
|
GetModuleFilenameExWFunc: TGetModuleFilenameExW = nil;
|
||||||
|
function GetModuleFilenameExW(hndProcess: HANDLE; hndModule: HMODULE; lpFilename: LPWSTR; nSize: DWord; out aResult: DWord): boolean;
|
||||||
|
begin
|
||||||
|
if not psapiLibLoaded then
|
||||||
|
begin
|
||||||
|
psapiLibLoaded:=true;
|
||||||
|
psapiLibHandle := SafeLoadLibrary('psapi.dll');
|
||||||
|
if psapiLibHandle=0 then exit(false);
|
||||||
|
GetModuleFilenameExWFunc := TGetModuleFilenameExW(GetProcAddress(IPHlpApiLibHandle, PChar('GetModuleFilenameExW')));
|
||||||
|
end;
|
||||||
|
if GetModuleFilenameExWFunc=nil then exit(false);
|
||||||
|
Result:=true;
|
||||||
|
aResult:=GetModuleFilenameExWFunc(hndProcess,hndModule,lpFilename,nSize);
|
||||||
|
end;
|
||||||
|
{$ENDIF}
|
||||||
|
|
||||||
function ReadNext(const Line: string; var p: integer): string;
|
function ReadNext(const Line: string; var p: integer): string;
|
||||||
var
|
var
|
||||||
l: SizeInt;
|
l: SizeInt;
|
||||||
@ -674,14 +716,14 @@ begin
|
|||||||
pTCPTable:=GetMem(aSize);
|
pTCPTable:=GetMem(aSize);
|
||||||
if pTCPTable=nil then exit;
|
if pTCPTable=nil then exit;
|
||||||
try
|
try
|
||||||
r:=GetTcpTable2(pTCPTable,aSize,true);
|
if not GetTcpTable2(pTCPTable,aSize,true,r) then exit;
|
||||||
if r=ERROR_INSUFFICIENT_BUFFER then
|
if r=ERROR_INSUFFICIENT_BUFFER then
|
||||||
begin
|
begin
|
||||||
ReAllocMem(pTCPTable,aSize);
|
ReAllocMem(pTCPTable,aSize);
|
||||||
if pTCPTable=nil then exit;
|
if pTCPTable=nil then exit;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
r:=GetTcpTable2(pTCPTable,aSize,true);
|
GetTcpTable2(pTCPTable,aSize,true,r);
|
||||||
if r<>NO_ERROR then exit;
|
if r<>NO_ERROR then exit;
|
||||||
|
|
||||||
{$R-}
|
{$R-}
|
||||||
@ -707,7 +749,7 @@ begin
|
|||||||
h:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, WINBOOL(false), aPid);
|
h:=OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, WINBOOL(false), aPid);
|
||||||
if h=0 then exit;
|
if h=0 then exit;
|
||||||
try
|
try
|
||||||
aSize:=GetModuleFilenameExW(h,0,@CurExeName[0],length(CurExeName));
|
if not GetModuleFilenameExW(h,0,@CurExeName[0],length(CurExeName),aSize) then exit;
|
||||||
if aSize>0 then
|
if aSize>0 then
|
||||||
begin
|
begin
|
||||||
aDesc:=CurExeName;
|
aDesc:=CurExeName;
|
||||||
@ -753,14 +795,14 @@ begin
|
|||||||
pTCPTable:=GetMem(aSize);
|
pTCPTable:=GetMem(aSize);
|
||||||
if pTCPTable=nil then exit;
|
if pTCPTable=nil then exit;
|
||||||
try
|
try
|
||||||
r:=GetTcpTable2(pTCPTable,aSize,true);
|
if not GetTcpTable2(pTCPTable,aSize,true,r) then exit;
|
||||||
if r=ERROR_INSUFFICIENT_BUFFER then
|
if r=ERROR_INSUFFICIENT_BUFFER then
|
||||||
begin
|
begin
|
||||||
ReAllocMem(pTCPTable,aSize);
|
ReAllocMem(pTCPTable,aSize);
|
||||||
if pTCPTable=nil then exit;
|
if pTCPTable=nil then exit;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
r:=GetTcpTable2(pTCPTable,aSize,true);
|
GetTcpTable2(pTCPTable,aSize,true,r);
|
||||||
if r<>NO_ERROR then exit;
|
if r<>NO_ERROR then exit;
|
||||||
|
|
||||||
Ports:=copy(AvoidPorts);
|
Ports:=copy(AvoidPorts);
|
||||||
|
Loading…
Reference in New Issue
Block a user