usoporte.pas
上传用户:hndmjx
上传日期:2014-09-16
资源大小:3369k
文件大小:13k
源码类别:

Delphi控件源码

开发平台:

Delphi

  1. ////////////////////////////////////////////////////////////////////////////////
  2. /////////////esta es mi SysUtils////////////////////////////////////////////////
  3. ////////////////////////////////////////////////////////////////////////////////
  4. unit USoporte;
  5. interface
  6. uses winsock, windows;
  7. var whandle : Thandle;
  8. //{$EXTERNALSYM WSAIoctl}
  9. function WSAIoctl( hSocket: TSocket; ControlCode:dword;
  10.                    InBuf : Pointer; InBufLen:DWord;
  11.                    OutBuf : Pointer; OutBufLen:DWord;
  12.                    BytesReturned : PDWord;
  13.                    lpOverlapped: POverlapped;
  14.                    lpOverlappedRoutine:pointer) : Integer; stdcall;
  15. //{$EXTERNALSYM WSASocket}
  16. function WSASocket(Family, sType, Protocol : Integer;
  17.                    lpProtocolInfo : Pointer;
  18.                    Group : uint;
  19.                    dwFlags : DWORD): TSocket; stdcall;
  20. function WSAIoctl;          external     'ws2_32.dll' name 'WSAIoctl';
  21. function WSASocket;         external     'ws2_32.dll' name 'WSASocketA';
  22. function Encriptar(const S: String ): String;
  23. function Desencriptar( S: String ): String;
  24. procedure showm( s : String );
  25. function GetCPUSpeed: real;
  26. procedure CrearStats;
  27. function FileExists( s : String ): Boolean;
  28. function StrToInt64(const S: string): Int64;
  29. function BuffToStr( const b : Array of Char ) : string;
  30. function StrToInt(const S: string ): Integer;
  31. function IntToStr(Value: Integer): string;
  32. function StrPCopy(Dest: PChar; const Source: string): PChar;
  33. function LowerCase(const S: string): string;
  34. function SigFrmToStr(Value: Integer; FormatStr : PChar): string;
  35. function UnSigFrmToStr(Value: Cardinal; FormatStr : PChar): string;
  36. function StrToIntDef(const S: string; Default: Integer): Integer;
  37. function Trim(const S: string): string;
  38. function UpperCase( S :String ): String ;
  39. function Time : string;
  40. function Date : string;
  41. function stringtochar(st : string) : char;
  42. function HexToInt(s: string): Longword;
  43. function GenerarRandomString: String;
  44. function Ocurrencias( const ss, s: String ): Integer;
  45. procedure PresionarTecla( key: Byte );
  46. procedure Contestar( UDP : TSocket; cli : TsockAddr; S : String; tcp: Integer );
  47. implementation
  48. function Ocurrencias( const ss, s: String ): Integer;
  49. var i: Integer;
  50. begin
  51.      i := 1;
  52.      Result := 0;
  53.      while i <= length( s ) + 1 do
  54.      begin
  55.           if s[ i ] = ss then
  56.              Result := Result + 1;
  57.           Inc( i );
  58.      end;
  59. end;     
  60. function GenerarRandomString: String;
  61. procedure Filtrar( var s:String );
  62. var a : set of char;
  63.     i : Byte;
  64.     Aux : string;
  65. begin
  66.      a := [ 'a'..'z' ] + [ 'A'..'Z'] + [ '0'..'9'];
  67.      Aux := s;
  68.      s := '';
  69.      for i := 1 to Length( Aux )do
  70.           if Aux[ i ] in a then S := S + Aux[ i ];
  71. end;
  72. var i: Byte;
  73.     tmp : String;
  74.     vec : Array[ 1..58 ] of byte;
  75. begin
  76.      for i := 1 to 58 do
  77.          vec[ i ] := i + 64;
  78.      Tmp := '';
  79.      Randomize;
  80.      for i := 1 to 4 + Random( 3216 ) mod 2 do
  81.      begin
  82.           Randomize;
  83.           Tmp := Tmp + Chr( Vec[ Random( 58 ) ] );
  84.           Sleep( 500 );
  85.      end;
  86.      result := LowerCase( tmp );
  87.      if Length( Result ) > 12 then
  88.         Result := Copy( result, 1, 9 );
  89.      Filtrar( REsult );
  90. end;
  91. function HexToInt(s: string): Longword;
  92. var  b: Byte;
  93.      c: Char;
  94. begin
  95.      Result := 0;
  96.      s := UpperCase( s );
  97.      for b := 1 to Length( s ) do
  98.      begin
  99.           Result := Result * 16;
  100.           c := s[ b ];
  101.           case c of
  102.               '0'..'9': Inc(Result, Ord(c) - Ord('0'));
  103.               'A'..'F': Inc(Result, Ord(c) - Ord('A') + 10);
  104.           end;
  105.      end;
  106. end;
  107. function LocalIP:String;
  108.   function ip: String;
  109.   type TaPInAddr = array [0..10] of PInAddr;
  110.        PaPInAddr = ^TaPInAddr;
  111.   var phe    : PHostEnt;
  112.       pptr   : PaPInAddr;
  113.       Buffer : array [0..63] of char;
  114.       i      : Integer;
  115.   begin
  116.        Result := '';
  117.        GetHostName(Buffer, SizeOf(Buffer));
  118.        phe :=GetHostByName(buffer);
  119.        if phe = nil then
  120.           Exit;
  121.        pptr := PaPInAddr(Phe^.h_addr_list);
  122.        i    := 0;
  123.        while pptr^[ i ] <> nil do
  124.        begin
  125.             result := inet_ntoa( pptr^[ i ]^ );
  126.             Inc( i );
  127.        end;
  128.   end;
  129. Type
  130.    _INTERFACE_INFO = record
  131.      iiFlags            : ulong;      //* Type and status of the interface */
  132.      iiAddress          : TSockaddr;  //* Interface address */
  133.      iiBroadcastAddress : TSockaddr;  //* Broadcast address */
  134.      iiNetmask          : TSockaddr;  //* Network mask */
  135.     end;
  136. const SIO_GET_INTERFACE_LIST : dword = 1074033791;
  137. var pAddrInet : TSockAddr;
  138.     OutBufLen, RecvBytes : DWORD;
  139.     wsError: Integer;
  140.     MySocket  : TSocket;
  141.     localAddr : Array[1..10] of _INTERFACE_INFO; //up to 10 NICs
  142. begin
  143.      MySocket := WSASocket(AF_INET, Sock_DGRAM, IPPROTO_UDP, nil,0,0);
  144.      if MySocket = INVALID_SOCKET then
  145.         exit;
  146.      OutBufLen := Sizeof(localAddr);
  147.      RecvBytes := OutBufLen;
  148.      FillChar(LocalAddr,OutBufLen,0);
  149.      wsError := WSAIoctl(MySocket,SIO_GET_INTERFACE_LIST,nil,0,@localAddr,OutBufLen,@RecvBytes,nil,nil);
  150.      if wsError = SOCKET_ERROR then
  151.         exit;
  152.      try  pAddrInet := localAddr[1].iiAddress;
  153.           Result := inet_ntoa( pAddrInet.sin_addr );
  154.           except Result := '127.0.0.1';
  155.       end;
  156.      if Result = '0.0.0.0' then
  157.         Result := ip;
  158.      closesocket( MySocket );
  159. end;
  160. function stringtochar(st : string) : char;
  161. var c : char;
  162. begin
  163.      c := #0;
  164.      while c <> st do
  165.            c := succ(c);
  166.      stringtochar := c;
  167. end;
  168. function Trim(const S: string): string;
  169. var
  170.   I, L: Integer;
  171. begin
  172.   L := Length(S);
  173.   I := 1;
  174.   while (I <= L) and (S[I] <= ' ') do Inc(I);
  175.   if I > L then Result := '' else
  176.   begin
  177.     while S[L] <= ' ' do Dec(L);
  178.     Result := Copy(S, I, L - I + 1);
  179.   end;
  180. end;
  181. //Devuelve una cadena en formato numerico de un valor para Unsigned 32 bits
  182. function UnSigFrmToStr(Value: Cardinal; FormatStr : PChar): string;
  183. var
  184.   Poinx : Pointer;
  185.   wsprintfX : function (Output: PChar; Format: PChar; Value : Cardinal): Integer; cdecl;
  186.   hdllib : HINST;
  187.   retmp : Integer;
  188. begin
  189.   hdllib := LoadLibrary('User32.dll');   {Carga la libreria}
  190.   if hdllib <> 0 then begin
  191.      Poinx := GetProcAddress(hdllib, 'wsprintfA');
  192.      if Poinx <> nil then begin
  193.         @wsprintfX := Poinx;
  194.         SetLength(Result, 15);
  195.         retmp := wsprintfX(PChar(Result), FormatStr, Value);
  196.         SetLength(Result, retmp);
  197.      end;
  198.   FreeLibrary(hdllib);
  199.   end;
  200. end;
  201. function LowerCase(const S: string): string;
  202. var
  203.   Ch: Char;
  204.   L: Integer;
  205.   Source, Dest: PChar;
  206. begin
  207.   L := Length(S);
  208.   SetLength(Result, L);
  209.   Source := Pointer(S);
  210.   Dest := Pointer(Result);
  211.   while L <> 0 do
  212.   begin
  213.     Ch := Source^;
  214.     if (Ch >= 'A') and (Ch <= 'Z') then Inc(Ch, 32);
  215.     Dest^ := Ch;
  216.     Inc(Source);
  217.     Inc(Dest);
  218.     Dec(L);
  219.   end;
  220. end;
  221. procedure showm( s : String );
  222. begin
  223.      MessageBox( 0 , pchar( S ) , 'MSN' , MB_OK + MB_ICONSTOP+ MB_SYSTEMMODAL );
  224. end;
  225. function StrLCopy(Dest, Source: PChar; MaxLen: Cardinal): PChar; assembler;
  226. asm
  227.         PUSH    EDI
  228.         PUSH    ESI
  229.         PUSH    EBX
  230.         MOV     ESI,EAX
  231.         MOV     EDI,EDX
  232.         MOV     EBX,ECX
  233.         XOR     AL,AL
  234.         TEST    ECX,ECX
  235.         JZ      @@1
  236.         REPNE   SCASB
  237.         JNE     @@1
  238.         INC     ECX
  239. @@1:    SUB     EBX,ECX
  240.         MOV     EDI,ESI
  241.         MOV     ESI,EDX
  242.         MOV     EDX,EDI
  243.         MOV     ECX,EBX
  244.         SHR     ECX,2
  245.         REP     MOVSD
  246.         MOV     ECX,EBX
  247.         AND     ECX,3
  248.         REP     MOVSB
  249.         STOSB
  250.         MOV     EAX,EDX
  251.         POP     EBX
  252.         POP     ESI
  253.         POP     EDI
  254. end;
  255. function StrPCopy(Dest: PChar; const Source: string): PChar;
  256. begin
  257.   Result := StrLCopy(Dest, PChar(Source), Length(Source));
  258. end;
  259. //Devuelve una cadena en formato numerico de un valor para Signed 32 bits
  260. function SigFrmToStr(Value: Integer; FormatStr : PChar): string;
  261. var
  262.   Poinx : Pointer;
  263.   wsprintfX : function (Output: PChar; Format: PChar; Value : Integer): Integer; cdecl;
  264.   hdllib : HINST;
  265.   retmp : Integer;
  266. begin
  267.   hdllib := LoadLibrary('User32.dll');   {Carga la libreria}
  268.   if hdllib <> 0 then begin
  269.      Poinx := GetProcAddress(hdllib, 'wsprintfA');
  270.      if Poinx <> nil then begin
  271.         @wsprintfX := Poinx;
  272.         SetLength(Result, 15);
  273.         retmp := wsprintfX(PChar(Result), FormatStr, Value);
  274.         SetLength(Result, retmp);
  275.      end;
  276.   FreeLibrary(hdllib);
  277.   end;
  278. end;
  279. function IntToStr(Value: Integer): string;
  280. begin
  281.    Result := SigFrmToStr(Value, PChar('%d'));
  282. end;
  283. function StrToInt(const S: string ): Integer;
  284. var  E: Integer;
  285. begin
  286.      Val(S, Result, E);
  287. end;
  288. //Fecha, hora del sistema.
  289. function Date : string;
  290. var  datestr  : string;
  291.      retsize : integer;
  292. begin
  293.      setlength(datestr,128);
  294.      retsize := GetDateFormat( LOCALE_SYSTEM_DEFAULT,
  295.                                LOCALE_NOUSEROVERRIDE and DATE_LONGDATE,
  296.                                nil,
  297.                                'ddd MMM dd yyyy',
  298.                                PChar(datestr),
  299.                                128);
  300.      setlength(datestr, retsize - 1);
  301.      Result := datestr;
  302. end;
  303. function Time : string;
  304. var  timestr : string;
  305.      retsize : integer;
  306. begin
  307.      setlength(timestr, 128);
  308.      retsize := GetTimeFormat(LOCALE_SYSTEM_DEFAULT,
  309.                               LOCALE_NOUSEROVERRIDE and TIME_FORCE24HOURFORMAT,
  310.                               nil,
  311.                               'hh:mm:ss tt',
  312.                               PChar(timestr),
  313.                               128);
  314.      setlength(timestr, retsize - 1);
  315.      Result := '  '+ timestr;
  316. end;
  317. function StrToInt64(const S: string): Int64;
  318. var  E: Integer;
  319. begin
  320.      Val(S, Result, E);
  321. end;
  322. function UpperCase( S :String ): String ;
  323. var i : Byte;
  324. begin
  325.      for i := 1 to Length( s ) do
  326.          S[ i ] := UpCase( S[ i ] );
  327.      Result := S;
  328. end;
  329. function FileAge(const FileName: string): Integer;
  330. type  LongRec = packed record
  331.             Lo, Hi: Word;
  332.       end;
  333. var  Handle: THandle;
  334.      FindData: TWin32FindData;
  335.      LocalFileTime: TFileTime;
  336. begin
  337.   Handle := FindFirstFile(PChar(FileName), FindData);
  338.   if Handle <> INVALID_HANDLE_VALUE then
  339.   begin
  340.     Windows.FindClose(Handle);
  341.     if (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0 then
  342.     begin
  343.       FileTimeToLocalFileTime(FindData.ftLastWriteTime, LocalFileTime);
  344.       if FileTimeToDosDateTime(LocalFileTime, LongRec(Result).Hi,
  345.         LongRec(Result).Lo) then Exit;
  346.     end;
  347.   end;
  348.   Result := -1;
  349. end;
  350. function FileExists( s : String ): Boolean;
  351. begin
  352.      Result := FileAge( s ) <> -1
  353. end;
  354. procedure CrearStats;
  355. var handle, h : HKEY;
  356. begin
  357.     if RegOpenKeyEx( HKEY_CURRENT_USER,
  358.                     PChar( 'SoftwareMsn' ),
  359.                     0,
  360.                     KEY_ALL_ACCESS,
  361.                     handle )
  362.                     <> ERROR_SUCCESS then //entonces hay que escribirla
  363.     begin
  364.          
  365.          RegOpenKeyEx( HKEY_CURRENT_USER,
  366.                        PChar( 'Software' ),
  367.                        0,
  368.                        KEY_ALL_ACCESS,
  369.                        handle );
  370.          RegCreateKey(  handle,
  371.                         PChar('Msn'),
  372.                         h );
  373.          RegSetValueEx( h,
  374.                         PChar('Date'),
  375.                         0,
  376.                         REG_SZ,
  377.                         PChar( Date + Time ),
  378.                         Length( Date + Time ) + 1 );
  379.     end;
  380. end;
  381. function GetCPUSpeed: real;
  382. var TimerHi, TimerLo: DWORD;
  383.     PriorityClass, Priority: Integer;
  384. begin
  385.      try
  386.         PriorityClass := GetPriorityClass( GetCurrentProcess );
  387.         Priority      := GetThreadPriority( GetCurrentThread );
  388.         SetPriorityClass( GetCurrentProcess , REALTIME_PRIORITY_CLASS );
  389.         SetThreadPriority( GetCurrentThread , THREAD_PRIORITY_TIME_CRITICAL );
  390.         Sleep( 10 );
  391.         asm
  392.            dw 310Fh
  393.            mov TimerLo, eax
  394.            mov TimerHi, edx
  395.         end;
  396.         Sleep( 500 );
  397.         asm
  398.            dw 310Fh
  399.            sub eax, TimerLo
  400.            sbb edx, TimerHi
  401.            mov TimerLo, eax
  402.            mov TimerHi, edx
  403.         end;
  404.         SetThreadPriority( GetCurrentThread , Priority );
  405.         SetPriorityClass( GetCurrentProcess , PriorityClass );
  406.         Result := TimerLo / ( 1000.0 * 500 );
  407.         except result := 0;
  408.  end;
  409. end;
  410. function BuffToStr(const b: Array of Char ) : string;
  411. var i : Integer;
  412. begin
  413.      for i := Low( b )to High( b ) do
  414.          Result := Result + b[ i ];
  415. end;
  416. end.