小弟写了一个dll 源码:
library Hook;
uses
SysUtils,//我要是不用ShareMem,在调用loadlibrary的时候弹出个对话框 提示raised too many consecutive exceptions 如果用了将会出现另外一种错误 请往下看
Classes,
Windows,
Messages,
ApiHook in 'ApiHook.pas';
type
Tdata=record
hook:THandle;
hooked:Boolean
end;
Pdata=^Tdata;
{$R *.res}
var
DllData:Pdata;
dizhi:Cardinal;
procedure HookPro(Ncode,Lparam,Wparam:LongWord);stdcall;
begin
if not DllData^.hooked then
begin
HookApi(dizhi);
DllData^.hooked:=True;
end;
CallNextHookEx(dlldata^.hook,Ncode,Lparam,Wparam);
end;
function HookInstall(sWindow:LongWord;ss:DWORD):Boolean;stdcall;
var
threadId:LongWord;
begin
Result:=False;
DllData^.hook:=0;
threadId:=GetWindowThreadProcessId(sWindow,nil);
DllData^.hook:=SetWindowsHookEx(WH_GETMESSAGE,@HookPro,HInstance,threadId);
if DllData^.hook >0 then
begin
Result:=True;
end
else
begin
Exit;
end;
end;
procedure UnHook;
begin
UnHookApi;
if DllData^.hooked then
begin
UnhookWindowsHookEx(dlldata^.hook);
end;
end;
procedure MyDllHandle(Reason:integer);
var
fhandle:THandle;
begin
case Reason of
DLL_PROCESS_ATTACH:
begin
fhandle:=CreateFileMapping($ffffffff,nil,PAGE_READWRITE,0,$ffff,'Mydll');
if fhandle=0 then
begin
if GetLastError=ERROR_ALREADY_EXISTS then
begin
fhandle:=OpenFileMapping(FILE_MAP_ALL_ACCESS,False,'Mydll');
if fhandle=0 then
begin
Exit;
end;
end
else
begin
Exit;
end;
DllData:=MapViewOfFile(fhandle,FILE_MAP_ALL_ACCESS,0,0,0);
if DllData=nil then
begin
CloseHandle(fhandle);
end;
end;
end;
DLL_PROCESS_DETACH:
begin
if Assigned(DllData) then
begin
UnmapViewOfFile(DllData);
DllData:=nil;
end;
end;
end;
end;
exports
HookInstall, UnHook, HookPro;
begin
DllProc:=@MyDllHandle;
MyDllHandle(DLL_PROCESS_ATTACH);
DllData^.hooked:=False;
end.
如果在dll里加入了sharemen 我在程序里调用的代码:
这个是uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
如果在uses里加入sharemen 程序编译不过
str:=GetCurrentDir()+'\Hook.dll';
if not FileExists(str) then
begin
ShowMessage('没有这个dll');
Exit;
end;
modlueHandle:=LoadLibrary(PChar(str));//它的modluehandle总是0
ShowMessage(IntToStr(GetLastError));//他的返回错误值是126
@InstallHook:=GetProcAddress(modlueHandle,'HookInstall');
请问 我这个应该怎么调用这个dll呀? 谢谢!
library Hook;
uses
SysUtils,//我要是不用ShareMem,在调用loadlibrary的时候弹出个对话框 提示raised too many consecutive exceptions 如果用了将会出现另外一种错误 请往下看
Classes,
Windows,
Messages,
ApiHook in 'ApiHook.pas';
type
Tdata=record
hook:THandle;
hooked:Boolean
end;
Pdata=^Tdata;
{$R *.res}
var
DllData:Pdata;
dizhi:Cardinal;
procedure HookPro(Ncode,Lparam,Wparam:LongWord);stdcall;
begin
if not DllData^.hooked then
begin
HookApi(dizhi);
DllData^.hooked:=True;
end;
CallNextHookEx(dlldata^.hook,Ncode,Lparam,Wparam);
end;
function HookInstall(sWindow:LongWord;ss:DWORD):Boolean;stdcall;
var
threadId:LongWord;
begin
Result:=False;
DllData^.hook:=0;
threadId:=GetWindowThreadProcessId(sWindow,nil);
DllData^.hook:=SetWindowsHookEx(WH_GETMESSAGE,@HookPro,HInstance,threadId);
if DllData^.hook >0 then
begin
Result:=True;
end
else
begin
Exit;
end;
end;
procedure UnHook;
begin
UnHookApi;
if DllData^.hooked then
begin
UnhookWindowsHookEx(dlldata^.hook);
end;
end;
procedure MyDllHandle(Reason:integer);
var
fhandle:THandle;
begin
case Reason of
DLL_PROCESS_ATTACH:
begin
fhandle:=CreateFileMapping($ffffffff,nil,PAGE_READWRITE,0,$ffff,'Mydll');
if fhandle=0 then
begin
if GetLastError=ERROR_ALREADY_EXISTS then
begin
fhandle:=OpenFileMapping(FILE_MAP_ALL_ACCESS,False,'Mydll');
if fhandle=0 then
begin
Exit;
end;
end
else
begin
Exit;
end;
DllData:=MapViewOfFile(fhandle,FILE_MAP_ALL_ACCESS,0,0,0);
if DllData=nil then
begin
CloseHandle(fhandle);
end;
end;
end;
DLL_PROCESS_DETACH:
begin
if Assigned(DllData) then
begin
UnmapViewOfFile(DllData);
DllData:=nil;
end;
end;
end;
end;
exports
HookInstall, UnHook, HookPro;
begin
DllProc:=@MyDllHandle;
MyDllHandle(DLL_PROCESS_ATTACH);
DllData^.hooked:=False;
end.
如果在dll里加入了sharemen 我在程序里调用的代码:
这个是uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
如果在uses里加入sharemen 程序编译不过
str:=GetCurrentDir()+'\Hook.dll';
if not FileExists(str) then
begin
ShowMessage('没有这个dll');
Exit;
end;
modlueHandle:=LoadLibrary(PChar(str));//它的modluehandle总是0
ShowMessage(IntToStr(GetLastError));//他的返回错误值是126
@InstallHook:=GetProcAddress(modlueHandle,'HookInstall');
请问 我这个应该怎么调用这个dll呀? 谢谢!
解决方案 »
- 散分吧,纪念不大顺当的2007年,希望今年顺利点!!!
- 怎么用SQL修改Access中的数字型数据
- 无窗口程序如何响应键盘?xp下
- 为什么我用这个代码时,总是在XLApp := CreateOleObject(‘Excel.Application‘)过不去啊??
- 为何在dbgrideh里编辑日期型字段会出错?
- 用DELPHI编程,在DBGRID中运行时,有修改和编辑功能,但是表格里的数据一直的可 编辑的,不管你是否选修改和编辑按钮,只要单击单元格,数据都是可编辑的,
- 请教高手我想在关闭机器时运行某个程序
- DELPHI的下载问题,高手请速进
- 如何把我的程序压缩到工作区
- 如何获取OpenPictureDialog1打开文件的名称,不包括后缀名
- delphi的dll中用到的bpl都得以共享的方式存在?
- Delphi 三层要怎么弄,哪里有些资料。方法如何实现
不过,你的例子看不出有加sharemem的必要