Home > Back-end >  Delphi call C of the DLL
Delphi call C of the DLL

Time:09-30

Existing have a write good C language DLL code below the functions to read E plate of a directory. The length of the mat files
Unsigned int ReadMatSize (char * path) {
The FILE * fp=NULL;
Char buffer [BUFFER_SIZE]={0};
Unsigned int size=0;

Fp=fopen (path, "rb");

If (fp==NULL) {
Printf (" Fail to open the file! \n");
The fclose (fp);
Return (255);
}

Fread (buffer, 1188, fp);
Clear_buffer (buffer, BUFFER_SIZE);
Fread (buffer, 1, 4, fp);
Size=bytes2int (buffer);
The size/=8;
Return (size);
Now use the Delphi call this DLL is implicit call methods
The unit MatRead;
Interface
Type
PChar=^ AnSiChar;
The function ReadMatSize (path: PChar) : Integer; Cdecl;
Implementation
The function ReadMatSize; External 'MatReadLib. DLL' name 'ReadMatSize';
End.
The unit main;

Interface

USES the
Winapi. Windows, Winapi Messages, System. SysUtils, System. Variants, System. Classes, Vcl. Graphics,
The Vcl. Controls, Vcl. Forms, Vcl. Dialogs, Vcl. StdCtrls;

Type
TForm1=class (TForm)
For: TButton;
Procedure Button1Click (Sender: TObject);
Private
{Private declarations}
Public
{Public declarations}
end;

Var
Form1: TForm1;

Implementation
USES the
MatRead;

{$R *. DFM}

Procedure TForm1. Button1Click (Sender: TObject);
Var
S: AnsiString.
P: PChar;
Size: Integer;
The begin
S:='E: \ \ test. The mat';
P:=PChar (s);

Size:=ReadMatSize (P);

end;

End.
Should compile some effect is click the button to complete the call but don't go wrong no result no popup window size:=ReadMatSize (p); Always unable to complete calls

CodePudding user response:

On a post to see you in the size is not defined error

Forget to tell you about the s:='E: \ \ test mat'; This sentence is wrong you and this is the C way \ is escape characters but Delphi is don't need \
And calling conventions you must pay attention to, this you see your this DLL project if there is no special emphasis on VS the default should be this but suggested stdcall instead

CodePudding user response:

Your confirmation is cdecl call way
  • Related