我看到段代码想转换成C#的代码.以下这段代码转换成C#应该怎么写?~~谢谢各位大虾帮个忙~~ library HookKey;{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
Classes,
Windows,
Messages;type
THookData=record
hook:HHOOK; //钩子句柄
winHwnd:HWND; //安装钩子窗体句柄
sendMsg:BOOL;//是否发送消息,true发送,false不发送
end;
PHookData=^THookData; //钩子记录指针var
hookData: PHookData;
mapHandle: THandle;
const cMMFileName: PChar = 'KeyBoradHookData';
const WM_KeyBoardHook: HWND = WM_USER + $1111; //窗体消息定义{$R *.res}
//钩子子程序
function KeyBoardHookProc(code:Integer;wp:WPARAM;lp:LPARAM):LRESULT;stdcall;
begin
if lp>=0 then
begin
//MessageBox(0,pchar(Floattostr(hookData.winHwnd)),'提示',MB_ICONINFORMATION or MB_OK);
if hookData.sendMsg then
SendMessage(hookData.winHwnd,WM_KeyBoardHook,wp,lp);
end;
result:= CallNextHookEx(HookData.hook,code,wp,lp);
end;//设置键盘系统钩子
function installHook(winHwnd:HWND):boolean;stdcall;
begin
hookData.winHwnd:=winHwnd;
hookData.sendMsg:=true;
if hookData.hook>0 then
UnhookWindowsHookEx(hookData.hook);
hookData.hook:=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHookProc,HInstance,0);
if hookData.hook>0 then
result:= True
else
result:= False;
end;//卸载已经安装的钩子
function uninstallHook():boolean;stdcall;
var
rtn:BOOL;
begin
rtn:=true;
if hookData.hook>0 then
rtn:=UnhookWindowsHookEx(hookData.hook);
Result:=rtn;
end;procedure setSendMsgStatus(sendMsgStatus: boolean);stdCall;
begin
hookData.sendMsg:= sendMsgStatus;
end;procedure openSharedData;//建立共享内存
var
size: Integer;
begin
size:= SizeOf(hookData);
mapHandle:= CreateFileMapping(DWord(-1),nil,PAGE_READWRITE,0,size,cMMFileName);
if mapHandle=0 then
if GetLastError= ERROR_ALREADY_EXISTS then
begin
mapHandle:= OpenFileMapping(FILE_MAP_ALL_ACCESS,False,cMMFileName);
if mapHandle=0 then Exit;
end
else
exit;
hookData:= MapViewOfFile(mapHandle,FILE_MAP_ALL_ACCESS,0,0,size);
if hookData=nil then
CloseHandle(mapHandle);
//RaiseLastWin32Error;
end;procedure closeSharedData;
begin
if Assigned(hookData) then
begin
UnmapViewOfFile(hookData);
if mapHandle<>0 then CloseHandle(mapHandle);
hookData := nil;
end;
end;procedure DLLEntryPoint(dwReason: DWord);
begin
case dwReason of
DLL_PROCESS_ATTACH: openSharedData;
DLL_PROCESS_DETACH: closeSharedData;
DLL_THREAD_ATTACH:;
DLL_THREAD_DETACH:;
end;
end;exports
installHook, uninstallHook, setSendMsgStatus;
begin
DLLProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }uses
SysUtils,
Classes,
Windows,
Messages;type
THookData=record
hook:HHOOK; //钩子句柄
winHwnd:HWND; //安装钩子窗体句柄
sendMsg:BOOL;//是否发送消息,true发送,false不发送
end;
PHookData=^THookData; //钩子记录指针var
hookData: PHookData;
mapHandle: THandle;
const cMMFileName: PChar = 'KeyBoradHookData';
const WM_KeyBoardHook: HWND = WM_USER + $1111; //窗体消息定义{$R *.res}
//钩子子程序
function KeyBoardHookProc(code:Integer;wp:WPARAM;lp:LPARAM):LRESULT;stdcall;
begin
if lp>=0 then
begin
//MessageBox(0,pchar(Floattostr(hookData.winHwnd)),'提示',MB_ICONINFORMATION or MB_OK);
if hookData.sendMsg then
SendMessage(hookData.winHwnd,WM_KeyBoardHook,wp,lp);
end;
result:= CallNextHookEx(HookData.hook,code,wp,lp);
end;//设置键盘系统钩子
function installHook(winHwnd:HWND):boolean;stdcall;
begin
hookData.winHwnd:=winHwnd;
hookData.sendMsg:=true;
if hookData.hook>0 then
UnhookWindowsHookEx(hookData.hook);
hookData.hook:=SetWindowsHookEx(WH_KEYBOARD,@KeyBoardHookProc,HInstance,0);
if hookData.hook>0 then
result:= True
else
result:= False;
end;//卸载已经安装的钩子
function uninstallHook():boolean;stdcall;
var
rtn:BOOL;
begin
rtn:=true;
if hookData.hook>0 then
rtn:=UnhookWindowsHookEx(hookData.hook);
Result:=rtn;
end;procedure setSendMsgStatus(sendMsgStatus: boolean);stdCall;
begin
hookData.sendMsg:= sendMsgStatus;
end;procedure openSharedData;//建立共享内存
var
size: Integer;
begin
size:= SizeOf(hookData);
mapHandle:= CreateFileMapping(DWord(-1),nil,PAGE_READWRITE,0,size,cMMFileName);
if mapHandle=0 then
if GetLastError= ERROR_ALREADY_EXISTS then
begin
mapHandle:= OpenFileMapping(FILE_MAP_ALL_ACCESS,False,cMMFileName);
if mapHandle=0 then Exit;
end
else
exit;
hookData:= MapViewOfFile(mapHandle,FILE_MAP_ALL_ACCESS,0,0,size);
if hookData=nil then
CloseHandle(mapHandle);
//RaiseLastWin32Error;
end;procedure closeSharedData;
begin
if Assigned(hookData) then
begin
UnmapViewOfFile(hookData);
if mapHandle<>0 then CloseHandle(mapHandle);
hookData := nil;
end;
end;procedure DLLEntryPoint(dwReason: DWord);
begin
case dwReason of
DLL_PROCESS_ATTACH: openSharedData;
DLL_PROCESS_DETACH: closeSharedData;
DLL_THREAD_ATTACH:;
DLL_THREAD_DETACH:;
end;
end;exports
installHook, uninstallHook, setSendMsgStatus;
begin
DLLProc := @DLLEntryPoint;
DLLEntryPoint(DLL_PROCESS_ATTACH);
end.
上面有一个扫描枪例子,你可以下载下来看看,那个类可以拿过来直接用.这是个系统钩子
楼主看对你有帮助没