C/C++如何调用Delphi编写的dll
我用delphi封装了spcomm控件,做成了一个dll,但是不知道c/c++如何调用,请给个示例
delphi的dll源码为:
library ComPort;
uses
SysUtils,
Classes,
Spcom in 'Spcom.pas';
exports
SetCallback,
OpenPort,
ClosePort,
OutDate;
begin
end.
unit Spcom;
interface
uses
Windows, SysUtils, spcomm;
type
TMYOBJ = class
procedure MYComReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);
end;
type
TCallback = procedure(s: Pchar); stdcall;
var
Read_busy, Open_busy, Port_active, Receive_finish: BOOLEAN;
S_DATA: string;
MYCOM: TComm;
MYOBJ: TMYOBJ;
FCallback: TCallback;
function OpenPort(Port: shortstring; BTL: INTEGER): INTEGER; stdcall;
function ClosePort: INTEGER; stdcall;
function OutDate(SD: string): INTEGER; stdcall;
procedure SetCallback(ACallback: TCallback); stdcall;
implementation
procedure INI_OBJ;
begin
MYCOM := TCOMM.Create(nil);
MYCOM.OnReceiveData := MYOBJ.MYComReceiveData;
end;
procedure FREE_OBJ;
begin
try
if MYOBJ <> nil then
begin
MYOBJ.FREE;
MYOBJ := nil;
end;
if MYCOM <> nil then
begin
MYCOM.FREE;
MYCOM := nil;
end;
except
//
end;
end;
function OutDate(SD: string): INTEGER; stdcall;
begin
Read_busy := TRUE; //发送开始
MYCOM.WriteCommData(pchar(SD), Length(SD));
Read_busy := FALSE; //发送结束
RESULT := 1;
end;
function OpenPort(Port: shortstring; BTL: INTEGER): INTEGER; stdcall;
begin
Open_busy := TRUE;
INI_OBJ;
MYCOM.CommName := Port; //串口号
MYCOM.BaudRate := BTL; //波特率
MYCOM.ByteSize := _8; //8位
MYCOM.Parity := None; //无校验
MYCOM.StopBits := _1; //一个停止位
try
MYCOM.StartComm;
Port_active := TRUE;
RESULT := 1;
except
Port_active := FALSE;
RESULT := -2;
end;
Open_busy := FALSE;
end;
function ClosePort: INTEGER; stdcall;
begin
try
if MYCOM <> nil then MYCOM.StopComm;
Port_active := FALSE;
RESULT := 1;
except
RESULT := -1;
end;
FREE_OBJ;
end;
procedure TMYOBJ.MYComReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);
var
S1: String;
//RD: Pchar;
begin
SetLength(S1, BufferLength);
Move(Buffer^, Pchar(S1)^, BufferLength);
S_DATA := S1;
//if Pos(#13, S_DATA) > 0 then RECEIVE_FINISH := TRUE;
RECEIVE_FINISH := TRUE;
if Assigned(FCallback) then FCallback(pchar(S_DATA));
end;
procedure SetCallback(ACallback: TCallback); stdcall;
begin
FCallback := ACallback;
end;
end.