smsysdll32.dpr

来自「SPServer.rar一个基于TCP/IP监听发送代码」· DPR 代码 · 共 283 行

DPR
283
字号
library SMSysDll32;

{ 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
  ShareMem,
  Classes,
  Windows,
  SysUtils,
  Winsock;

{$R *.res}

const
 pHand:byte=175;
 h27 = 'SendSMS';
 h28 = 'SendBatchSMS';
 h01 = 'SecurityATT';

{发送数据结构}
type
  TSendClient = Record
  Hand:byte;
  IPAddress:array [0..29] of byte;
  User:array [0..19] of byte;
  PassWord:array [0..19] of byte;
  CommandID:array [0..14] of byte;
  DestNo: array [0..251] of byte;
  ChargeNo: array [0..21] of byte;
  SvcType: array [0..10] of byte;
  Content: array [0..254] of byte;
  SPNumber:array [0..9] of byte;
end;

//返回服务器信息
type
 TServerResult = Record
  RecvType:byte;
  Hand:byte;
  CommandID: array [0..14] of byte;
  ResultValue:Integer;
  ResultMsg:array [0..160] of byte;
 end;

//下发客户端转发接收的数据
type
 TDeliverServer = Record
  RecvType:byte;
  SrcNo:array [0..21] of byte;
  Msg:array [0..160] of byte;
 end;


var
  skt        :TSocket;
  Iden_Attes :Boolean;
  pUser      :string;
  pPassWord  :string;

function GetLocalIP(var pIPAddress:PChar):Boolean;stdcall;
type
  TaPInAddr = array [0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe  : PHostEnt;
  pptr : PaPInAddr;
  Buffer : array [0..63] of char;
  I    : Integer;
  GInitData : TWSADATA;
begin
  Result:=False;
  WSAStartup($101, GInitData);
  GetHostName(Buffer, SizeOf(Buffer));
  phe :=GetHostByName(buffer);
  if phe = nil then Exit;
  pptr := PaPInAddr(Phe^.h_addr_list);
  I := 0;
  while pptr^[I] <> nil do begin
    pIPAddress:=inet_ntoa(pptr^[I]^);
    Inc(I);
  end;
  Result:=True;
  WSACleanup;
end;

function InitSocket:Integer;stdcall;
var
 WSAData:TWSAData;
begin
 //init Socket
if (WSAStartup(MAKEWORD(2,0),WSAData)<>0) then
    Result:=-1 else    //SOCKET初始化失败
    Result:=1;         //SOCKET初始化成功
end;

function DestroySocket:Integer;stdcall;
begin
 try
  //release winsock
    WSACleanUP();
    Result:=1;      //SOCKET释放成功
    CloseSocket(skt);
  except
   on exception do
    Result:=-1;     //SOCKET释放失败
  end;
end;

function ConnectServer(IPAddress:PChar;HostPort:Integer):Integer;stdcall;
var
 Address:TSockAddr;
 Re:Integer;
begin
 //create socket
 Iden_Attes:=False;
 skt:=Socket(AF_INET,SOCK_STREAM,0);
 if(skt=INVALID_SOCKET)
  then begin
    Result:=-1;     //创建SOCKET失败
    Exit;
   end;
 ZeroMemory(@Address,SizeOf(Address));
 Address.sin_family := AF_INET;
 Address.sin_addr.s_addr :=inet_addr(IPAddress);
 Address.sin_port:=htons(HostPort);
 Re := Connect(skt,Address,SizeOf(Address));
 if(Re<>0)
   then begin
     Result:=-2;     //网络连接失败
     Exit;
    end;
  Result:=1;         //网络连接成功
end;

function SendSMS(p_CommandID,p_DestNo,p_ChargeNo,p_SvcType,
                 p_Content,p_SPNumber:PChar):Integer;stdcall;
var
 xSendClient:TSendClient;
 tmpbuf:array[0..255] of Char;
 buffer:array[0..1023] of Char;
 Re:Integer;
 pIPAddress:PChar;
begin
 if not GetLocalIP(pIPAddress)
    then begin
      Result:=-3;
      exit;
    end;
 if not Iden_Attes
   then begin
     Result:=-2;      //未经过安全认证
     Exit;
    end;
  fillchar(tmpbuf,SizeOf(tmpbuf),' ');
  fillchar(buffer,SizeOf(buffer),' ');
  fillchar(xSendClient,SizeOf(xSendClient),' ');
  xSendClient.Hand:=pHand;
  StrPCopy(tmpbuf,pIPAddress);
  Move(tmpbuf,xSendClient.IPAddress,SizeOf(xSendClient.IPAddress));
  StrPCopy(tmpbuf,pUser);
  Move(tmpbuf,xSendClient.User,SizeOf(xSendClient.User));
  StrPCopy(tmpbuf,pPassWord);
  Move(tmpbuf,xSendClient.PassWord,SizeOf(xSendClient.PassWord));
  StrPCopy(tmpbuf,p_CommandID);
  Move(tmpbuf,xSendClient.CommandID,SizeOf(xSendClient.CommandID));
  StrPCopy(tmpbuf,p_DestNo);
  Move(tmpbuf,xSendClient.DestNo,SizeOf(xSendClient.DestNo));
  StrPCopy(tmpbuf,p_ChargeNo);
  Move(tmpbuf,xSendClient.ChargeNo,SizeOf(xSendClient.ChargeNo));
  StrPCopy(tmpbuf,p_SvcType);
  Move(tmpbuf,xSendClient.SvcType,SizeOf(xSendClient.SvcType));
  StrPCopy(tmpbuf,p_Content);
  Move(tmpbuf,xSendClient.Content,SizeOf(xSendClient.Content));
  StrPCopy(tmpbuf,p_SPNumber);
  Move(tmpbuf,xSendClient.SPNumber,SizeOf(xSendClient.SPNumber));
  Move(xSendClient,buffer,SizeOf(xSendClient));
  Re:=Send(skt,buffer,SizeOf(buffer),0);
  if(Re=SOCKET_ERROR)
   then begin
     Result:=-1;   //信息发送失败
     Exit;
   end;
   Result:=1;
end;

function IdenAttes(p_User,p_PassWord,p_CommandID:PChar):Integer;stdcall;
var
 xSendClient:TSendClient;
 tmpbuf:array[0..255] of Char;
 buffer:array[0..1023] of Char;
 Re:Integer;
 pIPAddress:PChar;
begin
 if not GetLocalIP(pIPAddress)
    then begin
      Result:=-3;
      exit;
    end;
  pUser:=p_User;
  pPassWord:=p_PassWord;
  fillchar(tmpbuf,SizeOf(tmpbuf),' ');
  fillchar(buffer,SizeOf(buffer),' ');
  fillchar(xSendClient,SizeOf(xSendClient),' ');
  xSendClient.Hand:=pHand;
  StrPCopy(tmpbuf,pIPAddress);
  Move(tmpbuf,xSendClient.IPAddress,SizeOf(xSendClient.IPAddress));
  StrPCopy(tmpbuf,p_User);
  Move(tmpbuf,xSendClient.User,SizeOf(xSendClient.User));
  StrPCopy(tmpbuf,p_PassWord);
  Move(tmpbuf,xSendClient.PassWord,SizeOf(xSendClient.PassWord));
  StrPCopy(tmpbuf,p_CommandID);
  Move(tmpbuf,xSendClient.CommandID,SizeOf(xSendClient.CommandID));
  StrPCopy(tmpbuf,'');
  Move(tmpbuf,xSendClient.DestNo,SizeOf(xSendClient.DestNo));
  StrPCopy(tmpbuf,'');
  Move(tmpbuf,xSendClient.ChargeNo,SizeOf(xSendClient.ChargeNo));
  StrPCopy(tmpbuf,'');
  Move(tmpbuf,xSendClient.SvcType,SizeOf(xSendClient.SvcType));
  StrPCopy(tmpbuf,'');
  Move(tmpbuf,xSendClient.Content,SizeOf(xSendClient.Content));
  StrPCopy(tmpbuf,'');
  Move(tmpbuf,xSendClient.SPNumber,SizeOf(xSendClient.SPNumber));
  Move(xSendClient,buffer,SizeOf(xSendClient));
//  send message
  Re:=Send(skt,buffer,SizeOf(buffer),0);
  if(Re=SOCKET_ERROR)
   then begin
     Result:=-1;   //信息发送失败
     Exit;
   end;
   Result:=1;
end;

function RecvMsg(var buffer:array of Char):Integer;stdcall;
var
 bufrec:array[0..1023] of Char;
 tmpbuf:array[0..255] of Char;
 Re:Integer;
 xServerResult:TServerResult;
begin
// try
//   fillchar(bufrec,SizeOf(bufrec),' ');
//   fillchar(tmpbuf,SizeOf(tmpbuf),' ');
//   fillchar(xResultServer,SizeOf(xResultServer),' ');
   Re := Recv(skt,bufrec,SizeOf(bufrec), 0);
   Move(bufrec,xServerResult,SizeOf(xServerResult));
//   Move(xResultServer.CommandID,tmpbuf,SizeOf(xResultServer.CommandID));
///   if tmpbuf=h01 then
//    begin
//     if xServerResult.Result=1 then
//      Iden_Attes:=True else
//       begin
//        Iden_Attes:=False;
//        Result:=-2;
//        Exit;
//       end;
//     end;
   Move(bufrec,buffer,SizeOf(bufrec));
   Result:=Re;
//  except
//   on exception do
//    Result:=-1;
//  end;
end;

exports
 InitSocket,
 DestroySocket,
 SendSMS,
 RecvMsg,
 IdenAttes,
 ConnectServer;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?