Best way to sort an array

You can add pointers to the elements of the array to a TList, then call TList.Sort with a comparison function, and finally create a new array and copy the values out of the TList in the desired order. However, if you’re using the next version, D2009, there is a new collections library which can sort … Read more

How to open an URL with the default browser with FireMonkey cross-platform applications?

Regarding the answer of mjn, I have written the following unit. I have successfully tested it on Windows but I don’t have an OSX to test it on this platform. If someone can confirm it works, I’d appreciate. unit fOpen; interface uses {$IFDEF MSWINDOWS} Winapi.ShellAPI, Winapi.Windows; {$ENDIF MSWINDOWS} {$IFDEF POSIX} Posix.Stdlib; {$ENDIF POSIX} type TMisc … Read more

Is there, or is there ever going to be, a conditional operator in Delphi?

Such an operator isn’t part of the current Delphi version because it wasn’t part of the previous version, and demand wasn’t great enough to justify the cost of adding it. (You’ll find that explanation applies to lots of features you wish you had in lots of products.) Delphi provides a set of IfThen functions in … Read more

How to detect inactive user

To track a user’s idle time you could hook keyboard and mouse activity. Note, however, that installing a system-wide message hook is a very invasive thing to do and should be avoided if possible, since it will require your hook DLL to be loaded into all processes. Another solution is to use the GetLastInputInfo API … Read more

How to access private methods without helpers?

If there is extended RTTI info generated for the class private members – fields and/or methods you can use it to gain access to them. Of course, accessing through RTTI is way slower than it was through class helpers. Accessing methods: var Base: TBase2; Method: TRttiMethod; Method := TRttiContext.Create.GetType(TBase2).GetMethod(‘UsefullButHidden’); Method.Invoke(Base, []); Accessing variables: var Base: … Read more

What’s the difference between public and published class members in Delphi?

The compiler generates RTTI (Run-Time Type Information) metadata for published members, but not for public members (by default). The main effect of this is that the published properties of an object will appear in the Object Inspector at design time. I do not know if you are writing components, but if you do, you probably … Read more

Converting TMemoryStream to ‘String’ in Delphi 2009

The code you have is unnecessarily complex, even for older Delphi versions. Why should fetching the string version of a stream force the stream’s memory to be reallocated, after all? function MemoryStreamToString(M: TMemoryStream): string; begin SetString(Result, PChar(M.Memory), M.Size div SizeOf(Char)); end; That works in all Delphi versions, not just Delphi 2009. It works when the … Read more

What is TMonitor in Delphi System unit good for?

TMonitor combines the notion of a critical section (or a simple mutex) along with a condition variable. You can read about what a “monitor” is here: http://en.wikipedia.org/wiki/Monitor_%28synchronization%29 Any place you would use a critical section, you can use a monitor. Instead of declaring a TCriticalSection, you can simple create a TObject instance and then use … Read more