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

 

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..254of 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..254of 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..254of 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, 
00);
    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 = 0or (UserPassHandle = 0or (ResultMemoHandle = 0then 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.

 

posted on 2009-06-28 11:05  jxgxy  阅读(778)  评论(0编辑  收藏  举报

导航