The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,854 other subscribers

Archive for the ‘Delphi’ Category

great answer by Remy Lebeau on windows – CreateProcessAsUser doesn’t work when “change user” – on Stack Overflow part of @StackExchange

Posted by jpluimers on 2012/01/10

You might wonder why I quoted two great StackOverflow answers recently. Well, it is because I absolutely love the way that StackExchange.com and StackOverflow.com changed how to find quality answers (and questions!) on topics varying from programmers through Cooking to Chines Language Usage in a community based way.

This one is by Remy Lebeau, who is part of TeamB:

You don’t need to enumerate running explorer.exe processes, you can use WTSGetActiveConsoleSessionId() instead, and then pass that SessionId to WTSQueryUserToken(). Note that WTSQueryUserToken() returns an impersonation token but CreateProcessAsUser() needs a primary token, so use DuplicateTokenEx() for that conversion.

You should also use CreateEnvironmentBlock() so the spawned process has a proper environment that is suited to the user account that is being used.

Lastly, set the STARTUPINFO.lpDesktop field to ‘WinSta0\Default’ instead of nil so the spawned UI can be made visible correctly.

I have been using this approach for several years now and have not had any problems with it. For example:

… code sample is in the answer …

–jeroen

via: windows – CreateProcessAsUser doesn’t work when “change user” – Stack Overflow.

Posted in Delphi, Development, Software Development | 2 Comments »

Great answer by Cosmin Prund: How and when are variables referenced in Delphi’s anonymous methods captured? – Stack Overflow

Posted by jpluimers on 2012/01/05

Every once in a while, by accident you stumble on a really great answer on StackOverflow.

Here is a quote from Cosmin Prund describing on how Delphi implements anonymous methods using a TInterfacedObject descendant:

When you have a function like the one in the question, where you have an anonymous method accessing a local variable, Delphi appears to create one TInterfacedObject descendant that captures all the stack based variables as it’s own public variables. Using Barry’s trick to get to the implementing TObject and a bit of RTTI we can see this whole thing in action.

Read his full answer for the complete description including sample code.

I stumbled on this great answer trough the question Is it possible for a managed local variable to transparently “travel to” another local scope? which might sound like an odd question, but it is not: StackOverflow is about learning, and some people do that by asking questions on solving problems in a very uncommon way, just to learn there are far better ways of obtaining what they want.

–jeroen

via: How and when are variables referenced in Delphi’s anonymous methods captured? – Stack Overflow.

Posted in Delphi, Development, Software Development | Leave a Comment »

Many more web platforms vulnerable to the hash collision attack (not only ASP.NET) #28C3 @hashDoS #hashDoS @ccc

Posted by jpluimers on 2011/12/29

When writing my Patch your ASP.NET servers ASAP early this morning, I didn’t have time to research the full extend of the vulnerabilities published at 28C3 (slides, mp4), though a small bell was ringing a message that I had seen something like it before earlier this century.

I was right, this posting on perlmonks direct me to a /. posting in 2003 pointing me to the research paper on low-bandwidth attacks based on hash collisions (pdf version) that I had seen before. Perl 5.8.1 fixed it September 2003 (search for “hash” in that link).

The attack can be used for DoS because a normal distributed hash table insert of n elements will be running O(n), but a carefully crafted insert of those elements will run O(n^2).

Carefully crafting a worst case scenario depends on how well you can predict collisions in the underlying hash table implementation, which – apparently – is not too difficult, and requires little bandwidth.

Many platforms and languages are vulnerable (already archived at the WayBack machine), including those based on Java, Tomcat, .NET, Ruby, PHP and more in greater or lesser extent. I have the impression that the list only includes big names, but presume platforms based on smaller names (ASP, Delphi, Objective C) are equally vulnerable.

Just read the articles on CERT 903934, oCERT 2011-003Arstechnica, Cryptanalysis.euHeise (German), Hackillusion and the research paper published at 28C3.

a few quotes:

“This attack is mostly independent of the underlying Web application and just relies on a common fact of how Web application servers typically work,” the team wrote, noting that such attacks would force Web application servers “to use 99% of CPU for several minutes to hours for a single HTTP request.”

“Prior to going public, Klink and Wälde contacted vendors and developer groups such as PHP, Oracle, Python, Ruby, Google, and Microsoft. The researchers noted that the Ruby security team and Tomcat have already released fixes, and that “Oracle has decided there is nothing that needs to be fixed within Java itself, but will release an updated version of Glassfish in a future CPU (critical patch update).”

“The algorithmic complexity of inserting n elements into the
table then goes to O(n**2), making it possible to exhaust hours of CPU time using a single HTTP request”

“We show that PHP 5, Java, ASP.NET as well as v8 are fully vulnerable to this issue and PHP 4,
Python and Ruby are partially vulnerable, depending on version or whether the server
running the code is a 32 bit or 64 bit machine.”

Microsoft seems to have been notified pretty late in the cycle, I presume because the researchers started with a some platforms and finally realized the breath of platforms involved.

The ultimate solution is to patch/fix the platforms using for instance a randomized hash function a.k.a. universal hashing.

Microsoft will provide a patch for ASP.NET later today, Ruby already patched and other vendors will soon or have already (please comment if you know of other platforms and patches).

The links this morning indicated there were no known attacks. That is (maybe was) true for ASP.NET, but for PHP a public proof of concept of such a DoS is has been published by Krzysztof Kotowicz (blog) with sources at github and a demo html page.

Temporary workarounds (based on the some of the links in this and the prior blog post, and the workarounds mentioned here and here):

  1. If you can: replace hash tables by more applicable data structures
    (I know this falls in the for-if anti-pattern category, but lots of people still use a hammer when a different tool works much better)
  2. Limit the request size
  3. Limit the maximum number of entries in the hash table
  4. Limit form requests only for sites/servers/etc that need it.
  5. Limit the CPU time that a request can use
  6. Filter out requests with large number of form entries

Some platforms already have applied temporary workarounds (I know of Tomcat (default max 10000 parameters), and PHP (default max_input_vars = 1000) did, and looks like the ASP.NET fix will do too).

Other platforms (like JRuby 1.6.5.1, CRuby 1.8.7 (comments) and Perl 5.8.1 in September 2003 ) fixed it the proper way.

Note: workarounds are temporary measures that will also deny legitimate requests. The only solution is to apply a fix or patch.

A major lesson learned today for a few people around me: when vendors start publishing “out of band” updates, do not trust a single 3rd party assessment with state “initial investigation”, but be diligent and do some further research.

–jeroen

PS: Just found out that most Azure users won’t need to manually apply a fix: just make sure your Hosted Service OS servicing policy is set to “Auto”.

Posted in .NET, ASP.NET, C#, Cloud Development, Delphi, Development, Java, PHP, Ruby, Scripting, Software Development, Web Development, Windows Azure | 6 Comments »

Added a few links to my “Tools” page, @WordPress bug spuriously inserting div tags still present.

Posted by jpluimers on 2011/12/28

While re-designing a Visual Studio 2010 plus Delphi XE2 install for a specific client, I updated some of my Tools page links:

And found out that the WordPress still wrongly inserts div tags when you step out a list by pressing Enter twice is still present. Annoying, as it has been there for at least 2 years, so I’m still interesting in people having a workaround for it.

–jeroen

Posted in .NET, C#, Delphi, Development, Software Development, TFS (Team Foundation System), Visual Studio 2008, Visual Studio 2010, Visual Studio and tools | 1 Comment »

Applying XE2 Update 3: uninstall IDE Fix Pack first, then apply, then install updated IDE Fix Pack. Also update EurekaLog.

Posted by jpluimers on 2011/12/22

Before installing updates, it is always wise to read the release notes.

In this case, the below quote from the Release Notes for XE2 Update 3 was very important for me, as I use the IDE Fix Pack:

IDE Fix Pack Is Not Compatible with Update 3

The IDE Fix Pack for XE2 is incompatible with XE2 Update 3. If you have the IDE Fix Pack for XE2, you should uninstall the IDE Fix Pack for XE2 before installing Update 3. A revised version of the IDE Fix Pack for XE2 will be made available at http://andy.jgknet.de/fixpack/ .

The cool thing is, on the same day that Delphi XE2 Update 3 got releasedAndy also released the new FixPack 4.6 last week and also explained the cause of the incompatibility.

Note that because of the same reason, more products will need to be updated. EurekaLog also released an update, and I expect more vendors to release updates soon.

Update 3 breaks the monthly release cycle, but for a reason. This update contains way more fixes than the previous ones, in a much wider area and with short turnarounds between reporting and fixing (yes, it does pay to [WayBack] report bugs through QualityCentral). Just [WayBack] read the list of fixes. It is similar to the big updates we used to have for previous Delphi versions.

It also requires a lot more disk space, so make sure you have at least 5 gigabytes of free disk space.

Not related to Update 3, but still nice is that Thomas Müller made available for download the Expertimental GExperts version 1.35-2011-12-18 that is compatible with Delphi XE2. It includes a code formatter that has different bugs than the Delphi XE2 one, but for me usually works better.

–jeroen

via: Release Notes for XE2 Update 3 – RAD Studio XE2.

Posted in Delphi, Delphi x64, Delphi XE2, Development, QC, Software Development | 1 Comment »

Delphi XE2 and iOS: things you should not do

Posted by jpluimers on 2011/12/20

When developing applications for iOS using Delphi XE2, it uses a smart but convoluted FPC detour.

That results in a few things you should take into account when developing iOS applications:

  • Do not use Unit scope identifiers (that’s why for instance the FireMonkey unit FMX.Dialogs is called FMX_Dialogs when building an iOS application)
    So don’t do a “uses System.SysUtils”, but “uses SysUtils” and don’t name your units using Unit scope identifiers for now.
    It would be so nice if Embarcadero shipped the tool that made all the FMX_* units out of the FMX.* units; that alone would make code sharing between non-iOS and iOS applications in Delphi a lot easier.
  • Do not use Generics (though FPC supports them, the FPC mode that Delphi XE2 uses for iOS compatibility does not)
  • Do not use new style RTTI or TValue (they are not supported by FPC)
  • Do not use any other dataset than the ones directly descending from TDataSet (so anything TClientDataSet or IBX like is out) mostly because those depend on external C obj files, Unit scope identifiers or new style RTTI
  • Do not spread your application sources over multiple directories
  • Do not use the TMyDynamicArray.Create shortcut of initializing dynamic arrays, but use a wrapper function that uses Open Array parameters as Rob Kennedy explains on StackOverflow.
  • Do not run dpr2xcode after you have changed any files it generated (believe me, you will change those). This basically makes you have to reinvent most of the dpr2xcode logic, which is a real pain, as I’m still in that process and not completed.
    These are the things you usually want to manually keep track of:
    – most of the manual changes are keeping the “YourProject.dpr” and dpr2xcode generated “YourProject.pas” in sync
    – altering the PNG files to show different splash graphics / application icons

I will extend this list over time.

Note that this detour should be gone in a future Delphi version, but for now you need to take the above into account.

It means that you might feel like programming with one hand behind your back. Well, the Objective C and Xcode way feels very similar, but from a different perspective :)

–jeroen

Posted in Delphi, Delphi XE2, Development, Software Development, xCode/Mac/iPad/iPhone/iOS/cocoa | 2 Comments »

Asphyre Sphinx 2: X-platform game development for Delphi XE2 and/or FPC

Posted by jpluimers on 2011/12/15

Just found out that during my holiday, Asphyre Sphinx 2 was released: a free 2D/3D framework for X-platform game development that comes with full source code.

It is based on FireMonkey supports Delphi XE2 Update 1 and up, and FPC (and yes: it supports on iOS too).

Interesting stuff!

–jeroen

Via: Asphyre Sphinx 2.

Posted in Delphi, Development, Software Development | Leave a Comment »

Trouble with Google Reader and GEXperts RSS feed :)

Posted by jpluimers on 2011/12/07

While catching up my feeds after an astonishing holiday around the Antarctic Peninsula (some photos at Flickr), I bumped into a Google Reader issue with the GExperts.com RSS feed: all posts seem to be stamped 20111117T0555.

Funny, as I remember being late to report GExperts 1.35 for Delphi XE2 was released a while a go :)

It reminded me to ask Thomas to publish the XE2 version of his Experimental GExperts build though.

Lets hope he is faster than me catching up :)

--jeroen

via: Google Reader feed for GExperts.org.

Posted in About, Antarctic, Delphi, Development, Personal, Software Development, Travel | 7 Comments »

When writing applications, include Keyboard Shortcuts for both the CUA and Windows/Apple shortcuts

Posted by jpluimers on 2011/12/06

When you write applications, it is important to include both the CUA and the Windows/Apple keyboard shortcuts, and get the tab order of keyboard accessible user elements right.

Many modern applications seem to put less and less emphasis on the most efficient user input device: the keyboard.

You should: it makes your application much more pleasant to use.

I wrote about CUA before, but the Windows and Mac shortcuts are just as important.

A small table (please post a comment if you know additions):

Keyboard Shortcuts for the most common tasks.
Function CUA Windows Mac
Copy Ctrl + Insert Ctrl + C Command + C
Cut Shift + Delete Ctrl + X Command + X
Paste Shift + Insert Ctrl + V Command + V
Delete before cursor Backspace Delete
Delete after cursor Delete Fn + Delete
Undo Alt + Backspace Ctrl + Z Command + Z
Redo Ctrl + Y Command + Y
Confirm the current task Enter Return
Cancel the current task Escape Escape
Next field Tab Tab
Previous field Shift + Tab Shift + Tab
Next pane Ctrl + F6
Previous pane Alt + F6
Next window F6  Cmd + `
Previous window Shift + F6
Application menu Alt + Space
Windows menu
Local menu Shift + F10 Local Menu

Note that many Linux programs follow both the CUA and Windows settings.

References:

–jeroen

Posted in .NET, Delphi, Development, Keyboards and Keyboard Shortcuts, Power User, Software Development, xCode/Mac/iPad/iPhone/iOS/cocoa | Leave a Comment »

File Extensions of Files Generated by RAD Studio – RAD Studio XE2

Posted by jpluimers on 2011/12/01

With Delphi 1, it was easy to choose what to put in your version control systems: basically .pas, .dfm, .dpr, .inc, .res, .cfg and you were set.

Now there are many more extensions involved, so it is harder to choose what to put in your version control system and what not.

The File Extensions of Files Generated by RAD Studio page helps you with that: it lists most of the Delphi file extensions (.local is a noticeable exception) that are used today.

–jeroen

via: File Extensions of Files Generated by RAD Studio – RAD Studio XE2.

Posted in Delphi, Development, Software Development | 4 Comments »