六 调用动态载入
1 建立一种过程类型[如果你对过程类型的变量只是一个指针的本质清楚的话,你就知道是怎么回事了]。如:
type
mypointer=procedure(form:Tform);Far;external;
var
Hinst:Thandle;
showform:mypointer;
begin
Hinst:=loadlibrary('yproject_dll');//Load一个Dll,按文件名找。
showform:=getprocaddress(Hinst,'showform');//按函数名找,大小写敏感。如果你知道自动化对象的本质就清楚了。
showform(application.mainform);//找到函数入口指针就调用。
Freelibrary(Hinst);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Text: Pchar;
begin
Text := Pchar(Edit1.Text);
// OpenForm(Application.MainForm);//为了调MDICHILD
InputCCC(Text);//为了实验DLL中的全局变量是否在各个应用程序间共享
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowCCC;//这里表明WINDOWS 32位应用程序DLL中的全局变量也是在应用程序地址空间中,16位应用程序或许不同,没有做实验。
end;
//In Delphi:
//This is a procedure exported out of TestLib.Dll.
procedure Proc(lpszText: PChar; nSize: Integer);
var
i: Integer;
begin
for i := 0 to nSize - 1 do
lpszText[i] := 'X'; //Just a demonstation perform.
end;
//In PB
//Declare external function(subroutine)
Subroutine Proc(Ref String lpszTest) Library "TestLib.dll"
//Statement goes here
String ls_Test
//What type of the ls_Text? (ANSI? UNICODE? DBCS?)
ls_Text = "CSDN 中国程序员"
Proc(ls_Text)
MessageBox("CSDN", ls_Text) //The contents of ls_Text has been changed?
//In VB
//Declare external function(sub)
//Can you tell me that does the declaration have error?
Private Declare Sub Proc Lib "TestLib.dll" (ByRef lpszText As String)
Private Sub Test()
Dim strText
strText = "CSDN 中国程序员"
//The type of strText if DBCS(UNICODE?)
Proc(strText) //Error most occurs here.
MsgBox strText
End Sub