全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1568
推到 Plurk!
推到 Facebook!

動態陣列當參數傳給dll(重複的主題)

答題得分者是:william
AJ
一般會員


發表:9
回覆:19
積分:20
註冊:2003-06-27

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-08-28 14:14:39 IP:211.21.xxx.xxx 未訂閱
看過前面的文章,還是有看沒有懂,所以做了實驗, 當我要使用動態array作參數,傳給function時, 卻出現型態不符的錯誤,程式碼如下:
procedure TForm1.Button1Click(Sender: TObject);
var pdata : pchar;
    len,cnt : integer;
    buffer : array of char;
begin
  setlength(buffer,10);
  len := get(buffer);   //這裡發生錯誤: Incompatible types: 'Array' and 'dynamic array'
  pdata := pchar(allocmem(len 1));
  for cnt:=0 to len-1 do
    pdata[cnt] := data[cnt];
  edit4.Text := strpas(pdata);
end;    function TForm1.Get(var buf : array of char):integer;
const test_data : array[0..8] of char =('1','2','3','4','5','6','7','8','9');
var len,cnt : integer;
begin
  len := length(test_data);
  for cnt:=0 to len-1 do
    buf[cnt] := test_data[cnt];
  result := len;
end;
以上是按照之前文章的方式去做,把get function的動態陣列宣告成var,結果是不行~不知道我哪個地方作錯了?於是又作修改,用type來宣告動態array就解決問題了~但是當我將get function放到dll中,又發生問題了,程式碼如下:
============Darray.dll============
unit Function_unit;
interface
uses Sysutils;    const CBuffer : Array[0..9] of Char = ('1','2','3','4','5','6','7','8','9','0');    type tbuffer = array of char;    function Get(var Buffer : tbuffer):integer;stdcall;    implementation    function Get(var Buffer :tbuffer):integer;
var len,cnt : integer;
begin
  len := length(cBuffer);
  setlength(buffer,len);
  for cnt:=0 to len-1 do
    buffer[cnt]:= cBuffer[cnt];
  result := len;
end;
end.    ==================test.exe============
  ...
type
  Tbuffer = array of char;
  TGet = function(var Buffer : Tbuffer):integer;stdcall;
 
var
  Form1: TForm1;
  dll_handle : THandle;   
  Get : TGet;    implementation    {$R *.DFM}    procedure TForm1.Button1Click(Sender: TObject);
var cbuffer : tbuffer;
    data : pchar;
    data_len,cnt : integer;
begin
  dll_handle:=loadlibrary(PCHAR('Darray.dll'));    //load dll file
  if dll_handle=0 then
  begin
    showmessage('DLL檔無法載入');
    exit;
  end else
  begin
    @Get := GetProcAddress(dll_handle, pchar('Get'));
    if @Get = nil then
      begin
        showmessage('無法呼叫DLL函數');
        exit;
      end else
      begin
        data_len := Get(cbuffer);   
      end;
  end;
  data := pchar(allocmem(data_len 1));
  for cnt:=0 to data_len-1 do
    data[cnt] :=  cbuffer[cnt];
  edit1.Text := strpas(data);   //這裡發生問題:Invalid pointer operation
end;    procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if dll_handle<>0 then FreeLibrary(dll_handle);
end;    end.
相同的做法但把get function放在dll中就有問題,array的值可以正確傳遞無誤,但還是有錯誤,看錯誤訊息是指標問題,小弟才疏學淺,實在不懂怎會有指標問題,前輩們能幫我解釋一下為什麼呢?又該如何解決?是不是我什麼地方有疏失? Delphi新手,很多觀念不懂!! 發表人 - AJ 於 2003/08/28 14:39:15
william
版主


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-08-28 14:39:02 IP:147.8.xxx.xxx 未訂閱
You should use ShareMem in both your dll and your host application projects as the very first unit. BTW, you should free the memory you allocated, otherwise... memory leak....
AJ
一般會員


發表:9
回覆:19
積分:20
註冊:2003-06-27

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-09-01 12:18:32 IP:211.21.xxx.xxx 未訂閱
謝謝william的提示 看了一下help,對ShareMem大致了解,我會試試ShareMem的用法 之前試了各種可能,覺得還是直接使用指標(PByteArray) 來作會簡單一點(可能是我自己比較熟悉的關係)。 非常感謝大大的指導。
系統時間:2024-05-08 17:39:35
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!