关于DLL的问题,200(200分)

  • 主题发起人 主题发起人 fartripLiao
  • 开始时间 开始时间
F

fartripLiao

Unregistered / Unconfirmed
GUEST, unregistred user!
在DELPHI6中怎么样动态调用DLL。
原来我在D5中的调用方法已不行了网上有的我试了也不行。
请各位高手帮帮小弟附上我原有的代码。
var
MyFunct:TintFunction;//TintFunction在D6中已不存在
begin
Hinst:=loadLibrary(dllname);
if Hinst>0 then
try
FPointer:=GetProcAddress(hinst,'TVCtrl32_SetVideoSource');//FPointer已不存在
if FPointer<>nil then
begin
MyFunct:=tintfunction(FPointer);
myFunct(GetVideoPortNo);
end
else
begin
showmessage('调用系统资源错误,请与开发商联系');
end;
finally
FreeLibrary(Hinst);
end;
end;
 
给你一个在2000里使窗体变半透明的例子。
procedure TFadeWindow.SetWndAlpha(const Alpha: Byte); //判断是否是WIN2000以上的版本。
var
major, minor : integer;
old: longint;
User32: Cardinal;
SetLayeredWindowAttributes: function (hwnd: LongInt; crKey: byte; bAlpha: byte; dwFlags: LongInt): LongInt; stdcall;
begin
GetWindowsVersion(major, minor);
if ((major >= 5) and (minor >= 0)) then //Windows 2000(NT5)
begin
User32 := LoadLibrary('USER32');
if User32 <> 0 then
try
SetLayeredWindowAttributes := GetProcAddress(User32, 'SetLayeredWindowAttributes');
if @SetLayeredWindowAttributes <> nil then
SetLayeredWindowAttributes(Handle,
0,
Alpha,
LWA_ALPHA);
finally
FreeLibrary(User32);
end;
end;
end;
 
Type
OPENDEVICE = Function (nMyAddress : Integer; hWnd : HWND): Pointer; stdcall;
var
lpOpenDevice : OPENDEVICE ;

implementation

function loadfunc():boolean;
begin
result := false;
hLibrary := LoadLibrary('ICDM.dll');
if hLibrary = 0 then
begin
showMessage('error!);
exit;
end;

@lpOpenDevice := GetProcAddress(hLibrary, 'OpenDevice');
if @lpOpenDevice = nil then
begin
FreeLibrary(hLibrary);
exit;
end;
result := true;
end;
 
例子写的不错就是没看明白
 
转一个,可以调用.
unit MainFfm;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;

type
{ First, define a procedural data type, this should reflect the
procedure that is exported from the DLL. }
TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall;

{ Create a new exception class to reflect a failed DLL load }
EDLLLoadError = class(Exception);

TMainForm = class(TForm)
lblDate: TLabel;
btnGetCalendar: TButton;
procedure btnGetCalendarClick(Sender: TObject);
end;

var
MainForm: TMainForm;

implementation

{$R *.DFM}

procedure TMainForm.btnGetCalendarClick(Sender: TObject);
var
LibHandle : THandle;
ShowCalendar: TShowCalendar;
begin

{ Attempt to load the DLL }
LibHandle := LoadLibrary('CALENDARLIB.DLL');
try
{ If the load failed, LibHandle will be zero.
If this occurs, raise an exception. }
if LibHandle = 0 then
raise EDLLLoadError.Create('Unable to Load DLL');
{ If the code makes it here, the DLL loaded successfully, now obtain
the link to the DLL's exported function so that it can be called. }
@ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');
{ If the function is imported successfully, then set lblDate.Caption to reflect
the returned date from the function. Otherwise, show the return raise
an exception. }
if not (@ShowCalendar = nil) then
lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
else
RaiseLastWin32Error;
finally
FreeLibrary(LibHandle); // Unload the DLL.
end;
end;

end.
 
多人接受答案了。
 
后退
顶部