線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1221
推到 Plurk!
推到 Facebook!

請問function type 的用途

答題得分者是:william
cyl
中階會員


發表:163
回覆:171
積分:66
註冊:2002-07-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2002-11-20 11:30:25 IP:61.218.xxx.xxx 未訂閱
unit DllUtils;    interface    uses   Windows, Messages, SysUtils, Classes, Forms, Controls;    type   IPlugin = interface   ['{D3F4445A-C704-42BC-8283-822541668919}']       function CreateForm(hMainForm: THandle): THandle;     procedure DestroyForm;     function ShowModalForm: Integer;   end;     TCreatePluginFunc = function (hApp: THandle): IPlugin; stdcal; function DllCreatePlugin(hLib, hApp: THandle): IPlugin; implementation resourcestring sErrorLoadingDLL = '無法載入模組!'; sErrorDllProc = '無法呼叫 DLL 函式: %s'; const SDllCreatePluginFuncName = 'CreatePlugin'; function DllCreatePlugin(hLib, hApp: THandle): IPlugin; var pProc: TFarProc; CreatePluginFunc: TCreatePluginFunc; begin Result := nil; if hLib = 0 then Exit; pProc := GetProcAddress(hLib, PChar(SDllCreatePluginFuncName)); if pProc = nil then raise Exception.CreateFmt(sErrorDllProc, [SDllCreatePluginFuncName]); CreatePluginFunc := TCreatePluginFunc(pProc); Result := CreatePluginFunc(hApp); end; end. 請問以上的code藍色部分的宣告我不是很了解,請問有人可以解釋給我了解嗎?為什麼 發表人 -
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2002-11-20 11:41:12 IP:147.8.xxx.xxx 未訂閱
It defines a function type "TCreatePluginFunc", which is accepting parameters hLib, hApp as handles and returning an interface IPlugin. stdcal (typing error? should be stdcall) is used to specify calling convention for this kind of function.
cyl
中階會員


發表:163
回覆:171
積分:66
註冊:2002-07-11

發送簡訊給我
#3 引用回覆 回覆 發表時間:2002-11-20 11:59:28 IP:61.218.xxx.xxx 未訂閱
引言: It defines a function type "TCreatePluginFunc", which is accepting parameters hLib, hApp as handles and returning an interface IPlugin. stdcal (typing error? should be stdcall) is used to specify calling convention for this kind of function.
那請問通常function 不是應該有function name,為什麼這個沒有name,還有一般function通常都沒有定義function type,而這種function為何要定義function type,通常用於何處?? 因為我的觀念非常不好,所以想請教一下,釐清一些觀念
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2002-11-20 13:48:14 IP:147.8.xxx.xxx 未訂閱
Becasue GetProcAddress returns ONLY an address (i.e. a pointer), you need to "type cast" it as a function in order to use it. For example:
type
    TMyFunction = function(Value: integer):integer;
...
function Func1(Value: integer): integer;
function Func2(Value: integer): integer;
...
var
    MyFunc : TMyFunction
....
    MyFunc := Func1;
    i := MyFunc(1); // same as i := Func1(1)
    MyFunc := Func2;
    i := MyFunc(1); // same as i := Func2(1)
系統時間:2024-05-09 10:07:13
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!