C# / .NET MessageBox is hidden behind the Form

Imagine calling the following C# code:MessageBox.Show(this, "Some text...", "Title", MessageBoxButtons.OKCancel);

After executing this line, the Message Box is not visible, it seems like hidden behind your form, without input focus, mouse clicks cause default beeps, and it suddenly appears if you click Alt or F10 key.

It turns out I had a custom drawing routine for one of the form’s controls (OwnerDraw / OnPaint), and I was changing the property that was causing the redraw cycle. Make sure, that in your OnPaint / DrawItem / DrawSubitem / etc. you are not invalidating your Control.

Also, if you are using .NET ListView control, it has a bug.

Because of a bug in the underlying Win32 control, the DrawItem event occurs without accompanying DrawSubItem events once per row in the details view when the mouse pointer moves over the row, causing anything painted in a DrawSubItem event handler to be overpainted by a custom background drawn in a DrawItem event handler. See the example in the OwnerDraw reference topic for a workaround that invalidates each row when the extra event occurs. An alternative workaround is to put all your custom drawing code in a DrawSubItem event handler and paint the background for the entire item (including subitems) only when the DrawListViewSubItemEventArgs.ColumnIndex value is 0.

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawitem%28v=vs.80%29.aspx

This is not limited to MessageBox using C# / .NET framework. This also happens very often, with Win32 & WinAPI, with a Form class, etc.:

Posted in Programming | Tagged , , , | 1 Comment

String.format in Java has weird rounding by default

I was participating in one of our Android projects, and while testing I discovered, that our program acted weird — app displayed one value, but acted like the value was different. The value was 1.45, and our Java Android app displayed 1.4 instead of 1.5.

The source looked something like:
double d = 1.45;
String val = String.format(Locale.US, "%.1f", d) + "%";

I am still a beginner in Java, and at first I believed, that String.format rounds down by default. This was till I saw, that value 1.55 is rounded up by default.

Digging into Java documentation I found, that String.format uses RoundingMode HALF_EVEN by default:

Rounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor. Behaves as for RoundingMode.HALF_UP if the digit to the left of the discarded fraction is odd; behaves as for RoundingMode.HALF_DOWN if it’s even. Note that this is the rounding mode that statistically minimizes cumulative error when applied repeatedly over a sequence of calculations. It is sometimes known as “Banker’s rounding,” and is chiefly used in the USA. This rounding mode is analogous to the rounding policy used for float and double arithmetic in Java.

http://docs.oracle.com/javase/6/docs/api/java/math/RoundingMode.html

Of course other beginners are also discovering this Java Rounding.

Unfortunately Android does not support setRoundingMode, so currently using the following workaround:
double d = (double)Math.round(((double)percents) * 10f) / 10f;
String val = String.format(Locale.US, "%.1f", d) + "%";

Posted in Programming | Tagged , | 1 Comment

PowerShell in Unicode, Windows 8 RP and Windows Server 2012 RC

PowerShell 3.0 under Windows 8

I was testing our software with latest version of Microsoft Windows — Windows 8 Release Preview and Windows Server 2012 Release Candidate (Windows Server 2012 RC Datacenter).

There was some changed behavior in very specific cases for some rarely used Windows API. I also found some race conditions, that showed up in Windows 8, but all this was trivial to fix.

The strange problem appeared in capturing console output from PowerShell in UTF-8 or Unicode (UTF-16). We are using CreateProcess Windows API function with redirected pipes. And strangely all output is converted from Unicode to ASCII. We are using OutputEncoding UTF-8:
[Console]::OutputEncoding = [Text.Encoding]::Utf8See example here: Unicode in PowerShell – example #2.

Windows 8 is using a new version of PowerShell — v3, so I tried to use compatibility command line switch -version 2.0, and this solved the problem.

Most probably this is a bug in Microsoft PowerShell — PowerShell somewhere is converting all output to ASCII. The question remains, will this be fixed in RTM or we will be unable to use Unicode output in PowerShell v3.

P.S. Currently I am unable to test this under Windows 8 RTM or Windows Server 2012 RTM, because for MSDN subscribers, Windows 8 and 2012 will be available on August 15, 2012.

Posted in Computers, OS | Tagged , , , , | 2 Comments

Visual Studio Form Editor: To prevent possible data loss before loading the designer, the following errors must be resolved

This is not the first time. I have seen this error message in the past (probably multiple times), and every time I look to this error and I do not understand, why it appeared and how to fix it. I read the text, projects not referenced, projects have been built… wait… yes, projects must be built, otherwise Inherited Forms can not be shown. Building the projects with base classes solved the problem.

Visual Studio Form Editor: To prevent possible data loss before loading the designer, the following errors must be resolved: The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following clasess in the file: MyClass.cs — The base class MyBaseClass.cs could not be loaded. Ensure the assembly has been referenced and that all projects have been built.

Posted in Programming | Tagged , , | 1 Comment

Spammed by WordPress comment stealing bot with Facebook profile

Some time ago (1/2 year, may be 1 year) strange comments started to appear in our WordPress comment moderation queue. They all contained some random comments from random places, they all had the similar URL:
http://www.facebook.com/profile.php?id=XXXXXXXXXXXXXXX
where XXXXXXXXXXXXXXX is profile ID, that is changing from one comment to another. I didn’t click on the fake profiles, do not have time to research who are behind this.

Today regular chunk of SPAM in moderation queue, again with fake Facebook profiles, again bypassed CAPTCHA somehow (Chinese clickers perhaps), but one particular comment grabbed my attention. It is comment from post about old tabled unboxing.
Here is a comment:

Author : Bagas (IP: 92.99.196.92 , 92.99.196.92)
E-mail : rkleinschmidt@SOMETHING–HEALTH–related–WAS–here.org
URL : http://www.facebook.com/profile.php?id=XXXXXXXXXXXXXXX
Whois : http://whois.arin.net/rest/ip/92.99.196.92
Comment:
I buy few unit from amazon , at first i think the $199 is good deal , but actllauy this player is worst , the touch screen really slow and hard to scroll , and the app , i think all junk app , please consider don’t think $199 is a good deal , better u add another bucks for really goods item.

The strange thing is that this comment seems like absolutely legitimate user commenting on Archos tablet. Yes, touch screen is slow, there are junk apps, etc.

I did a quick Google search, and instantly found original site, where this comment was stolen:
http://www.yugatech.com/personal-computing/archos-7-home-tablet/

It is blog post about Archos tablet. And comment is from “June 25, 2010 at 5:32 pm”. It is also running on the WordPress engine.

So the theory:

  • The Comment Stealing BOT (CSB) finds random WordPress blog;
  • CSB then finds some random posts;
  • CSB somehow searches the internet, using keywords from my blog post;
  • CSB finds some WordPress blog and grabs some random comments;
  • Sometimes it succeeds, and comment looks like real user post;
  • It tries to promote some Facebook pages;
  • If you are managing multiple blogs, you spot this pattern instantly.

Of course other WordPress users are noticing this too:

Posted in Web | Tagged , , | 1 Comment

Your program is returning ErrorLevel 128 (exit code)

Here is a scenario:

  • You have written a program;
  • You run it;
  • First line of the program never gets executed;
  • ErrorLevel (exit code) 128 is returned.

You search Google for solution, but nothing useful is found. This happened to me. Problem was — Windows was running out of memory on my virtual computer. This can happen on physical machine too.

Later I found MS support article. User32.dll or Kernel32.dll fails to initialize:

Sometimes an application that is executed by either CreateProcess() or CreateProcessAsUser() fails and you receive one of the following error messages:
Initialization of the dynamic library \system32\user32.dll failed. The process is terminating abnormally.
Initialization of the dynamic library \system32\kernel32.dll failed. The process is terminating abnormally.
The failed process returns the exit code 128 or
ERROR_WAIT_NO_CHILDREN

Posted in OS, Programming | Tagged , , | Leave a comment

Online CPU benchmark now with user comments

Two weeks ago we launched a New version (v1.5) of online CPU benchmark – SilverBench. This new version allows users to post comments — details about their tested machines. Of course these comments are optional and completely anonymous if user chooses to.

Comments can be viewed on the main page, result page and Top 100 page.

SilverBench comments

Posted in Web | Tagged , , , | Leave a comment

How to execute JavaScript after inserting into element.innerHTML automatically

Here is a scenario:

  1. you have some software/component that inserts partial HTML into page using JavaScript element.innerHTML function;
  2. you can insert any HTML tags including <script>;
  3. you can not call any JS function (software/component does not have such functionality), and inserted script using innerHTML is not executed automatically.

After some lost time trying to come up with the solution, I found the following trick using image tag and onload attribute. Again if you try to use script tag, it will not work:
<script type="text/javascript">alert('testing')</script>
However code with image trick will execute JavaScript automatically:
<img src="/images/1x1-transparent.png" onload="alert('testing');this.parentNode.removeChild(this);" />

Note that removeChild, will remove just inserted image, so your HTML code stays unaffected.

Sources:

Posted in Programming, Web | Tagged , , , | 1 Comment

Disappearing folder – bug in Windows 7

In process of testing one of our automation software I hit interesting Windows 7 bug. First I found it in our software, and right away tested this scenario on Windows 7, and bug was there.

The bug. Create folder on the Desktop.

Right click on Desktop

New folder

New folder appears and cursor blinks allowing to change the name for the folder.

Cursor at the end

Place cursor at the end of folder name, and enter dot (.) character.

Dot at the end

After pressing Enter key the folder disappears.

Folder disappears

But do not worry, it will come back after reboot, or after refresh (F5 key).

After refresh

Appears that this bug/behavior… that only Windows 7 is affected. Other Windows versions I tested does not have this bug, even Windows XP and upcoming Windows 8 behaves correctly (see screenshots at the end of the post).

A side note. By “correctly” in this case I mean, that folder does not disappear, however perhaps, there is another bug. The dot “.” character disappears in all Windows versions. This of course depends on interpretation, for example, I also tested this on Ubuntu 10, and Ubuntu (and probably most nixes) allows dot (.) at the end of filename.

Why this happens? Every filename consists of two parts: filename and extension. These two parts are separated by dot (.) character. Windows Shell hides this dot from you, and when you enter “filename.”, it thinks, that this must be a filename without extension, so it stores only filename. In theory you should be able to trick shell by adding more dots, like “filename…”, but Windows 7 Shell reduces them to no dot. Of course you can create file or folder with one or more dots at the end, but for this you will need a normal file manager, like Far Manager (which is open source). Also, keep in mind, that you will not be able to delete this file or folder from desktop using Windows Shell, you will need to switch to file manager again.

Note 1. Windows Server 2008 R2 is based on the same codebase, so it has the same bug.
Note 2. Operating Systems used in the test:

  • Windows 7 Professional 64-bit
  • Windows Vista Enterprise
  • Windows Server 2008 Enterprise
  • Windows Server 2008 R2 Datacenter
  • Windows Server 2003 Enterprise
  • Windows 8 Consumer preview
  • Windows XP
  • Ubuntu 10
Posted in Computers, OS | Tagged , , , , , | 1 Comment

When Windows automatic update suddenly stops updating Windows automatically

Recently we noticed that some of our test and dev servers were not updating automatically through Windows or Microsoft update.

It was real pain to find why this happened. There are numerous threads in the Internet about failed Windows update, and it is very hard to find the right answer. Luckily, I found a post in CNET forum from Jim Sheehy from Sequel Data Systems. He supports more than 800 desktops and he is writing about three possible causes of Windows update failure:

  1. The Windows Update Temporary folder is corrupted. (%windir%\SoftwareDistribution)
  2. Update Agent 3.0 is not installed properly. (Download latest Update Agent)
  3. Installer 3.1 is corrupted. (Reinstall Windows Installer 3.1 or …)

Read more at CNET forum: Help! My Windows automatic updates suddenly stopped working.

Posted in OS | Tagged , , , | Leave a comment