您的位置:首页 > 其它

SendMessage操作另一个EXE,存在这,以后也许用得着。

2009-06-28 11:05 344 查看
unit Unit1;

interface

uses
Windows, SysUtils, Classes, Controls, Forms, StdCtrls, IdTCPServer, IdContext,
IdAntiFreeze, IdAntiFreezeBase, IdBaseComponent, IdComponent, IdCustomTCPServer,
IdGlobal, Messages, Dialogs;

type
TfrmMain = class(TForm)
grp4: TGroupBox;
mmoLog: TMemo;
IdTCPServer1: TIdTCPServer;
IdAntiFreeze1: TIdAntiFreeze;
procedure IdTCPServer1Execute(AContext: TIdContext);
procedure FormCreate(Sender: TObject);
private
public
{ Public declarations }
end;

var
frmMain: TfrmMain;
cnt : Integer = 0;
UserNameHandle, UserPassHandle, NewAccountHandle, ResultMemoHandle: Cardinal;

function EnumWindowsProc(Hwnd:THandle;lParam:LParam):boolean;Stdcall;
function EnumChildProc(Hwnd:THandle;lParam:LParam):boolean;Stdcall;

implementation

{$R *.dfm}

uses
Unit_Msg;

function EnumWindowsProc(Hwnd:THandle;lParam:LParam):boolean;
var
WindowCaption:array[0..254] of Char;
begin
GetWindowText(Hwnd,WindowCaption,255);
if StrPas(WindowCaption) = 'CH_Ts2_RELAY MALA NET' then
begin
cnt := 0;
EnumChildWindows(Hwnd,@EnumChildProc,0);
Result := False;
Exit;
end;
Result := True;
end;

function EnumChildProc(Hwnd:THandle;lParam:LParam):boolean;
var
WindowClass:array[0..254] of Char;
begin
GetClassName(Hwnd,WindowClass,255);
begin
Inc(cnt);
case cnt of
25: UserPassHandle := Hwnd;
26: UserNameHandle := Hwnd;
20: NewAccountHandle := Hwnd;
34: ResultMemoHandle := Hwnd; //第一个memo 33是第二个memo
21: ;
end;
//以下这句代码编号所有子控件,可按EDIT选
//if Pos('EDIT',UpperCase(StrPas(WindowClass))) > 0 then
//SendMessage(Hwnd,WM_SETTEXT,0,LongInt(PChar(IntToStr(cnt))));
end;
Result := True;
end;

procedure TfrmMain.IdTCPServer1Execute(AContext: TIdContext);
var
GameName, Pass: string;
arr: array[0..254] of Char;
cmdHead: string;
ReturnMsg: TReturnMsg;
Buff: TIdBytes;
AddUser: T_AddUser; //add user
begin
cmdHead := AContext.Connection.IOHandler.ReadLn;
//注册帐号
if 'AddUser' = cmdHead then begin
AContext.Connection.IOHandler.ReadBytes(Buff, SizeOf(AddUser));
BytesToRaw(Buff, AddUser, SizeOf(AddUser));
GameName := AddUser.uGameName;
Pass := AddUser.uPass;
//发送模拟信息
SendMessage(UserNameHandle, WM_SETTEXT, 0, Integer(PChar(GameName)));
SendMessage(UserPassHandle, WM_SETTEXT, 0, Integer(PChar(Pass)));
SendMessage(NewAccountHandle, BM_CLICK, 0, 0);
Sleep(1000);
//获取memo内容
SendMessage(ResultMemoHandle,WM_GETTEXT,255,Longint(@arr[0]));
SendMessage(ResultMemoHandle, WM_SETTEXT, 0, Integer(PChar('')));
//比较是否成功
if Pos('101 + 1 RESULT: 1 GNAME: ' + GameName, arr) > 0 then begin
ReturnMsg.Tag := SM_AddNewId_Fail;
ReturnMsg.str := Format('%s 帐号已存在!', [GameName]);
Buff := RawToBytes(ReturnMsg, SizeOf(ReturnMsg));
AContext.Connection.IOHandler.Write(Buff);
Exit;
end else if Pos('101 + 1 RESULT: 0 GNAME: ' + GameName, arr) > 0 then begin
ReturnMsg.Tag := SM_AddNewId_Success;
ReturnMsg.str := Format('%s 注册成功!', [GameName]);
MmoLog.Lines.Add(Format('%s%s 注册成功!来自IP: %s', [GetNowTime, GameName, AContext.Connection.Socket.Binding.PeerIP]));
Buff := RawToBytes(ReturnMsg, SizeOf(ReturnMsg));
AContext.Connection.IOHandler.Write(Buff);
Exit;
end else begin
ReturnMsg.Tag := SM_AddNewId_Fail;
ReturnMsg.str := '未知错误!';
Buff := RawToBytes(ReturnMsg, SizeOf(ReturnMsg));
AContext.Connection.IOHandler.Write(Buff);
Exit;
end;
end;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
var
HndForm: THandle;
begin
HndForm := FindWindow(nil, PChar('CH_Ts2_RELAY MALA NET'));
if HndForm = 0 then begin
ShowMessage('Relay.exe未启动,程序将退出。');
Application.Terminate;
end else begin
//先获取句柄
Enumwindows(@EnumWindowsProc,0);
if (UserNameHandle = 0) or (UserPassHandle = 0) or (ResultMemoHandle = 0) then begin
ShowMessage('Handle获取失败,程序将退出。');
Application.Terminate;
end;
end;

try
//listen
IdTCPServer1.Bindings.Add.IP := '0.0.0.0';
IdTCPServer1.Bindings.Add.Port := 8897;
IdTCPServer1.Active := True;
mmoLog.Lines.Add(Format('%s封包注册服务已启动。', [GetNowTime]));
mmoLog.Lines.Add(Format('%s正在监听 %d 端口', [GetNowTime, 8897]));
except
MmoLog.Lines.Add(Format('%s无法打开 %d 端口,请检查端口是否正在使用中。', [GetNowTime, 8897]));
Exit;
end;
end;

end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐