有谁愿意公开一些自己做的DLL文件源码? (20分)

  • 主题发起人 主题发起人 嫩手
  • 开始时间 开始时间
当你的dll使用string类型的参数或函数时才要这样。
对,只要在.dpr里,要放第一个,且dll和调用它的exe都是这样。
 
to zw84611:
你的意思是如果是string类型的,才用stdcall和export,如果是其它类型的,就不
用写了?
 
不,我说的是ShareMem

 
你的意思是:只有用string类型的时候才用ShareMem,而且还在把它放在.dpr引用的第
一个?
 
我给你个例子吧:

mouse hook:

dll:
------------------
library MHK;

{ 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,
windows,
messages,
//Classes,
MapFileUnit in 'MapFileUnit.pas';

var
hNextHookProc: HHook;
HMapFile:THandle;
CommonData:pCommonData;
ZW_MSG : UINT;

{$R *.RES}

{================== Install Mouse Hook Support ==============================}
function MousePosHookHandler(iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
pMouse : PMOUSEHOOKSTRUCT;
begin

if iCode < 0 then
begin
Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);
windows.Beep(1000,200);
exit;
end;

pMouse := PMOUSEHOOKSTRUCT(lParam);

if (wParam = WM_MOUSEMOVE) then
begin
CommonData.MousePos := pMouse.pt;
//CommonData.hWndCapture := pMouse.hWnd;
//PostMessage(CallBackHandle, 0, 0, 1);
PostMessage(CommonData^.CallBackHandle, ZW_MSG, 0, 1);
//windows.Beep(1000,200);
end;

Result := CallNextHookEx(hNextHookProc, iCode, wParam, lParam);

end;

function EnableMouseHook(hld:hwnd): BOOL; stdcall;export;
begin
Result := False;
if hNextHookProc <> 0 then Exit;

hNextHookProc := SetWindowsHookEx(WH_MOUSE, MousePosHookHandler,Hinstance, 0);

if CommonData <> nil then
begin
CommonData^.CallBackHandle := hld;
//CommonData^.CallBackProcID := ProcessID;
end;

Result :=hNextHookProc <> 0 ;
end;

function DisableMouseHook: BOOL; stdcall;export;
begin
try
if hNextHookProc <> 0 then
begin
UnhookWindowshookEx(hNextHookProc);
hNextHookProc := 0;
end;
Result := hNextHookProc = 0;
except
MessageBeep(0);
end;
end;

procedure DLlMain(dwReason: DWORD);
begin
case dwReason of

DLL_PROCESS_ATTACH:
begin
HMapFile:=0;
MapCommonData(CommonData,HMapFile);
end; // DLL_PROCESS_ATTACH:

DLL_PROCESS_DETACH:
begin
if CommonData<>nil then UnMapCommonData(CommonData,HMapFile);
end; // DLL_PROCESS_DETACH:

end; //calse
end;



exports
EnableMouseHook,
DisableMouseHook;

begin
hNextHookProc := 0;
ZW_MSG := RegisterWindowMessage('WM_ZWNOTIFY');
CommonData := nil;
DLLProc := @DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
end.

======================
exe:
----------------------
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
MapFileUnit;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
procedure WndProc(var Mess: TMessage); override;
end;

var
Form1: TForm1;
ZW_MSG : UINT;
HMapFile:THandle;
CommonData:pCommonData;

implementation

{$R *.DFM}

function EnableMouseHook(hld:hwnd): boolean; stdcall; external 'MHK.dll';
function DisableMouseHook: boolean; stdcall; external 'MHK.dll';

procedure TForm1.WndProc(var Mess: TMessage);
begin
if (mess.msg = ZW_MSG) then
begin
caption := 's';
if CommonData<>nil then with CommonData^ do
Caption := Format('Mouse Pos: %d, Y : %d',
[MousePos.X,
MousePos.Y]);
end;
inherited;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ZW_MSG := RegisterWindowMessage('WM_ZWNOTIFY');
//CommonData := nil;
MapCommonData(CommonData,HMapFile);
if not(EnableMouseHook(handle)) then ShowMessage('Enable Mouse Hook failed.');
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if not(DisableMouseHook) then ShowMessage('Disable Mouse Hook failed.');
if CommonData<>nil then UnMapCommonData(CommonData,HMapFile);
end;

end.

 
那我还想知道,你写的dll里,有的时候写stdcall;export,有时候不写是怎么回事?
有什么区别吗?
 
对于给exe调用的函数是要写export的。
stdcall则不一定要有。stdcall表示是Windows API的标准调用方式
 
那你有时写,有时不写,运动时会有什么问题吗?
 
我什么时候“有时写,有时不写”了?[:)]
 
你刚才不是给了我两个地址吗?一个是KeyboardHook的,一个是MouseHook的,在MapFileUnit
单元里好像是有两过程都没有使用stdcall;export
 
to zw84611:
我不是给你找麻烦,我是想搞清楚是怎么回事,我写的时候应该注意什么
 
还有是不是在implementation的上面和下面函数或过程后面都要加stdcall;export
 
MapFileUnit中的是内部函数,不是dll提供exe调用的函数!
另外,dll和exe中的申明要一致,要么都有stdcall,要么都没有。
 
噢,谢谢!对了,你有QQ吗?我可以把你加为好友吗?以后向你好好学习
 
sorry,我没有QQ
 
没关系,谢谢!
 
多人接受答案了。
 
后退
顶部