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

調用dll問題

尚未結案
oktony
中階會員


發表:67
回覆:132
積分:64
註冊:2004-12-05

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-05-25 11:27:09 IP:220.161.xxx.xxx 訂閱
小弟要調用一個C寫的SiUSBXp.dll文件,function宣告如下
Prototype: SI_STATUS SI_GetNumDevices (LPDWORD NumDevices)
Parameters: 1. NumDevices—Address of a DWORD variable that will contain the number of devices connected on return
Return Value: SI_STATUS = SI_SUCCESS or
SI_DEVICE_NOT_FOUND or
SI_INVALID_PARAMETER
SI_SUCCESS=0x01;
SI_INVALID_PARAMETER=0x06;
SI_DEVICE_NOT_FOUND=0xff;
此dll有個vb的調用範例:
Public Declare Function SI_GetNumDevices Lib "SiUSBXp.dll" (ByRef lpwdNumDevices As Long) As Integer
-------------------------------------------------------
Dim DevNum As Long
Status = SI_GetNumDevices(DevNum)

而小弟現在在delphi裡面寫
function SI_GetNumDevices(lpdwNumDevices: PLongInt): Integer; stdcall;external 'SiUSBXp.DLL';
-----------------------------------------------------------------------------------------
var
te:PLongInt;
stat:Integer;
procedure TForm1.Button1Click(Sender: TObject);

begin
stat:=SI_GetNumDevices(@te);
end;

則run后結果是正確的,但是會有unsafe code‘@ operator',小弟不太明白lpdwNumDevices的類型是否定義正確,VB裡面有Byref關鍵字來代表傳地址值,delphi裡面應該如何寫才規範呢?謝謝!
AndrewK
高階會員


發表:6
回覆:151
積分:161
註冊:2006-10-09

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-05-27 07:41:56 IP:60.250.xxx.xxx 訂閱
以下資訊給您參考看看
一項有再修正過:
1.VB裡面有Byref關鍵字來代表傳地址值
delphi 可利用 var 來代表傳參考

2.
function SI_GetNumDevices(lpdwNumDevices: PLongInt): Integer; stdcall;external 'SiUSBXp.DLL';

te:PLongInt;
stat:Integer;
procedure TForm1.Button1Click(Sender: TObject);
begin
stat:=SI_GetNumDevices(@te);
我會把他改成
stat:=SI_GetNumDevices(te);

原因:參數型態和變數型態都是 Longint 的指標

3.
以下網址有對於錯誤訊息的介紹
unsafe code‘@ operator'
http://delphi.ktop.com.tw/board.php?cid=30&fid=72&tid=51144
也可以
===================引 用 oktony 文 章===================
小弟要調用一個C寫的SiUSBXp.dll文件,function宣告如下
Prototype: SI_STATUS SI_GetNumDevices (LPDWORD NumDevices)
Parameters: 1. NumDevices—Address of a DWORD variable that will contain the number of devices connected on return
Return Value: SI_STATUS = SI_SUCCESS or
SI_DEVICE_NOT_FOUND or
SI_INVALID_PARAMETER
SI_SUCCESS=0x01;
SI_INVALID_PARAMETER=0x06;
SI_DEVICE_NOT_FOUND=0xff;
此dll有個vb的調用範例:
Public Declare Function SI_GetNumDevices Lib "SiUSBXp.dll" (ByRef lpwdNumDevices As Long) As Integer
-------------------------------------------------------
Dim DevNum As Long
Status = SI_GetNumDevices(DevNum)

而小弟現在在delphi裡面寫
function SI_GetNumDevices(lpdwNumDevices: PLongInt): Integer; stdcall;external 'SiUSBXp.DLL';
-----------------------------------------------------------------------------------------
var
te:PLongInt;
stat:Integer;
procedure TForm1.Button1Click(Sender: TObject);

begin
stat:=SI_GetNumDevices(@te);
end;

則run后結果是正確的,但是會有unsafe code‘@ operator',小弟不太明白lpdwNumDevices的類型是否定義正確,VB裡面有Byref關鍵字來代表傳地址值,delphi裡面應該如何寫才規範呢?謝謝!
------
Just Do It
-------------------------
其實男生不是真的喜歡你不減肥,而是喜歡你愛吃還不肥;也不是真的喜歡你不化妝,而是喜歡你素顏也好看;也不是真的喜歡你瘦,而是喜歡你瘦卻有胸;也不是真喜歡你獨立,而是他忙的時候別煩他。女孩子,太認真你就輸了。
編輯記錄
AndrewK 重新編輯於 2009-05-28 06:38:17, 註解 無‧
AndrewK 重新編輯於 2009-06-26 14:52:06, 註解 無‧
系統時間:2024-03-29 9:47:25
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!