Delphi是否只能使用.dll? [英] Can Delphi only use a .dll if required?

查看:117
本文介绍了Delphi是否只能使用.dll?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 函数Inp(PortAddress:Integer) : 整数;标准外部'inpout32.dll'名称'Inp32'; 

程序输出(PortAddress,Value:Integer);标准外部'inpout32.dll'名'Out32';

然而,我不想使用软件发布inpout32库,除非他们明确需要。目前该程序在执行时表示Not Found,除非它们存在于根或System32中。



如果用户具有特定的选项集,用户将只会调用这些方法,但是直到在使用inpout库之后,才从.ini文件中收集。



有些方法只能在需要时使用这个库,而不是声明它的方式?

解决方案

在2010年之前的Delphi版本中,您必须使用经典的动态加载。考虑这个典型(简单)的示例,调用 Beep 功能从 Kernel32.dll (您应该硬编码路径在真实的代码当然!):

  type 
TBeepFunc = function(dwFreq:DWORD; dwDuration:DWORD) :BOOL;标准

程序TForm4.FormClick(Sender:TObject);
var
lib:HMODULE;
prc:TBeepFunc;
begin

lib:= LoadLibrary('C:\WINDOWS\System32\Kernel32.dll');
如果lib = 0,则RaiseLastOSError;
try
@prc:= GetProcAddress(lib,'Beep');
如果分配(prc)然后
prc(400,2000)
else
ShowMessage('WTF?No Beep in Kernel32.dll?
finally
FreeLibrary(lib);
结束
结束


I have added these two methods to the 1st unit of my Delphi 5 application.

function Inp(PortAddress: Integer): Integer; stdcall; external 'inpout32.dll' name 'Inp32';

procedure Output(PortAddress, Value: Integer); stdcall; external 'inpout32.dll' name 'Out32';

However I don't want to have to issue the inpout32 library with the software unless they explicitly need it. Currently the program says "Not Found" upon executing unless they're present in the root or System32.

Users will only call these methods if they have a specific option set, but this is not gathered from the .ini file until after the inpout library is used.

Is there a way to only use this library when required like some components do, rather than declaring it the way I have?

解决方案

In Delphi versions prior to 2010, you have to use classic dynamic loading. Consider this typical (and simple) example calling the Beep function from Kernel32.dll (which you should not hardcode the path to in real code, of course!):

type
  TBeepFunc = function(dwFreq: DWORD; dwDuration: DWORD): BOOL; stdcall;

procedure TForm4.FormClick(Sender: TObject);
var
  lib: HMODULE;
  prc: TBeepFunc;
begin

  lib := LoadLibrary('C:\WINDOWS\System32\Kernel32.dll');
  if lib = 0 then RaiseLastOSError;
  try
    @prc := GetProcAddress(lib, 'Beep');
    if Assigned(prc) then
      prc(400, 2000)
    else
      ShowMessage('WTF? No Beep in Kernel32.dll?!');
  finally
    FreeLibrary(lib);
  end;
end;

这篇关于Delphi是否只能使用.dll?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆