現有有一個寫好的C語言 DLL 部分代碼如下 實作的功能為讀取E盤目錄下一個.mat檔案的長度
unsigned int ReadMatSize(char *path){
FILE *fp=NULL;
char buffer[BUFFER_SIZE]={0};
unsigned int size=0;
fp=fopen(path,"rb");
if (fp==NULL){
printf("Fail to open file!\n");
fclose(fp);
return(255);
}
fread(buffer,1,188,fp);
clear_buffer(buffer,BUFFER_SIZE);
fread(buffer,1,4,fp);
size=bytes2int(buffer);
size/=8;
return(size);
現在使用Delphi 呼叫這個DLL 采用的是隱式呼叫的方法
unit MatRead;
interface
type
PChar=^AnSiChar;
function ReadMatSize(path:PChar):Integer;cdecl;
implementation
function ReadMatSize;external 'MatReadLib.dll' name 'ReadMatSize';
end.
unit main;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
MatRead;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
s:AnsiString;
P:PChar;
size:Integer;
begin
s:='E:\\test.mat';
p:=PChar(s);
size:=ReadMatSize(P);
end;
end.
應該有的效果是點擊按鈕 完成呼叫 但是編譯不出錯 無結果 無彈窗 size:=ReadMatSize(p);始終無法完成呼叫
uj5u.com熱心網友回復:
上個貼子看到你 size沒有定義 報錯忘了跟你講 s:='E:\\test.mat'; 這句錯了 你這是C的寫法 \是轉義符 但是delphi是不需要 \
還有呼叫約定你得注意下,這個你看看你這個dll工程 如果沒有特別強調VS默認應該是這個 但是建議改為stdcall
uj5u.com熱心網友回復:
你確認是cdecl呼叫方式轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/100403.html
標籤:語言基礎/算法/系統設計
上一篇:delphi操作excel
