<<DLL中的回调函数>>(100分)

  • 主题发起人 主题发起人 powersite
  • 开始时间 开始时间
P

powersite

Unregistered / Unconfirmed
GUEST, unregistred user!
DLL怎样通知客户程序?请讲解一下使用回调函数的具体用法?
 
在调用DLL中函数的时候传给它一个指向回调函数的指针。
然后在DLL中的函数过程里调用回调函数。
必须确保回调函数指针是有效的。
 


[h1]GetProcAddress[/h1]
The GetProcAddress function retrieves the address of an exported function or variable from the specified dynamic-link library (DLL).

FARPROC GetProcAddress(
HMODULE hModule, // handle to DLL module
LPCSTR lpProcName // function name
);
Parameters
hModule
[in] Handle to the DLL module that contains the function or variable. The LoadLibrary or GetModuleHandle function returns this handle.
lpProcName
[in] Pointer to a null-terminated string containing the function or variable name, or the function's ordinal value. If this parameter is an ordinal value, it must be in the low-order word
the high-order word must be zero.
Return Values
If the function succeeds, the return value is the address of the exported function or variable.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks
The spelling and case of a function name pointed to by lpProcName must be identical to that in the EXPORTS statement of the source DLL's module-definition (.Def) file. The exported names of functions may differ from the names you use when calling these functions in your code. This difference is hidden by macros used in the SDK header files. For more information, see Conventions for Function Prototypes.

The lpProcName parameter can identify the DLL function by specifying an ordinal value associated with the function in the EXPORTS statement. GetProcAddress verifies that the specified ordinal is in the range 1 through the highest ordinal value exported in the .DEF file. The function then uses the ordinal as an index to read the function's address from a function table. If the .DEF file does not number the functions consecutively from 1 to N (where N is the number of exported functions), an error can occur where GetProcAddress returns an invalid, non-NULL address, even though there is no function with the specified ordinal.

In cases where the function may not exist, the function should be specified by name rather than by ordinal value.

Windows 95/98/Me: GetProcAddress is supported by the Microsoft Layer for Unicode to provide more consistent behavior across all Windows operating systems. To use this version, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.

Example Code
For an example, see Using Run-Time Dynamic Linking.

Requirements
Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Winbase.h
include Windows.h.
Library: Use Kernel32.lib.

See Also
Dynamic-Link Libraries Overview, Dynamic-Link Library Functions, FreeLibrary, GetModuleHandle, LoadLibrary

 
你可以首先定义一个变量(pchar型,为的是像C程序也可以用),用此变量调用DLL中的函数,
函数调用成功后,变量中的内容,即为你的回调内容.
 
举个例子:

DLL中:
type
TCallBackFunc = procedure(x,y: integer);stdcall


var
CallBackFunc: TCallBackFunc


//赋值接口
procedure SetCallBackFunc(Func: TCallBackFunc);stdcall

begin
CallBackFunc := Func;
end;

DLL中调用:
if assigned(CallBackFunc) then CallBackFunc(CommonData.MousePos.x,CommonData.MousePos.y);
---------------------------
外部调用程序:

type
TCallBackFunc = procedure(x,y: integer);stdcall


procedure SetCallBackFunc(Func: TCallBackFunc);stdcall
external '../mousehook.dll';

procedure MyCallBack(x,y: integer);stdcall;
begin
Form1.Caption := format('%d:%d',[x,y]);
end;

//赋值
SetCallBackFunc(MyCallBack);
 
接受答案了
 
后退
顶部