sendunit.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:2k
- unit sendunit;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, StdCtrls, Buttons;
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Label1: TLabel;
- BitBtn1: TBitBtn;
- Label2: TLabel;
- Edit2: TEdit;
- BitBtn2: TBitBtn;
- procedure BitBtn1Click(Sender: TObject);
- procedure BitBtn2Click(Sender: TObject);
- private
- { Private declarations }
- public
- procedure SendData;
- { Public declarations }
- end;
- Const
- C1= 17856;
- C2= 23589;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- function EncryptModule(SourceStr:String;Key:Word;N:Integer):String;
- var
- I:Integer;
- begin
- SetLength(Result,Length(SourceStr));//利用SetLength函数指定密文长度
- //对每一个索引元素进行变换
- for I:=1 to Length(SourceStr) do
- begin
- Result[I]:=Char(byte(SourceStr[I]) xor (Key Shr N));
- Key:= (byte(Result[I]) + Key)*C1+C2;
- end;
- end;
- procedure TForm1.SendData; //发送消息和数据过程
- var
- Copydatastruct: TCopyDataStruct; //定义一个Tcopydatastruct类型的变量
- hwnd: THandle; //句柄类型,用于存储目标对象的句柄
- begin
- Copydatastruct.cbData := Length (Edit1.Text) + 1;
- //为传递的数据区分配内存并把要传送的字符串放入数据区
- GetMem (Copydatastruct.lpData, Copydatastruct.cbData );
- StrCopy (Copydatastruct.lpData, PChar (Edit1.Text));
- // 获得接收窗口的句柄
- Hwnd := FindWindow (nil, '利用WM_COPYDATA交换数据Receive改进版');
- // 如果目标对象存在,则向其发送WM_COPYDATA消息
- if Hwnd <> 0 then
- SendMessage (Hwnd, WM_COPYDATA, Handle, Cardinal(@Copydatastruct))
- else
- ShowMessage ('目标对象窗口没找到!');
- //释放资源
- FreeMem (Copydatastruct.lpData);
- end;
- procedure TForm1.BitBtn1Click(Sender: TObject);
- begin
- if Edit1.Text<>'' then
- SendData;
- end;
- procedure TForm1.BitBtn2Click(Sender: TObject);
- begin
- Edit1.Text:=EncryptModule(Edit2.Text,12345,10);
- end;
- end.