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.
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.
相关文章推荐
- [VB.NET]VB.net下重命名文件的操作问题,为什么在我重命名文件之后,原始文件仍然存在(在有I/O异常时)
- 简单的iOS通讯录操作封装(添加联系人、检查号码是否存在)
- 解决修改properties 属性文件存在缓存问题,附带操作properties文件工具类
- (数据库篇) SQL查询~ 存在一个表而不在另一个表中的数据
- Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
- 判断某键值是否存在[注册表操作]
- Extjs 项目中常用的小技巧,也许你用得着(5)--设置 Ext.data.Store 传参的请求方式
- python 安装 win 下的exe结尾的文件操作
- JS操作select相关方法:新增 修改 删除 选中 清空 判断存在 等
- 如何操作我的程序的另一个实例
- js操作select控件方法(新增、修改、删除、选中、清空、判断存在等)
- MFC库文件版本对程序的影响:Unhandled exception in ***.exe(MFCO42D.dll)0xC00000005:Access Violation,但多数情况下还是对内存操作不当造成的
- MFC操作SQlite,打开数据库路径存在中文,解决方案
- Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
- js操作select控件大全(包含新增、修改、删除、选中、清空、判断存在等)
- Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等) (转)
- C#对FTP的操作(上传,下载,重命名文件,删除文件,文件存在检查)
- Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)[转]
- 在c#中调用另一个应用程序或命令行(.exe 带参数) 【转】
- Win32控制台应用程序如何调用另一个.exe