The Road to Delphi

Delphi – Free Pascal – Oxygene


9 Comments

Checking if a TCP port is Open using Delphi and Winsocks

Many times we need to know if a TCP port is open or not, here I leave a function to perform this task using winsock.

Code tested in Delphi 7, 2007 and 2010.


uses
  Winsock;

function PortTCP_IsOpen(dwPort : Word; ipAddressStr:AnsiString) : boolean;
var
  client : sockaddr_in;
  sock   : Integer;

  ret    : Integer;
  wsdata : WSAData;
begin
 Result:=False;
 ret := WSAStartup($0002, wsdata); //initiates use of the Winsock DLL
  if ret<>0 then exit;
  try
    client.sin_family      := AF_INET;  //Set the protocol to use , in this case (IPv4)
    client.sin_port        := htons(dwPort); //convert to TCP/IP network byte order (big-endian)
    client.sin_addr.s_addr := inet_addr(PAnsiChar(ipAddressStr));  //convert to IN_ADDR  structure
    sock  :=socket(AF_INET, SOCK_STREAM, 0);    //creates a socket
    Result:=connect(sock,client,SizeOf(client))=0;  //establishes a connection to a specified socket
  finally
  WSACleanup;
  end;
end;

see this sample of usage

begin
 if PortTCP_IsOpen(3306,'127.0.0.1') then 
  DoMyStuff();
end;


Leave a comment

Delphi compiler magic functions and procedures

If you look in System.pas, you won’t find a High() or Low()  function that is declared anything like what you would expect.  These functions and procedures are called compiler “magic” functions.

This list enumerate the intrinsic routines exposed by the System unit. These intrinsic routines are actually handled by the compiler rather then the run-time library.

Original list http://docwiki.embarcadero.com/RADStudio/en/Delphi_Intrinsic_Routines

System.Abs Returns an absolute value.
System.Addr Returns a pointer to a specified object.
System.Append Prepares an existing file for adding text to the end.
System.Assert Tests whether a Boolean expression is true.
System.Assigned Tests for a nil (unassigned) pointer or procedural variable.
System.Assign Associates the name of an external file with a file variable.
System.AssignFile Associates the name of an external file with a file variable.
System.BlockRead Reads one or more records from an open file into a variable.
System.BlockWrite Writes one or more records from a variable to an open file.
System.Break Causes the flow of control to exit a for, while, or repeat statement.
System.Chr Returns the character for a specified ASCII value.
System.Close Terminates the association between a file variable and an external file.
System.CloseFile Terminates the association between file variable and an external disk file.
System.Concat Concatenates two or more strings into one.
System.Continue Allows the flow of control to proceed to the next iteration of for, while, or repeat statements.
System.Copy Returns a substring of a string or a segment of a dynamic array.
System.Dec Decrements a variable by 1 or N.
System.Default Returns the default value for generic type.
System.Delete Removes a substring from a string.
System.Dispose Releases memory allocated for a dynamic variable.
System.Eof Tests whether the file position is at the end of a file.
System.Eoln Tests whether the file pointer is at the end of a line.
System.Erase Deletes an external file.
System.Exclude Removes an element from a Delphi set.
System.Exit Exits from the current procedure.
System.FilePos Returns the current file position.
System.FileSize Returns the number of records in a file.
System.FillChar Fills contiguous bytes with a specified value.
System.Finalize Uninitializes a dynamically allocated variable.
System.Flush Empties the buffer of a text file opened for output.
System.FreeMem FreeMem frees a memory block.
System.GetMem GetMem allocates a memory block.
System.Halt Initiates abnormal termination of a program.
System.Hi Returns the high-order byte of X as an unsigned value.
System.High Returns the highest value in the range of an argument.
System.Inc Increments an ordinal value by one or N.
System.Include Adds an element to a Delphi set.
System.Initialize Initializes a dynamically allocated variable.
System.Insert Inserts a substring into a string beginning at a specified point.
System.Length Returns the number of characters in a string or elements in an array.
System.Lo Returns the low order Byte of argument X.
System.Low Returns the lowest value in a range.
System.New Creates a new dynamic variable and sets P to point to it.
System.Odd Returns true if argument is an odd number.
System.Pi Returns 3.1415926535897932385.
System.Pred Returns the predecessor of the argument.
System.Ptr Converts a specified address to a pointer.
System.Read Read reads data from a file.
System.ReadLn Reads a line of text from a file.
System.ReallocMem ReallocMem reallocates a memory block.
System.Rename Changes the name of an external file.
System.Reset Opens an existing file.
System.Rewrite Creates a new file and opens it.
System.Round Returns the value of X rounded to the nearest whole number.
System.RunError Stops execution and generates a runtime error.
System.Seek Moves the current position of a file to a specified component.
System.SeekEof Returns the end-of-file status of a file, ignoring whitespace.
System.SeekEoln Returns the end-of-line status of a file, ignoring whitespace.
System.SetLength Sets the length of a string or dynamic-array variable.
System.SetString Sets the contents and length of the given string.
System.SizeOf Returns the number of bytes occupied by a variable or type.
System.Slice Returns a sub-section of an array.
System.Sqr Returns the square of a number.
System.Str Formats a string and returns it to a variable.
System.Succ Returns the successor of an argument.
System.Swap Exchanges high order byte with the low order byte of an integer or word.
System.Trunc Truncates a real number to an integer.
System.TypeHandle Returns the RTTI information for a given type.
System.TypeInfo Returns the RTTI information for a given type.
System.TypeOf Deprecated routine.
System.Val Converts a string to a numeric representation.
System.VarCast Converts a variant to specified type.
System.VarCopy Copies a Variant to another Variant.
System.Write Writes to either a typed file or a text file
System.WriteLn Writes to a text file and adds an end-of-line marker.


Leave a comment

Unofficial RAD Studio 2007 Debugger Fix for Windows 7 published

An Unofficial RAD Studio 2007 Debugger Fix for Windows 7 has been published.

The primary problem being fixed is a debugger assert that usually occurs when terminating a process being debugged on a 64-bit version of Windows 7.

The text of the assert is:
Assertion failure: “(!”SetThreadContext failed”)”
This version also fixes a few minor problems when debugging on Windows 7.

you can download this patch from this site


Leave a comment

JDBC drivers for Blackfish

In StackOverflow someone ask Where can I find the JDBC driver for Blackfish?

The JDBC Drivers are located in the Blackfish SQL lib installation directory. You can download and install The Blackfish SQL 8 for Java Trial (Windows Version) wich includes the JDBC Drivers from here.

check theses files

  • jds.jar Local JDBC database connectivity
  • jdsremote.jar Remote JDBC thin client database connectivity

You can read theses articles for more info


Leave a comment

Delphi 2009, C++Builder 2009 and RAD Studio 2009 hotfix is now available

This hotfix addresses issues with debugging on Windows 7. you can found more info in this site.

This hotfix addresses issues with debugging on Windows 7, including:

  • Assertion failure: “(!SetThreadContext failed”)”
  • On Windows 7, running under the debugger can cause the debugger to crash on shutdown.
  • No Wait Chain is shown on Windows 7

Internal Tracking Number(s): QC 74732, QC 78575, 271589


Leave a comment

Review of Delphi 2010 in TechRepublic

You can read a nice review of Delphi 2010 in TechRepublic.

Summary

If you want to do Windows development, and you want to work with native code in a native style, I think that Delphi 2010 is a very productive tool. It combines a lot that really saves time (such as DataSnap and IDE Insight) with the power and flexibility of native code. If you need support for certain functionality (especially touch-related items), it is the only shipping product on the market that does these things.

Every time I work with the Embarcadero team, I feel like they are writing the tools that they would love to use; they really are “developers’ developers,” and they understand the development process very well and implement it as tools quite nicely.

If you don’t mind learning a new system, a new language, and a new way of doing things, Delphi 2010 is worth a long look for .NET and Visual C++ developers.

Delphi 2010


1 Comment

Whole Word Search Function in Delphi

To search a whole word in a string, you can use the SearchBuf function declarated in the StrUtils.pas unit .

	function SearchBuf(Buf: PAnsiChar; BufLen: Integer; SelStart: Integer; SelLength: Integer; SearchString: AnsiString; Options: TStringSearchOptions): PAnsiChar; overload;

Buf is the text buffer to search.
BufLen is the length, in chars, of Buf.
SelStart is the first character of the search when Options indicates a backward search (does not include soDown). The first character in Buf has position 0.
SelLength is the number of characters after SelStart that the search begins when Options indicates a forward search (includes soDown).
SearchString is the string to find in Buf.
Options determines whether the search runs forward (soDown) or backward from SelStart or SelStart+SelLength, whether the search is case sensitive (soMatchCase), and whether the matching string must be a whole word (soWholeWord).

If SearchBuf finds a match, it returns a pointer to the first character of the matching string in Buf. If it does not find a match, SearchBuf returns nil.

Now using this function we can construct a new  function which will return true or false if find a word in a string.

function ExistWordInString(const AString:PWideChar;const ASearchString:string;ASearchOptions: TStringSearchOptions): Boolean;
var
  Size : Integer;
begin
  Size:=StrLen(aString);
  result := SearchBuf(AString, Size, 0, 0, ASearchString, ASearchOptions)<>nil;
end;

Use it this way
Case-insensitive

  ExistWordInString('Go Delphi Go','Delphi',[soWholeWord,soDown]); //Return True
  ExistWordInString('Go Delphi, Go','Delphi',[soWholeWord,soDown]); //Return True
  ExistWordInString('Go ,Delphi, Go','Delphi',[soWholeWord,soDown]); //Return True
  ExistWordInString('Go DELPHI Go','Delphi',[soWholeWord,soDown]); //Return True

Case sensitive

  ExistWordInString('Go Delphi Go','Delphi',[soWholeWord,soDown,soMatchCase]); //Return True
  ExistWordInString('Go DELPHI Go','Delphi',[soWholeWord,soDown,soMatchCase]); //Return False
  ExistWordInString('Go DelphI Go','Delphi',[soWholeWord,soDown,soMatchCase]); //Return False


Leave a comment

Delphi 2010 Help Update 1 released

Embarcadero has released the Delphi 2010 Help Update 1. you can find more info here.

Improvements in Help Update 1

In the VCL:

  • Documentation has been completed for the StdConvs unit.
  • Documentation has been completed for the GestureMgr unit, which was new in the RTM release.
  • Documentation has been added for the following new units:
  • Documentation has been added for many of the DataSnap APIs. See the DSConnect, DSHTTP, DSProd, and DSServer units.
  • New resurfaced intrinsic ROUTINES are documented.
  • The documentation team has fixed approximately 25 bugs reported on customer forums and in direct customer feedback.

In the IDE topics:

  • The documentation team has fixed approximately 20 bugs reported in QC, RAID, or direct customer feedback.
  • The View > History command is now documented.