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

C#呼叫Delphi DLL

答題得分者是:sryang
sj123
一般會員


發表:2
回覆:3
積分:1
註冊:2004-04-26

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-09-02 12:09:19 IP:220.128.xxx.xxx 未訂閱
請問各位高手:
現在有一個Delphi寫的DLL,我需要用C#去呼叫並取得回傳值,
目前問題是我無法接收到正確PortInfo.sPortName[0].的值,是哪裡有問題呢?

Delphi中的程式如下(TEST.dll):
type
TPort_Information = record
iPortID : integer;
sPortName : pchar,
iPortType : integer;
end;

function Get_Info(var Port_Information:TPort_Information ) : integer ;
begin
Port_Information.iPortID:=0;
Port_Information.iPortType:=2;
Port_Information.sPortName[0]:=char(10);
result:=0;
end;

C#目前寫法為:
public struct TPort_Information
{
public int iPortID;
public char[] sPortName;
public int iPortType;
}
[DllImport("TEST.dll", EntryPoint = "Get_Info", CallingConvention = CallingConvention.StdCall)]
public static extern int m_Get_Info(ref TPort_Information fPort_Information);

private void button1_Click(object sender, EventArgs e)
{
TPort_Information PortInfo;
PortInfo.iPortID = 0;
PortInfo.iPortType = 0;
PortInfo.sPortName = new char[5];
PortInfo.sPortName[0] = 'A';
m_Get_Info(ref PortInfo);
}
sryang
尊榮會員


發表:39
回覆:762
積分:920
註冊:2002-06-27

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-09-06 18:18:17 IP:124.10.xxx.xxx 訂閱
function Get_Info(var Port_Information:TPort_Information ) : integer ; stdcall;

別忘了 stdcall!
------
歡迎參訪 "腦殘賤貓的備忘錄" http://maolaoda.blogspot.com/
系統時間:2024-05-06 18:40:57
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!