UnitCustomMessageSender.pas 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. unit UnitCustomMessageSender;
  2. {$WARN SYMBOL_PLATFORM OFF}
  3. interface
  4. uses
  5. ComObj,SysUtils,
  6. ActiveX,
  7. AxCtrls,
  8. Classes,
  9. RealICQCOMInterfaces_TLB,
  10. ASPTypeLibrary_TLB,
  11. StdVcl,
  12. Winsock2,
  13. RealICQSocket,
  14. Registry,
  15. Windows;
  16. type
  17. TCustomMessageSender = class(TAutoObject, IConnectionPointContainer, ICustomMessageSender)
  18. private
  19. { Private declarations }
  20. FConnectionPoints: TConnectionPoints;
  21. FConnectionPoint: TConnectionPoint;
  22. FEvents: ICustomMessageSenderEvents;
  23. { note: FEvents maintains a *single* event sink. For access to more
  24. than one event sink, use FConnectionPoint.SinkList, and iterate
  25. through the list of sinks. }
  26. m_scriptContext: IScriptingContext;
  27. FResult: Integer;
  28. public
  29. procedure Initialize; override;
  30. protected
  31. { Protected declarations }
  32. property ConnectionPoints: TConnectionPoints read FConnectionPoints
  33. implements IConnectionPointContainer;
  34. procedure EventSinkChanged(const EventSink: IUnknown); override;
  35. procedure OnStartPage(const unk: IUnknown); safecall;
  36. procedure OnEndPage; safecall;
  37. procedure Send(ServerAddress: OleVariant; ServerPort: SYSINT; LoginName,
  38. Content: OleVariant; Flag: SYSINT); safecall;
  39. procedure OpenUMC(LoginName: OleVariant); safecall;
  40. function Get_Result: SYSINT; safecall;
  41. end;
  42. const
  43. AppKey = '\Software\Winsoft\LxTalk';
  44. CompanyKeyValue = '\Winsoft'; //做OEM版时,此处应该改为对应公司的网址或名称
  45. AppTitle = '办公助手';
  46. iAtom: PChar = 'Winsoft';
  47. implementation
  48. uses Variants, ComServ,ComConst, ShellAPI, LoggerManager;
  49. var logger: TLogger4Delphi;
  50. //------------------------------------------------------------------------------
  51. procedure TCustomMessageSender.OpenUMC(LoginName: OleVariant);
  52. var
  53. ExeFileName: String;
  54. FVar: OleVariant;
  55. Registry: TRegistry;
  56. Handle: HWND;
  57. begin
  58. ExeFileName := '';
  59. {从注册表中读取 BaseURL}
  60. Registry := TRegistry.Create;
  61. try
  62. Registry.RootKey := HKEY_LOCAL_MACHINE;
  63. if Registry.OpenKey(AppKey + CompanyKeyValue, True) then
  64. begin
  65. ExeFileName := Registry.ReadString('ExeFileName');
  66. end;
  67. finally
  68. Registry.Free;
  69. end;
  70. Handle := openmutex(mutex_all_access, False, iAtom);
  71. try
  72. if Handle = 0 then
  73. begin
  74. // if MessageBox(0, PChar(AppTitle + ' 还未启动,是否启动?'), '提示', MB_ICONQUESTION or MB_YESNO) = ID_YES then
  75. // begin
  76. ShellExecute(0, 'open', PChar('"' + ExeFileName + '"'), nil, nil, SW_SHOWNORMAL);
  77. // end;
  78. // Exit;
  79. end;
  80. finally
  81. closeHandle(Handle);
  82. end;
  83. end;
  84. //------------------------------------------------------------------------------
  85. procedure TCustomMessageSender.EventSinkChanged(const EventSink: IUnknown);
  86. begin
  87. FEvents := EventSink as ICustomMessageSenderEvents;
  88. end;
  89. //------------------------------------------------------------------------------
  90. procedure TCustomMessageSender.Initialize;
  91. begin
  92. inherited Initialize;
  93. FConnectionPoints := TConnectionPoints.Create(Self);
  94. if AutoFactory.EventTypeInfo <> nil then
  95. FConnectionPoint := FConnectionPoints.CreateConnectionPoint(
  96. AutoFactory.EventIID, ckSingle, EventConnect)
  97. else FConnectionPoint := nil;
  98. end;
  99. //------------------------------------------------------------------------------
  100. procedure TCustomMessageSender.OnStartPage(const unk: IUnknown);
  101. begin
  102. FResult := -1;
  103. m_scriptContext := unk as IScriptingContext;
  104. end;
  105. //------------------------------------------------------------------------------
  106. procedure TCustomMessageSender.OnEndPage;
  107. begin
  108. m_scriptContext := nil;
  109. end;
  110. //------------------------------------------------------------------------------
  111. procedure TCustomMessageSender.Send(ServerAddress: OleVariant;
  112. ServerPort: SYSINT; LoginName, Content: OleVariant; Flag: SYSINT);
  113. var
  114. FServerAddress: String;
  115. FServerPort: Integer;
  116. ServerSocket: TSocket;
  117. ServerAddr: TSockAddrIn;
  118. LastError: Integer;
  119. nIndex,
  120. ReturnValue: Integer;
  121. Buf: array[0..255] of Byte;
  122. FFlag: Byte;
  123. FContent,
  124. FLoginName: String;
  125. LoginNameLength,
  126. ContentLength,
  127. BufferLength: SmallInt;
  128. SendBuffer: Array of Byte;
  129. begin
  130. logger.Info('start calling');
  131. FServerAddress := ServerAddress;
  132. FServerPort := ServerPort;
  133. FLoginName := LoginName;
  134. FContent := Content;
  135. FFlag := Flag;
  136. logger.Info('ServerAddress:' + FServerAddress + ' Port:' + IntToStr(FServerPort) + 'LoginName:'+FLoginName);
  137. logger.Info('Content:'+ FContent);
  138. LoginNameLength := Length(FLoginName);
  139. ContentLength := Length(FContent);
  140. logger.Info('Creating Socket');
  141. ServerSocket := Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  142. if ServerSocket = INVALID_SOCKET then logger.Error(Format('创建套接字失败,错误代码:%d',[WSAGetLastError]));
  143. ServerAddr.sin_family:= AF_INET;
  144. ServerAddr.sin_port:= htons(FServerPort);
  145. ServerAddr.sin_addr.S_addr:= inet_addr(PChar(FServerAddress));
  146. logger.Info('Connect to Server');
  147. connect(ServerSocket, @ServerAddr, SizeOf(ServerAddr));
  148. LastError := WSAGetLastError();
  149. if LastError <> 0 then
  150. begin
  151. closesocket(ServerSocket);
  152. logger.Error(Format('无法建立与服务器的连接,错误代码:%d', [LastError]));
  153. end;
  154. BufferLength := 7 + LoginNameLength + ContentLength;
  155. logger.Info('Packing Data');
  156. SetLength(SendBuffer, BufferLength);
  157. nIndex := 0;
  158. //填充 (1)协议类型(0xFE) 1byte
  159. SendBuffer[nIndex] := $FE;
  160. Inc(nIndex, 1);
  161. //填充 (2)消息总长度 2byte
  162. CopyMemory(@SendBuffer[nIndex], @BufferLength, 2);
  163. Inc(nIndex, 2);
  164. //填充 (3)接收人用户名长度 1byte
  165. SendBuffer[nIndex] := Byte(LoginNameLength);
  166. Inc(nIndex, 1);
  167. //填充 (4)接收人用户名 动态长度
  168. CopyMemory(@SendBuffer[nIndex], PChar(FLoginName), LoginNameLength);
  169. Inc(nIndex, LoginNameLength);
  170. //填充 (5)消息发送方式 1byte
  171. SendBuffer[nIndex] := FFlag;
  172. Inc(nIndex, 1);
  173. //填充 (6)消息内容长度 2byte
  174. CopyMemory(@SendBuffer[nIndex], @ContentLength, 2);
  175. Inc(nIndex, 2);
  176. //填充 (7)消息内容 动态长度
  177. CopyMemory(@SendBuffer[nIndex], PChar(FContent), ContentLength);
  178. //Inc(nIndex, ContentLength);
  179. logger.Info('Sending Data');
  180. try
  181. ReturnValue := Winsock2.Send(ServerSocket, SendBuffer[0], BufferLength, 0);
  182. if ReturnValue <= 0 then
  183. begin
  184. FResult :=$FF;
  185. closesocket(ServerSocket);
  186. logger.Error('往服务器发送数据失败');
  187. end;
  188. // FillChar(Buf, 256, #0);
  189. // ReturnValue := Recv(ServerSocket, Buf, 1, 0);
  190. // if ReturnValue <> 1 then
  191. // begin
  192. // closesocket(ServerSocket);
  193. // logger.Error('服务器上返回了错误的数据');
  194. // end;
  195. closesocket(ServerSocket);
  196. //FResult := Buf[0];
  197. //if FResult = $FF then logger.Error('数据发送失败');
  198. except
  199. on E: Exception do
  200. logger.Info('异常类名称:' + E.ClassName
  201. + #13#10 + '异常信息:' + E.Message);
  202. end;
  203. logger.Info('OK');
  204. end;
  205. //------------------------------------------------------------------------------
  206. function TCustomMessageSender.Get_Result: SYSINT;
  207. begin
  208. Result := FResult;
  209. end;
  210. //------------------------------------------------------------------------------
  211. initialization
  212. TAutoObjectFactory.Create(ComServer, TCustomMessageSender, Class_CustomMessageSender,
  213. ciMultiInstance, tmApartment);
  214. logger := TLogger4Delphi.Create;
  215. finalization
  216. logger.Free;
  217. end.