procedure tform1.edit2keypress(sender: tobject; var key: char);
var
order: integer;
txt: pchar;
pfunc: tfarproc;
moudle: thandle;
begin
moudle := loadlibrary('c:\dlls\example.dll');
if moudle > 32 then
begin
edit2.text := '';
pfunc := getprocaddress(moudle,'instr');
txt := stralloc(80);
txt := strpcopy(txt,edit1.text);
order := tinstr(pfunc)(txt,key);
if order = -1 then
label1.caption := '不包含这个字符 '
end else
label1.caption := '位于第'+inttostr(order+1)+'位';
freelibrary(moudle);
end;
//———————————— 用于实现数据传输的dlls的编写
用于实现数据传输的dlls与一般dlls的编写基本相同,其中特别的地方是:
1. 定义一个全局变量句柄:
var
hmem: thandle;
2. 定义一个过程,返回该全局变量的句柄。该过程要包含在exports子句中。如:
function getglobalmem: thandle; export;
begin
result := hmem;
end;
3. 在初始化代码中分配全局内存块:
程序清单如下:
begin
hmem := globalalloc(gmem_moveable and gmem_ddeshare,num);
if hmem = 0 then
messagedlg('could not allocate memory',mtwarning,[mbok],0);
end.
//————————————————————————————————
num是一个预定义的常数。
表10.3 全局内存块的分配标志 :标 志 意 义
—————————————————————————————————
gmem_ddeshare 分配可由应用程序共享的内存
gmem_discardable 分配可抛弃的内存(只与gmem_moveable连用)
gmem_fixed 分配固定内存