Использование C-шной DLL
Автор: Xavier Pacheco
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TMainForm = class(TForm)
Button1: TButton;
Button2: TButton;
FooData: TEdit;
Button3: TButton;
Button4: TButton;
SetCVarData: TEdit;
GetCVarData: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
_GlobalVar: PChar = 'This is a Delphi String';
implementation
uses PasStrng;
{$R *.DFM}
{$L ccode.obj}
type
TFoo = class
function Function1(Str1: PChar): integer; virtual; cdecl; abstract;
function Function2(i: integer): integer; virtual; cdecl; abstract;
end;
PCharArray = ^TCharArray;
TCharArray = array[0..127] of char;
// import from OBJ file:
function _SAYHELLO(Text: PChar): Integer; cdecl; external;
procedure _C_VAR; external; // trick to import OBJ data
// imports from DLL file:
function ClassFactory: TFoo; cdecl; external 'cdll.dll'
name '_CLASSFACTORY';
procedure ClassKill(Foo: TFoo); cdecl; external 'cdll.dll'
name '_CLASSKILL';
procedure TMainForm.Button1Click(Sender: TObject);
begin
_SayHello('hello world');
end;
procedure TMainForm.Button2Click(Sender: TObject);
var
Foo: TFoo;
begin
Foo := ClassFactory;
Foo.Function1('huh huh, cool.');
FooData.Text := IntToStr(Foo.Function2(10));
ClassKill(Foo);
end;
function GetCArray: string;
var
A: PCharArray;
begin
A := PCharArray(@_C_VAR);
Result := A^;
end;
procedure SetCArray(const S: string);
var
A: PCharArray;
begin
A := PCharArray(@_C_VAR);
StrLCopy(A^, PChar(S), SizeOf(TCharArray));
end;
procedure TMainForm.Button3Click(Sender: TObject);
begin
SetCArray(SetCVarData.Text);
end;
procedure TMainForm.Button4Click(Sender: TObject);
begin
GetCVarData.Text := GetCArray;
end;
end.
Скачать весь проект
|