高手请进:如何将dll中全局变量传递给应用程序?
写了个钩子,需要将hook到的字符串传递给我写的应用程序中,请问该怎么做?
似乎内存映射能解决,但是具体不知道怎么做,请各位高手帮忙。有例程最好。谢谢了。
[解决办法]
首先获得你的程序的实例句柄,然后可以发消息,内存映射,管道等等各种方法
[解决办法]
给你个例子
//获得目录
StrRoot := GetCurrentDir();
if (StrRoot[Length(StrRoot)] <> '\ ')then StrRoot := StrRoot+ '\ ';
StrRoot := StrRoot+ 'HicMemory.tmp ';
//创建文件
FileHandle := CreateFile(PChar(StrRoot),GENERIC_READ or GENERIC_WRITE,FILE_SHARE_WRITE,nil,CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY,handle);
//创建内存空间
FHand := CreateFileMapping(FileHandle,nil,PAGE_READWRITE,0,64*1024,PChar( 'TESTMEMORY '));
//映射
p := MapViewOfFile(FHand,FILE_MAP_WRITE or FILE_MAP_READ,0,0,6400);
//获取内容
QueryFirst.Close ;
QueryFirst.SQL.Clear ;
QueryFirst.SQL.Add( 'select * from di_state order by hicno ');
try
QueryFirst.Open;
except
end;
iout :=0;
Str := ' ';
QueryFirst.First ;
while (not QueryFirst.Eof) do
begin
if iout <> QueryFirst.FieldByName( 'HICNO ').AsInteger then
begin
iData := 0 ;
iout:= QueryFirst.FieldByName( 'HICNO ').AsInteger ;
Str := Str+ '! '+Format( '%.2d ',[iout])+ '00 ';
QuerySec.Close ;
QuerySec.SQL.Clear ;
QuerySec.SQL.Add( 'select * from di_state where hicno= '+IntToStr(iout)+ ' and state=1 order by dino ');
QuerySec.Open;
QuerySec.First ;
while (not QuerySec.Eof) do
begin
iData := iData + Trunc(power(10,(QuerySec.FieldByName( 'dino ').AsInteger -1)));
QuerySec.Next ;
end;
StrTemp := Format( '%.8d ',[idata]);
Str := Str+ StrToBit(StrTemp) + #13#10;
end;
QueryFirst.Next ;
end;
StrPCopy(CharTemp,Str);
//写映射
ApointerMemory(p)^ := CharTemp;
//以下为测试用代码
{ ShowMessage( 'Write: '+#10 + ApointerMemory(p)^);
readHandle := OpenFileMapping(FILE_MAP_READ,false,PChar( 'HICMEMORY '));
readP := MapViewOfFile(readHandle,FILE_MAP_READ,0,0,6400);
ShowMessage( 'read : '+#10 + ApointerMemory(readP)^);
}
[解决办法]
还有这个
http://www.52delphi.com/list.asp?ID=633
[解决办法]
如果变量是Integer这类的发消息就可以了,如果是文本或者结构体,就用共享内存,发消息通知更改。
[解决办法]
- Delphi(Pascal) code
DLL 文件:-------------------------------------------- library MyHook;uses SysUtils, Windows, Messages, Classes;{$R *.res}const WM_MyMessage = WM_USER + 1; {自定义消息}var hook: HHOOK; info: string; h: HWND; {用作外部窗口的句柄}{获取外部窗口的句柄}function SetHWnd(hwnd: HWND): Boolean; stdcall;begin h := hwnd; Result := True;end;function MouseHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;begin case wParam of WM_MOUSEMOVE : info := '鼠标位置'; WM_LBUTTONDOWN : info := '按下'; WM_LBUTTONUp : info := '放开'; end; info := Format('%s: %d,%d', [info, PMouseHookStruct(lParam)^.pt.X, PMouseHookStruct(lParam)^.pt.Y]); {通过消息把数据传递给指定窗口} PostMessage(h, WM_MyMessage, 0, Integer(PChar(info))); Result := CallNextHookEx(hook, nCode, wParam, lParam);end;function SetHook: Boolean; stdcall;const WH_MOUSE_LL =14;begin hook := SetWindowsHookEx(WH_MOUSE_LL, @MouseHook, HInstance, 0); Result := hook <> 0;end;function DelHook: Boolean; stdcall;begin Result := UnhookWindowsHookEx(hook);end;exports SetHook, DelHook, MouseHook, SetHWnd;beginend.测试代码:-------------------------------------------- unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;const WM_MyMessage = WM_USER + 1;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure MyMessage(var msg: TMessage); message WM_MyMessage; {定义一个消息方法接受消息} end; function SetHook: Boolean; stdcall; function DelHook: Boolean; stdcall; function SetHWnd(hwnd: HWND): Boolean; stdcall;var Form1: TForm1;implementation{$R *.dfm}function SetHook; external 'MyHook.dll';function DelHook; external 'MyHook.dll';function SetHWnd; external 'MyHook.dll';procedure TForm1.Button1Click(Sender: TObject);begin SetHook; SetHWnd(Handle);end;procedure TForm1.Button2Click(Sender: TObject);begin DelHook;end;procedure TForm1.FormCreate(Sender: TObject);begin Button1.Caption := '安装钩子'; Button2.Caption := '载卸钩子'; FormStyle := fsStayOnTop; {为了测试, 让窗口一直在前面}end;procedure TForm1.FormDestroy(Sender: TObject);begin DelHook;end;{把接受到的内容显示在窗体}procedure TForm1.MyMessage(var msg: TMessage);begin Text := PChar(msg.LParam);end;end.--------------------------------------------测试窗体:-------------------------------------------- object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 78 ClientWidth = 271 Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'Tahoma' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate OnDestroy = FormDestroy PixelsPerInch = 96 TextHeight = 13 object Button1: TButton Left = 48 Top = 32 Width = 75 Height = 25 Caption = 'Button1' TabOrder = 0 OnClick = Button1Click end object Button2: TButton Left = 144 Top = 32 Width = 75 Height = 25 Caption = 'Button2' TabOrder = 1 OnClick = Button2Click endend