我那个是两个应用程序都对同一个串口进行操作,为了避免两个程序同时对这个串口进行操作,想创建一个对象来对这个串口进行操作,因为这两个程序是用的一个dll,所有我在动态链接库里创建对象,源码如下
unit FInterface;
interface
Type
IDemoIntef = interface
function InitMsm(): Boolean; Stdcall;
function MySend(Port: Integer; Baud:Integer; SendStr: PChar; SendNum: PChar): Boolean; stdcall;
function Receive(Port: Integer; Baud: Integer; i: Integer; StrText: Pointer; StrNum: Pointer; StrTim: Pointer): Boolean; stdcall;
function ReInitMsm(Port: Integer; Baud: Integer): Boolean;
end;
implementation
end.
unit uInterClass;
interface
uses SysUtils, FInterface, syncObjs;
Type
TDemoIntf = Class(TInterfacedObject,IDemoIntef)
public
function InitMsm(): Boolean; Stdcall;
function MySend(Port: Integer; Baud:Integer; SendStr: PChar; SendNum: PChar): Boolean; stdcall;
function Receive(Port: Integer; Baud: Integer; i: Integer; StrText: Pointer; StrNum: Pointer; StrTim: Pointer): Boolean; stdcall;
function ReInitMsm(Port: Integer; Baud: Integer): Boolean;
end;
function CreateTDemointf: IDemoIntef; stdcall;
function GetHelloInit: IDemoIntef; stdcall;
implementation
function InitModem(comport:integer;baud:integer):boolean;stdcall; external 'Mysms.dll';
function SendSms(comport:integer; baud:integer; sMessage
ointer; sSendto
ointer; bEnglishSm:boolean; bAlertSm:boolean; bSR:boolean):boolean;stdcall; external 'Mysms.dll';
function SendAtCommand(comport:integer; baud:integer; sCmd
ointer; sEcho
ointer; nLen:integer):boolean;stdcall; external 'Mysms.dll';
function ReadSms(comport: integer; baud: integer; nIndex: integer; sMessage: Pointer; sFrom: Pointer; sTime: Pointer; bDel: boolean):boolean;stdcall; external 'Mysms.dll';
var
_IDemoIntef: IDemoIntef;
FLock: TCriticalSection;
InitFlag: Boolean;
function GetHelloInit: IDemoIntef; stdcall;
begin
try
if not Assigned(FLock) then
begin
FLock := TCriticalSection.Create;
end;
Result := CreateTDemointf;
except
end;
end;
function CreateTDemointf: IDemoIntef; stdcall;
begin
if not Assigned( _IDemoIntef) then
_IDemoIntef := Tdemointf.Create;
Result := _IDemoIntef;
end;
//***************初始化短信猫******************************
Function TDemoIntf.initmsm() : Boolean; StdCall;
begin
if ReInitMsm(1, 9600) then
begin
Result := True;
end else
begin
Result := False;
end;
end;
function TDemoIntf.ReInitMsm(Port: Integer; Baud: Integer): Boolean;
var
bb: Boolean;
ass: PChar; //at指令 at
ain: array [0..255] of Char; //返回的状态
begin
if not InitFlag then
begin
ass := 'at'#13;
FLock.Enter;
try
bb:=InitModem(Port, Baud);
finally
FLock.Leave;
end;
Sleep(200);
if bb then
begin
SendAtCommand(Port, Baud, ass, @ain[0], 255);
if (ain[2] = 'O') and (ain[3] = 'K') then
begin
Result := True;
InitFlag := True;
end else
begin
Result := False;
InitFlag := False;
end;
end else
begin
Result := False;
InitFlag := False;
end;
end else
begin
Result := True;
end;
end;
//**********发送短信******************************
function TDemoIntf.MySend(Port: Integer; Baud:Integer; SendStr: PChar; SendNum: PChar): Boolean; stdcall;
var
bb: Boolean;
i: Integer;
begin
i := 0;
repeat
FLock.Enter;
try
bb := SendSms(Port, Baud, SendStr, SendNum, False, False, False);
finally
FLock.Leave;
end;
Sleep(10);
i := i + 1;
until (not bb) and (i < 3);
if bb then
begin
Result := True;
end else
begin
InitMsm;
Result := False;
end;
end;
//**********读取短信******************************
function TDemoIntf.Receive(Port: Integer; Baud: Integer; i: Integer; StrText: Pointer; StrNum: Pointer; StrTim: Pointer): Boolean; stdcall;
var
bb: Boolean;
begin
bb := ReadSms(Port, Baud, i, StrText, StrNum, StrTim, True);
if bb then
begin
Result:=True
end else
begin
Result:=False;
end;
end;
initialization
InitFlag := False;
end.
library InterTest;
{ Important note about DLL memory management: ShareMem must be the
first unit in your library's USES clause AND your project's (select
Project-View Source) USES clause if your DLL exports any procedures or
functions that pass strings as parameters or function results. This
applies to all strings passed to and from your DLL--even those that
are nested in records and classes. ShareMem is the interface unit to
the BORLNDMM.DLL shared memory manager, which must be deployed along
with your DLL. To avoid using BORLNDMM.DLL, pass string information
using PChar or ShortString parameters. }
uses
ExceptionLog,
SysUtils,
Classes,
FInterface in 'FInterface.pas',
uInterClass in 'uInterClass.pas';
{$R *.res}
exports
GetHelloInit;
begin
end.
这就是我写的动态链接库的源码,麻烦老兄给看看吧