用LoadLibrary()The LoadLibrary function maps the specified executable module into the address space of the calling process. HINSTANCE LoadLibrary( LPCTSTR lpLibFileName // address of filename of executable module
);
ParameterslpLibFileNamePoints to a null-terminated string that names the executable module (either a .DLL or .EXE file). The name specified is the filename of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.DEF) file. If the string specifies a path but the file does not exist in the specified directory, the function fails.
If a path is not specified and the filename extension is omitted, the default library extension .DLL is appended. However, the filename string can include a trailing point character (.) to indicate that the module name has no extension. When no path is specified, the function searches for the file in the following sequence: 1. The directory from which the application loaded.
2. The current directory.
3. Windows 95: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.Windows NT: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is SYSTEM32.4. Windows NT: The 16-bit Windows system directory. There is no Win32 function that obtains the path of this directory, but it is searched. The name of this directory is SYSTEM.
5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6. The directories that are listed in the PATH environment variable. The first directory searched is the one directory containing the image file used to create the calling process (for more information, see the CreateProcess function). Doing this allows private dynamic-link library (DLL) files associated with a process to be found without adding the process's installed directory to the PATH environment variable.
Once the function obtains a fully qualified path to a library module file, the path is compared (case independently) to the full paths of library modules currently loaded into the calling process. These libraries include those loaded when the process was starting up as well as those previously loaded by LoadLibrary but not unloaded by FreeLibrary. If the path matches the path of an already loaded module, the function just increments the reference count for the module and returns the module handle for that library. Return ValuesIf the function succeeds, the return value is a handle to the module.
If the function fails, the return value is NULL. To get extended error information, call GetLastError
. ResLoadLibrary can be used to map a DLL module and return a handle that can be used in GetProcAddress to get the address of a DLL function. LoadLibrary can also be used to map other executable modules. For example, the function can specify an .EXE file to get a handle that can be used in FindResource or LoadResource.
Module handles are not global or inheritable. A call to LoadLibrary by one process does not produce a handle that another process can use ?for example, in calling GetProcAddress. The other process must make its own call to LoadLibrary for the module before calling GetProcAddress. If the module is a DLL not already mapped for the calling process, the system calls the DLL's DllEntryPoint function with the DLL_PROCESS_ATTACH value. If the DLL's entry-point function does not return TRUE, LoadLibrary fails and returns NULL.
Windows 95: If you are using LoadLibrary to load a module that contains a resource whose numeric identifier is greater than 0x7FFF, LoadLibrary fails.
);
ParameterslpLibFileNamePoints to a null-terminated string that names the executable module (either a .DLL or .EXE file). The name specified is the filename of the module and is not related to the name stored in the library module itself, as specified by the LIBRARY keyword in the module-definition (.DEF) file. If the string specifies a path but the file does not exist in the specified directory, the function fails.
If a path is not specified and the filename extension is omitted, the default library extension .DLL is appended. However, the filename string can include a trailing point character (.) to indicate that the module name has no extension. When no path is specified, the function searches for the file in the following sequence: 1. The directory from which the application loaded.
2. The current directory.
3. Windows 95: The Windows system directory. Use the GetSystemDirectory function to get the path of this directory.Windows NT: The 32-bit Windows system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is SYSTEM32.4. Windows NT: The 16-bit Windows system directory. There is no Win32 function that obtains the path of this directory, but it is searched. The name of this directory is SYSTEM.
5. The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
6. The directories that are listed in the PATH environment variable. The first directory searched is the one directory containing the image file used to create the calling process (for more information, see the CreateProcess function). Doing this allows private dynamic-link library (DLL) files associated with a process to be found without adding the process's installed directory to the PATH environment variable.
Once the function obtains a fully qualified path to a library module file, the path is compared (case independently) to the full paths of library modules currently loaded into the calling process. These libraries include those loaded when the process was starting up as well as those previously loaded by LoadLibrary but not unloaded by FreeLibrary. If the path matches the path of an already loaded module, the function just increments the reference count for the module and returns the module handle for that library. Return ValuesIf the function succeeds, the return value is a handle to the module.
If the function fails, the return value is NULL. To get extended error information, call GetLastError
. ResLoadLibrary can be used to map a DLL module and return a handle that can be used in GetProcAddress to get the address of a DLL function. LoadLibrary can also be used to map other executable modules. For example, the function can specify an .EXE file to get a handle that can be used in FindResource or LoadResource.
Module handles are not global or inheritable. A call to LoadLibrary by one process does not produce a handle that another process can use ?for example, in calling GetProcAddress. The other process must make its own call to LoadLibrary for the module before calling GetProcAddress. If the module is a DLL not already mapped for the calling process, the system calls the DLL's DllEntryPoint function with the DLL_PROCESS_ATTACH value. If the DLL's entry-point function does not return TRUE, LoadLibrary fails and returns NULL.
Windows 95: If you are using LoadLibrary to load a module that contains a resource whose numeric identifier is greater than 0x7FFF, LoadLibrary fails.
解决方案 »
- 哇咔,只能发不能看,我们就用标题聊天吧~~~
- delphi: OleContainer显示excel表格,如何实现、修改提示、保存、打印预览、打印等?
- delphi 操作 ini 文件
- 这个(dxdbtrel.dcu)是什么控件用???(急)
- 寻找《Advanced Windows》及其附书光盘
- 请教一个有关DLL释放的问题
- 一个小问题(主丛表录入),希望各位路过来看看。
- SQL server中类型只有datetime型,无date型,那我用datetimepicker1.date时输入日期数据时为什么都同时产生了一个默认时间?
- 哪儿有delphi控件下载?
- 请教大虾们:如何检测自己的程序消耗了多少系统资源?(内详)
- 求救:自己用vc写的一个动态连接库,用pb可正确调用,但delphi却调用错误。
- 怎么知道两个datetime相差多少秒??
library DLLPrj;uses
ShareMem, SysUtils, Windows,
Classes,
DLLunit in 'DLLunit.pas';exports //here to export the function name
Add; //只要函数名就可以了{$R *.RES}procedure DLLMain(dwReason: DWORD);
begin
case dwReason of //of couse you can write no code follow {}
DLL_PROCESS_ATTACH: {add some code};
DLL_PROCESS_DETACH: {add some code};
DLL_THREAD_ATTACH: {add some code};
DLL_THREAD_DETACH: {add some code};
end;//case
end;begin
{The two statement must be added}
DLLProc := @DLLMain;
DLLMain(DLL_PROCESS_ATTACH);
end.///================DLL uNit ===========或者直接把函数写在Project文件里也可以;
unit DLLunit;interfacefunction Add(X, Y: integer): integer; stdcall;implementationfunction Add(X, Y: integer): integer;
begin
Result := X+Y;
end;end.//============调用Add函数
unit NormalUnit;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls, Buttons;type
TMainForm = class(TForm)
Edit1: TEdit;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;//这句是关键,它通过Delphi隐式的调用了DLL中的函数Add,不需要LoadLibrary什么的
function Add(X, Y: integer): integer; stdcall; external 'DLLPrj.DLL' name 'Add';var
MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.Button1Click(Sender: TObject);
begin
Edit1.Text := IntToStr(Add(1, 2));
end;end.
然后调用 GetProcAddress()
type
TAdd = function(X, Y: integer): integer; stdcall;
var
hDLL: THandle;
Add: TAdd;
begin
hDLL := 0;
try
hDLL := LoadLibrary('DLLPrj.DLL');
if (hDLL>0) then begin
Add := GetProcAddress(hDLL, 'Add');
Edit1.Text := IntToStr(Add(1, 2));
end else begin
raise Exception.Create('Load DLLPrj.DLL failed');
end;//if
finally
Windows.FreeLibrary(hDLL);
end;//try...finally
end;