首页上一页 1 下一页尾页 3 条记录 1/1页
请教一个delphi转换成c#代码的方法
发表在C#图书答疑
2010-08-12
是否精华
是
否
版块置顶:
是
否
unit UWooolPublic;
interface
uses
ScktComp,iniFiles,SysUtils,Windows,DecodeEncode,Controls,Sockets,jpeg,Classes,ExtCtrls,Forms;
const
GameIniName = 'Game.ini';
ConfigIniname = 'Config.ini';
type
TServerInfo = packed record
ServerIP : string;
ServerPort : String;
GroupNum : Integer;
Group : String;
GroupNick : String;
end;
TUserInfo = packed record
UserID : array[0..10] of char;
PassWord : array[0..10] of char;
UserName : array[0..20] of char;
ManID : array[0..14] of char;
Email : array[0..14] of char;
Question1 : array[0..20] of char;
Answer1 : array[0..12] of char;
Linkname : array[0..39] of char;
end;
TUserInfo1= packed record
Question2 : array[0..20] of char;
Answer2 : array[0..12] of char;
ChrisDay : array[0..10] of char;
MobilePhone : array[0..13] of char;
Memo1 : array[0..52] of char;
Memo2 : array[0..52] of char;
end;
TEditPasswordInfo = record
GameID : array[0..10] of char;
OldPassword : array[0..10] of char;
NewPasswod : array[0..10] of char;
end;
TGetPasswordinfo = record
GameID : array[0..9] of char;
ChrisDay : array[0..9] of char;
Question1 : array[0..9] of char;
Answer1 : array[0..9] of char;
Question2 : array[0..9] of char;
Answer2 : array[0..9] of char;
end;
TWoool = class
private
FFullScreen : Boolean; procedure SetFullScreen (const Value: Boolean);
protected
public
Flag : Char;
property FullScreen : Boolean read FFullScreen write SetFullScreen;
{判断服务器是否在线}
function CheckServerOnLine:Boolean;
{登陆游戏}
procedure LogGame;
{注册号码}
function RegeditWooolID:Boolean;
{修改密码}
function EditPassWord:Boolean;
{取回密码}
function GetPassWord:Boolean;
{发送消息}
procedure SendMsg(msg: TMsg;datasize:integer);
{处理消息}
procedure OnMsg(msg: TMsg;datasize:integer);
end;
var
ServerInfo : TServerInfo ;
ServerInfoList : array of TServerInfo ;
ClientSocket : TTcpClient;
UserInfo : TUserInfo;
UserInfo1 : TUserInfo1;
Woool : TWoool;
EditPasswordInfo : TEditPasswordInfo;
GetPasswordinfo : TGetPasswordinfo;
JpgImage : TJpegImage ;
JpgStream : TResourceStream;
procedure LoadImageResource(Sender: TObject;ResourceName:String);
procedure ClearMemory;
implementation
{ TWoool }
procedure LoadImageResource(Sender: TObject;ResourceName:String);
begin
JpgStream := TResourceStream.Create (Hinstance,ResourceName,RT_RCDATA) ;
try
Jpgimage.LoadFromStream (JpgStream) ;
(Sender as TImage).Picture.Bitmap.Assign (Jpgimage);
finally
FreeAndNil(JpgStream);
end;
end;
function TWoool.CheckServerOnLine: Boolean;
begin
Result:=False;
try
ClientSocket.Close;
ClientSocket.RemoteHost := ServerInfo.ServerIP;
ClientSocket.RemotePort := ServerInfo.ServerPort;
if ClientSocket.Connect then
Result:=True;
except
end;
end;
procedure TWoool.LogGame;
var
GameIniFile:TIniFile;
ConfigIniFile:TIniFile;
begin
GameIniFile:=TIniFile.Create(GetCurrentDIr+'\'+'data\Game.Ini');
ConfigIniFile:=TIniFile.Create(GetCurrentDIr+'\'+'data\Config.Ini');
try
GameIniFile.WriteString('Config','ServerIP',ServerInfo.ServerIP);
GameIniFile.WriteString('Config','ServerPort',ServerInfo.ServerPort);
GameIniFile.WriteInteger('Config','GroupNum',ServerInfo.GroupNum);
GameIniFile.WriteString('Config','Group0',ServerInfo.Group);
GameIniFile.WriteString('Config','GroupNick0',ServerInfo.GroupNick);
if FullScreen then
ConfigIniFile.WriteString('Config','FullScreen','Yes')
else ConfigIniFile.WriteString('Config','FullScreen','No');
WinExec( 'Data/woool.dat', SW_SHOW );
Sleep (2000);
Application.Terminate;
finally
FreeAndNil(GameIniFile);
FreeAndNil(ConfigIniFile);
end;
end;
function TWoool.RegeditWooolID: Boolean;
var
codedbuffer:array[0..4095] of char;
codedbuffer1:array[0..4095] of char;
codedsize :integer;
codedsize1 :integer;
msg:TMsg;
msg1:TMsg;
begin
if ClientSocket.Active then
begin
ZeroMemory(@msg,sizeof(TMsg));
ZeroMemory(@msg1,sizeof(TMsg));
copyMemory(@msg.data,@UserInfo,sizeof(UserInfo));
copyMemory(@msg1.data,@UserInfo1,sizeof(UserInfo1));
msg.cmd := 2002;
codedsize := Encode( @msg, sizeof(tmsgheader)+sizeof(UserInfo), @codedbuffer[2] );
codedsize1 := Encode( @msg1.data, sizeof(UserInfo1), @codedbuffer1[2]);
codedbuffer[0] := '#';
codedbuffer[1] := flag;
codedbuffer1[0] := ' ';
codedbuffer1[1] := ' ';
copymemory(@codedbuffer[codedsize+2],@codedbuffer1,codedsize1+2);
codedbuffer[codedsize+codedsize1+4] := '!';
ClientSocket.SendBuf(codedbuffer, codedsize+codedsize1+5 );
Inc(Flag);
if flag > '9' then flag := '1';
end else MessageBox(0, '连接服务器失败!', nil, 0 );
result:=True;
end;
function TWoool.EditPassWord: Boolean;
var
msg:TMsg;
codedbuffer:array[0..4095] of char;
bufCount:Integer;
codedsize :integer;
begin
if ClientSocket.Active then
begin
ZeroMemory(@msg,sizeof(TMsg));
fillchar(codedbuffer,sizeof(codedbuffer),#0);
strlcopy( msg.data
, EditPasswordInfo.GameID
, Length(EditPasswordInfo.GameID) );
msg.data[Length(Trim(EditPasswordInfo.GameID))] := #9 ;
strlcopy( msg.data+Length(Trim(Trim(EditPasswordInfo.GameID)))+1
, EditPasswordInfo.OldPassword
,Length(Trim(EditPasswordInfo.OldPassword)) );
msg.data[Length(Trim(EditPasswordInfo.GameID))
+Length(Trim(EditPasswordInfo.OldPassword))+1]
:= #9;
strlcopy(
msg.data+Length(Trim(EditPasswordInfo.GameID))
+Length(Trim(EditPasswordInfo.OldPassword))
+2
, EditPasswordInfo.NewPasswod
,Length(Trim(EditPasswordInfo.NewPasswod)) );
msg.data[Length(trim(EditPasswordInfo.GameID))
+Length(trim(EditPasswordInfo.OldPassword))+2+Length(Trim(EditPasswordInfo.NewPasswod))]
:= #0;
bufCount:=
Length(trim(EditPasswordInfo.GameID))
+1
+Length(trim(EditPasswordInfo.OldPassword))
+1
+Length(Trim(EditPasswordInfo.NewPasswod));
msg.cmd := 2003;
codedsize := Encode( @msg
, sizeof(tmsgheader)+bufCount
, @codedbuffer[2] );
codedbuffer[0] := '#';
codedbuffer[1] := flag;
codedbuffer[codedsize+2] := '!';
ClientSocket.SendBuf(codedbuffer, codedsize+3 );
Inc(Flag);
if flag > '9' then flag := '1';
end else MessageBox(0, '连接服务器失败!', nil, 0 );
Result:=True;
end;
function TWoool.GetPassWord: boolean;
var
codedbuffer:array[0..4095] of char;
codedsize :integer;
msg:TMsg;
LengthStr:Integer;
begin
if ClientSocket.Active then
begin
ZeroMemory(@msg,sizeof(TMsg));
fillchar(codedbuffer,sizeof(codedbuffer),#0);
LengthStr:=0;
strlcopy( msg.data
, GetPasswordinfo.GameID
, Length(GetPasswordinfo.GameID) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.GameID));
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.Question1
,Length(GetPasswordinfo.Question1) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Question1))+1;
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.Answer1
,Length(GetPasswordinfo.Answer1) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Answer1))+1;
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.Question2
,Length(GetPasswordinfo.Question2) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Question2))+1;
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.Answer2
,Length(GetPasswordinfo.Answer2) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Answer2))+1;
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.ChrisDay
,Length(GetPasswordinfo.ChrisDay) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.ChrisDay))+1;
msg.cmd := 3042;
codedsize := Encode( @msg, sizeof(tmsgheader)+LengthStr, @codedbuffer[2] );
codedbuffer[0] := '#';
codedbuffer[1] := flag;
codedbuffer[codedsize+2] := '!';
ClientSocket.SendBuf(codedbuffer, codedsize+3 );
Inc(Flag);
if flag > '9' then flag := '1';
end else MessageBox(0, '连接服务器失败!', nil, 0 );
Result:=True;
end;
procedure TWoool.SendMsg(msg: TMsg; datasize: integer);
var
codedbuffer:array[0..4095] of char;
codedsize :integer;
begin
if ClientSocket.Active then
begin
codedsize := Encode( @msg, sizeof(tmsgheader)+datasize, @codedbuffer[2] );
codedbuffer[0] := '#';
codedbuffer[1] := flag;
codedbuffer[codedsize+2] := '!';
ClientSocket.SendBuf(codedbuffer, codedsize+3 );
Inc(Flag);
if flag > '9' then flag := '1';
end else MessageBox(0, '连接服务器失败!', nil, 0 );
end;
procedure TWoool.OnMsg(msg: TMsg; datasize: integer);
begin
GetTickCount();
case msg.cmd of
$1fc:
begin
if msg.flag = 1 then
MessageBox(0, '该帐号已经有人用过了,请选择其他帐号!', '注意!', 0 )
else
MessageBox(0, '该帐号可以使用!', '注意!', 0 );
end;
$1f8:
begin
MessageBox( 0, '帐号注册成功!', '注意!', 0 );
end;
$1f9:
begin
MessageBox( 0, '帐号注册失败!', '注意!', 0 );
end;
$1fa:
begin
MessageBox( 0, '密码修改成功!', '注意!', 0 );
end;
$1fb:
begin
MessageBox( 0, '原始密码有错误!', '注意!', 0 );
end;
3004:
begin
MessageBox( 0, PChar('取回密码成功'+'['+msg.data+']'+'!'), '注意!', 0 );
end;
3005:
begin
MessageBox( 0, '取回密码失败!', '注意!', 0 );
end;
else
MessageBox( 0, '未知消息~~', nil, 0 );
end;
end;
procedure ClearMemory;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
end;
end;
procedure TWoool.SetFullScreen(const Value: Boolean);
begin
FFullScreen := Value;
end;
end.
interface
uses
ScktComp,iniFiles,SysUtils,Windows,DecodeEncode,Controls,Sockets,jpeg,Classes,ExtCtrls,Forms;
const
GameIniName = 'Game.ini';
ConfigIniname = 'Config.ini';
type
TServerInfo = packed record
ServerIP : string;
ServerPort : String;
GroupNum : Integer;
Group : String;
GroupNick : String;
end;
TUserInfo = packed record
UserID : array[0..10] of char;
PassWord : array[0..10] of char;
UserName : array[0..20] of char;
ManID : array[0..14] of char;
Email : array[0..14] of char;
Question1 : array[0..20] of char;
Answer1 : array[0..12] of char;
Linkname : array[0..39] of char;
end;
TUserInfo1= packed record
Question2 : array[0..20] of char;
Answer2 : array[0..12] of char;
ChrisDay : array[0..10] of char;
MobilePhone : array[0..13] of char;
Memo1 : array[0..52] of char;
Memo2 : array[0..52] of char;
end;
TEditPasswordInfo = record
GameID : array[0..10] of char;
OldPassword : array[0..10] of char;
NewPasswod : array[0..10] of char;
end;
TGetPasswordinfo = record
GameID : array[0..9] of char;
ChrisDay : array[0..9] of char;
Question1 : array[0..9] of char;
Answer1 : array[0..9] of char;
Question2 : array[0..9] of char;
Answer2 : array[0..9] of char;
end;
TWoool = class
private
FFullScreen : Boolean; procedure SetFullScreen (const Value: Boolean);
protected
public
Flag : Char;
property FullScreen : Boolean read FFullScreen write SetFullScreen;
{判断服务器是否在线}
function CheckServerOnLine:Boolean;
{登陆游戏}
procedure LogGame;
{注册号码}
function RegeditWooolID:Boolean;
{修改密码}
function EditPassWord:Boolean;
{取回密码}
function GetPassWord:Boolean;
{发送消息}
procedure SendMsg(msg: TMsg;datasize:integer);
{处理消息}
procedure OnMsg(msg: TMsg;datasize:integer);
end;
var
ServerInfo : TServerInfo ;
ServerInfoList : array of TServerInfo ;
ClientSocket : TTcpClient;
UserInfo : TUserInfo;
UserInfo1 : TUserInfo1;
Woool : TWoool;
EditPasswordInfo : TEditPasswordInfo;
GetPasswordinfo : TGetPasswordinfo;
JpgImage : TJpegImage ;
JpgStream : TResourceStream;
procedure LoadImageResource(Sender: TObject;ResourceName:String);
procedure ClearMemory;
implementation
{ TWoool }
procedure LoadImageResource(Sender: TObject;ResourceName:String);
begin
JpgStream := TResourceStream.Create (Hinstance,ResourceName,RT_RCDATA) ;
try
Jpgimage.LoadFromStream (JpgStream) ;
(Sender as TImage).Picture.Bitmap.Assign (Jpgimage);
finally
FreeAndNil(JpgStream);
end;
end;
function TWoool.CheckServerOnLine: Boolean;
begin
Result:=False;
try
ClientSocket.Close;
ClientSocket.RemoteHost := ServerInfo.ServerIP;
ClientSocket.RemotePort := ServerInfo.ServerPort;
if ClientSocket.Connect then
Result:=True;
except
end;
end;
procedure TWoool.LogGame;
var
GameIniFile:TIniFile;
ConfigIniFile:TIniFile;
begin
GameIniFile:=TIniFile.Create(GetCurrentDIr+'\'+'data\Game.Ini');
ConfigIniFile:=TIniFile.Create(GetCurrentDIr+'\'+'data\Config.Ini');
try
GameIniFile.WriteString('Config','ServerIP',ServerInfo.ServerIP);
GameIniFile.WriteString('Config','ServerPort',ServerInfo.ServerPort);
GameIniFile.WriteInteger('Config','GroupNum',ServerInfo.GroupNum);
GameIniFile.WriteString('Config','Group0',ServerInfo.Group);
GameIniFile.WriteString('Config','GroupNick0',ServerInfo.GroupNick);
if FullScreen then
ConfigIniFile.WriteString('Config','FullScreen','Yes')
else ConfigIniFile.WriteString('Config','FullScreen','No');
WinExec( 'Data/woool.dat', SW_SHOW );
Sleep (2000);
Application.Terminate;
finally
FreeAndNil(GameIniFile);
FreeAndNil(ConfigIniFile);
end;
end;
function TWoool.RegeditWooolID: Boolean;
var
codedbuffer:array[0..4095] of char;
codedbuffer1:array[0..4095] of char;
codedsize :integer;
codedsize1 :integer;
msg:TMsg;
msg1:TMsg;
begin
if ClientSocket.Active then
begin
ZeroMemory(@msg,sizeof(TMsg));
ZeroMemory(@msg1,sizeof(TMsg));
copyMemory(@msg.data,@UserInfo,sizeof(UserInfo));
copyMemory(@msg1.data,@UserInfo1,sizeof(UserInfo1));
msg.cmd := 2002;
codedsize := Encode( @msg, sizeof(tmsgheader)+sizeof(UserInfo), @codedbuffer[2] );
codedsize1 := Encode( @msg1.data, sizeof(UserInfo1), @codedbuffer1[2]);
codedbuffer[0] := '#';
codedbuffer[1] := flag;
codedbuffer1[0] := ' ';
codedbuffer1[1] := ' ';
copymemory(@codedbuffer[codedsize+2],@codedbuffer1,codedsize1+2);
codedbuffer[codedsize+codedsize1+4] := '!';
ClientSocket.SendBuf(codedbuffer, codedsize+codedsize1+5 );
Inc(Flag);
if flag > '9' then flag := '1';
end else MessageBox(0, '连接服务器失败!', nil, 0 );
result:=True;
end;
function TWoool.EditPassWord: Boolean;
var
msg:TMsg;
codedbuffer:array[0..4095] of char;
bufCount:Integer;
codedsize :integer;
begin
if ClientSocket.Active then
begin
ZeroMemory(@msg,sizeof(TMsg));
fillchar(codedbuffer,sizeof(codedbuffer),#0);
strlcopy( msg.data
, EditPasswordInfo.GameID
, Length(EditPasswordInfo.GameID) );
msg.data[Length(Trim(EditPasswordInfo.GameID))] := #9 ;
strlcopy( msg.data+Length(Trim(Trim(EditPasswordInfo.GameID)))+1
, EditPasswordInfo.OldPassword
,Length(Trim(EditPasswordInfo.OldPassword)) );
msg.data[Length(Trim(EditPasswordInfo.GameID))
+Length(Trim(EditPasswordInfo.OldPassword))+1]
:= #9;
strlcopy(
msg.data+Length(Trim(EditPasswordInfo.GameID))
+Length(Trim(EditPasswordInfo.OldPassword))
+2
, EditPasswordInfo.NewPasswod
,Length(Trim(EditPasswordInfo.NewPasswod)) );
msg.data[Length(trim(EditPasswordInfo.GameID))
+Length(trim(EditPasswordInfo.OldPassword))+2+Length(Trim(EditPasswordInfo.NewPasswod))]
:= #0;
bufCount:=
Length(trim(EditPasswordInfo.GameID))
+1
+Length(trim(EditPasswordInfo.OldPassword))
+1
+Length(Trim(EditPasswordInfo.NewPasswod));
msg.cmd := 2003;
codedsize := Encode( @msg
, sizeof(tmsgheader)+bufCount
, @codedbuffer[2] );
codedbuffer[0] := '#';
codedbuffer[1] := flag;
codedbuffer[codedsize+2] := '!';
ClientSocket.SendBuf(codedbuffer, codedsize+3 );
Inc(Flag);
if flag > '9' then flag := '1';
end else MessageBox(0, '连接服务器失败!', nil, 0 );
Result:=True;
end;
function TWoool.GetPassWord: boolean;
var
codedbuffer:array[0..4095] of char;
codedsize :integer;
msg:TMsg;
LengthStr:Integer;
begin
if ClientSocket.Active then
begin
ZeroMemory(@msg,sizeof(TMsg));
fillchar(codedbuffer,sizeof(codedbuffer),#0);
LengthStr:=0;
strlcopy( msg.data
, GetPasswordinfo.GameID
, Length(GetPasswordinfo.GameID) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.GameID));
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.Question1
,Length(GetPasswordinfo.Question1) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Question1))+1;
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.Answer1
,Length(GetPasswordinfo.Answer1) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Answer1))+1;
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.Question2
,Length(GetPasswordinfo.Question2) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Question2))+1;
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.Answer2
,Length(GetPasswordinfo.Answer2) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.Answer2))+1;
msg.data[LengthStr] := #9;
strlcopy(
msg.data+LengthStr+1
, GetPasswordinfo.ChrisDay
,Length(GetPasswordinfo.ChrisDay) );
LengthStr:=LengthStr+Length(Trim(GetPasswordinfo.ChrisDay))+1;
msg.cmd := 3042;
codedsize := Encode( @msg, sizeof(tmsgheader)+LengthStr, @codedbuffer[2] );
codedbuffer[0] := '#';
codedbuffer[1] := flag;
codedbuffer[codedsize+2] := '!';
ClientSocket.SendBuf(codedbuffer, codedsize+3 );
Inc(Flag);
if flag > '9' then flag := '1';
end else MessageBox(0, '连接服务器失败!', nil, 0 );
Result:=True;
end;
procedure TWoool.SendMsg(msg: TMsg; datasize: integer);
var
codedbuffer:array[0..4095] of char;
codedsize :integer;
begin
if ClientSocket.Active then
begin
codedsize := Encode( @msg, sizeof(tmsgheader)+datasize, @codedbuffer[2] );
codedbuffer[0] := '#';
codedbuffer[1] := flag;
codedbuffer[codedsize+2] := '!';
ClientSocket.SendBuf(codedbuffer, codedsize+3 );
Inc(Flag);
if flag > '9' then flag := '1';
end else MessageBox(0, '连接服务器失败!', nil, 0 );
end;
procedure TWoool.OnMsg(msg: TMsg; datasize: integer);
begin
GetTickCount();
case msg.cmd of
$1fc:
begin
if msg.flag = 1 then
MessageBox(0, '该帐号已经有人用过了,请选择其他帐号!', '注意!', 0 )
else
MessageBox(0, '该帐号可以使用!', '注意!', 0 );
end;
$1f8:
begin
MessageBox( 0, '帐号注册成功!', '注意!', 0 );
end;
$1f9:
begin
MessageBox( 0, '帐号注册失败!', '注意!', 0 );
end;
$1fa:
begin
MessageBox( 0, '密码修改成功!', '注意!', 0 );
end;
$1fb:
begin
MessageBox( 0, '原始密码有错误!', '注意!', 0 );
end;
3004:
begin
MessageBox( 0, PChar('取回密码成功'+'['+msg.data+']'+'!'), '注意!', 0 );
end;
3005:
begin
MessageBox( 0, '取回密码失败!', '注意!', 0 );
end;
else
MessageBox( 0, '未知消息~~', nil, 0 );
end;
end;
procedure ClearMemory;
begin
if Win32Platform = VER_PLATFORM_WIN32_NT then
begin
SetProcessWorkingSetSize(GetCurrentProcess, $FFFFFFFF, $FFFFFFFF);
end;
end;
procedure TWoool.SetFullScreen(const Value: Boolean);
begin
FFullScreen := Value;
end;
end.