C/C++如何调用Delphi编写的dll

badmonkey1 2010-06-17 11:04:35
我用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.
...全文
46 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
呃,所有函数前面都得加一个__stdcall
  • 打赏
  • 举报
回复
void SetCallback( void(*pf)(const char*) );


function OpenPort(Port: shortstring; BTL: INTEGER): INTEGER; stdcall;

改成
function OpenPort(Port: PChar ; BTL: INTEGER): INTEGER; stdcall;

int OpenPort(const char* Port,int BTL);

int ClosePort();

function OutDate(SD: string): INTEGER; stdcall;

改成
function OutDate(SD: PChar ): INTEGER; stdcall;

int OutDate( const char* sd );

3,881

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 其它技术问题
社区管理员
  • 其它技术问题社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧