| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- unit USMSSender;
- {$WARN SYMBOL_PLATFORM OFF}
- interface
- uses
- ComObj, ActiveX, RealICQInterfaces_TLB, StdVcl, Winsock2, RealICQSocket, Windows;
- type
- TSMSSender = class(TAutoObject, ISMSSender, IObjectSafety)
- private
- FObjectSafetyFlags: DWORD;
- FServerAddress: String;
- FServerPort: SYSINT;
- protected
- function Get_ServerAddress: PWideChar; safecall;
- procedure Set_ServerAddress(Value: OleVariant); safecall;
- function Get_ServerPort: SYSINT; safecall;
- procedure Set_ServerPort(Value: SYSINT); safecall;
- procedure SendSMS(PhoneID, P5PID: OleVariant; SendDateTime: TDateTime;
- Content: OleVariant); safecall;
- function GetInterfaceSafetyOptions(const IID: TIID; pdwSupportedOptions,
- pdwEnabledOptions: PDWORD): HResult; stdcall;
- function SetInterfaceSafetyOptions(const IID: TIID; dwOptionSetMask,
- dwEnabledOptions: DWORD): HResult; stdcall;
- end;
- implementation
- uses ComServ;
- //------------------------------------------------------------------------------
- function TSMSSender.GetInterfaceSafetyOptions(const IID: TIID;
- pdwSupportedOptions, pdwEnabledOptions: PDWORD): HResult;
- var
- Unk: IUnknown;
- begin
- if (pdwSupportedOptions = nil) or (pdwEnabledOptions = nil) then
- begin
- Result := E_POINTER;
- Exit;
- end;
- Result := QueryInterface(IID, Unk);
- if Result = S_OK then
- begin
- pdwSupportedOptions^ := INTERFACESAFE_FOR_UNTRUSTED_CALLER or
- INTERFACESAFE_FOR_UNTRUSTED_DATA;
- pdwEnabledOptions^ := FObjectSafetyFlags and
- (INTERFACESAFE_FOR_UNTRUSTED_CALLER or INTERFACESAFE_FOR_UNTRUSTED_DATA);
- end
- else begin
- pdwSupportedOptions^ := 0;
- pdwEnabledOptions^ := 0;
- end;
- end;
- //------------------------------------------------------------------------------
- function TSMSSender.SetInterfaceSafetyOptions(const IID: TIID;
- dwOptionSetMask, dwEnabledOptions: DWORD): HResult;
- var
- Unk: IUnknown;
- begin
- Result := QueryInterface(IID, Unk);
- if Result <> S_OK then Exit;
- FObjectSafetyFlags := dwEnabledOptions and dwOptionSetMask;
- end;
- function TSMSSender.Get_ServerAddress: PWideChar;
- begin
- end;
- procedure TSMSSender.Set_ServerAddress(Value: OleVariant);
- begin
- FServerAddress := Value;
- end;
- function TSMSSender.Get_ServerPort: SYSINT;
- begin
- end;
- procedure TSMSSender.Set_ServerPort(Value: SYSINT);
- begin
- FServerPort := Value;
- end;
- procedure TSMSSender.SendSMS(PhoneID, P5PID: OleVariant;
- SendDateTime: TDateTime; Content: OleVariant);
- var
- ServerSocket: TSocket;
- ServerAddr: TSockAddrIn;
- LastError: Integer;
-
- nIndex,
- ReturnValue: Integer;
- Buf: array[0..255] of Byte;
- FPhoneID, FP5PID, FContent: String;
- PhoneIDLength,
- P5PIDLength,
- ContentLength,
- BufferLength: SmallInt;
- SendBuffer: Array of Byte;
- begin
- ServerSocket := Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if ServerSocket = INVALID_SOCKET then raise TSocketException.CreateFmt('创建套接字失败,错误代码:%d',[WSAGetLastError]);
- ServerAddr.sin_family:= AF_INET;
- ServerAddr.sin_port:= htons(FServerPort);
- ServerAddr.sin_addr.S_addr:= inet_addr(PChar(FServerAddress));
- connect(ServerSocket, @ServerAddr, SizeOf(ServerAddr));
- LastError := WSAGetLastError();
- if LastError <> 0 then
- begin
- closesocket(ServerSocket);
- raise TSocketException.CreateFmt('无法建立与服务器的连接,错误代码:%d', [LastError]);
- end;
- FPhoneID := PhoneID;
- FP5PID := P5PID;
- FContent := Content;
-
- PhoneIDLength := Length(FPhoneID);
- P5PIDLength := Length(FP5PID);
- ContentLength := Length(FContent);
- BufferLength := 14 + PhoneIDLength + P5PIDLength + ContentLength;
- SetLength(SendBuffer, BufferLength);
- nIndex := 0;
- //填充 (1)协议类型(0x98) 1byte
- SendBuffer[nIndex] := $98;
- Inc(nIndex, 1);
- //填充 (2)消息总长度 2byte
- CopyMemory(@SendBuffer[nIndex], @BufferLength, 2);
- Inc(nIndex, 2);
- //填充 (3)电话号码长度 1byte
- SendBuffer[nIndex] := Byte(PhoneIDLength);
- Inc(nIndex, 1);
- //填充 (4)电话号码 动态长度,由(4)指定
- CopyMemory(@SendBuffer[nIndex], PChar(FPhoneID), PhoneIDLength);
- Inc(nIndex, PhoneIDLength);
- //填充 (5)用户名长度 1byte
- SendBuffer[nIndex] := Byte(P5PIDLength);
- Inc(nIndex, 1);
- //填充 (6)用户名 动态长度,由(4)指定
- CopyMemory(@SendBuffer[nIndex], PChar(FP5PID), P5PIDLength);
- Inc(nIndex, P5PIDLength);
- //填充 (7)短消息长度长度 1byte
- SendBuffer[nIndex] := Byte(ContentLength);
- Inc(nIndex, 1);
- //填充 (8)短消息长度 动态长度,由(6)指定
- CopyMemory(@SendBuffer[nIndex], PChar(FContent), ContentLength);
- Inc(nIndex, ContentLength);
- //填充 (9)发送时间 8byte,64位浮点类型(double即TDateTime类型)
- CopyMemory(@SendBuffer[nIndex], @SendDateTime, 8);
- //Inc(nIndex, 8);
- ReturnValue := Send(ServerSocket, SendBuffer[0], BufferLength, 0);
- if ReturnValue <= 0 then
- begin
- closesocket(ServerSocket);
- raise TSocketException.Create('往服务器发送数据失败');
- end;
- FillChar(Buf, 256, #0);
- ReturnValue := Recv(ServerSocket, Buf, 1, 0);
- if ReturnValue <> 1 then
- begin
- closesocket(ServerSocket);
- raise TSocketException.Create('服务器上返回了错误的数据');
- end;
-
- closesocket(ServerSocket);
- if Buf[0] <> 0 then raise TSocketException.Create('数据发送失败');
- end;
- initialization
- TAutoObjectFactory.Create(ComServer, TSMSSender, Class_SMSSender,
- ciMultiInstance, tmApartment);
- end.
|