<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Eli Bendersky's website - Microsoft</title><link href="https://eli.thegreenplace.net/" rel="alternate"></link><link href="https://eli.thegreenplace.net/feeds/microsoft.atom.xml" rel="self"></link><id>https://eli.thegreenplace.net/</id><updated>2025-05-31T13:46:04-07:00</updated><entry><title>On spaces in the paths of programs and files on Windows</title><link href="https://eli.thegreenplace.net/2011/01/28/on-spaces-in-the-paths-of-programs-and-files-on-windows" rel="alternate"></link><published>2011-01-28T16:25:47-08:00</published><updated>2022-10-04T14:08:24-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2011-01-28:/2011/01/28/on-spaces-in-the-paths-of-programs-and-files-on-windows</id><summary type="html">
        &lt;p&gt;Today while debugging the build of Python 3.2 with Visual Studio, I ran into a caveat with invoking programs on Windows from directories with spaces in their name. The caveat applies equally to the standard C &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;system&lt;/span&gt;&lt;/tt&gt; call, to Windows batch files and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cmd&lt;/span&gt; &lt;span class="pre"&gt;/c&lt;/span&gt;&lt;/tt&gt;, and to any scripting …&lt;/p&gt;</summary><content type="html">
        &lt;p&gt;Today while debugging the build of Python 3.2 with Visual Studio, I ran into a caveat with invoking programs on Windows from directories with spaces in their name. The caveat applies equally to the standard C &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;system&lt;/span&gt;&lt;/tt&gt; call, to Windows batch files and &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cmd&lt;/span&gt; &lt;span class="pre"&gt;/c&lt;/span&gt;&lt;/tt&gt;, and to any scripting/programming language with an interface to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;system&lt;/span&gt;&lt;/tt&gt;, such as Python.&lt;/p&gt;
&lt;p&gt;Suppose I have a program sitting in a directory with spaces in its name. For the sake of example let's take a trivial batch script called &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dumpfile.bat&lt;/span&gt;&lt;/tt&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;type %1
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;All it does is print the contents of the file passed to it as the first argument. I will place it in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;D:\temp\Spaces&lt;/span&gt; &lt;span class="pre"&gt;dir&lt;/span&gt;&lt;/tt&gt;. How do I invoke this script with &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;system&lt;/span&gt;&lt;/tt&gt;? By quoting around the executable name:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #00007f; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #00007f"&gt;os&lt;/span&gt;

cmdline = &lt;span style="color: #7f007f"&gt;r&amp;#39;&amp;quot;D:\temp\Spaces dir\dumpfile.bat&amp;quot; paths_with_spaces.py&amp;#39;&lt;/span&gt;
&lt;span style="color: #00007f; font-weight: bold"&gt;print&lt;/span&gt; &lt;span style="color: #7f007f"&gt;&amp;quot;&amp;gt;&amp;gt; &amp;quot;&lt;/span&gt;, cmdline
os.system(cmdline)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This script is named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;paths_with_spaces.py&lt;/span&gt;&lt;/tt&gt;, so it asks &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dumpfile.bat&lt;/span&gt;&lt;/tt&gt; to print itself. And this works as expected.&lt;/p&gt;
&lt;p&gt;Now, suppose I want to invoke &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dumpfile.bat&lt;/span&gt;&lt;/tt&gt; on some other file, which also has spaces in its full path. For demonstration I will place a simple text file named &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;file.txt&lt;/span&gt;&lt;/tt&gt; also in &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;D:\temp\Spaces&lt;/span&gt; &lt;span class="pre"&gt;dir&lt;/span&gt;&lt;/tt&gt;.&lt;/p&gt;
&lt;p&gt;Then, I write:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #00007f; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #00007f"&gt;os&lt;/span&gt;

cmdline = &lt;span style="color: #7f007f"&gt;r&amp;#39;&amp;quot;D:\temp\Spaces dir\dumpfile.bat&amp;quot; &amp;quot;D:\temp\Spaces dir\file.txt&amp;quot;&amp;#39;&lt;/span&gt;
&lt;span style="color: #00007f; font-weight: bold"&gt;print&lt;/span&gt; &lt;span style="color: #7f007f"&gt;&amp;quot;&amp;gt;&amp;gt; &amp;quot;&lt;/span&gt;, cmdline
os.system(cmdline)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Note that I placed the argument filename in quotes as well, since it also contains spaces.&lt;/p&gt;
&lt;p&gt;Unfortunately, I get an error:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;D:\temp\Spaces&amp;#39; is not recognized as an internal or external command,
operable program or batch file.
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;procmon&lt;/span&gt;&lt;/tt&gt;, I can find out that the &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;os.system&lt;/span&gt;&lt;/tt&gt; call actually invokes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;C:\WINDOWS\system32\cmd.exe /c &amp;quot;D:\temp\Spaces dir\dumpfile.bat&amp;quot; &amp;quot;D:\temp\Spaces dir\file.txt&amp;quot;
&amp;gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;under the hood. Running this manually from the command-line, I get the same error. So this is a problem with the Windows &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cmd.exe&lt;/span&gt;&lt;/tt&gt; processor, not my &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;os.system&lt;/span&gt;&lt;/tt&gt; call.&lt;/p&gt;
&lt;p&gt;Alas, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;cmd.exe&lt;/span&gt;&lt;/tt&gt; is indeed exceptionally dumb. Here's a snippet from its documentation:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (&amp;quot;) characters:&lt;/p&gt;
&lt;ol class="arabic simple"&gt;
&lt;li&gt;If all of the following conditions are met, then quote characters on the command line are preserved:&lt;/li&gt;
&lt;/ol&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;no /S switch&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;exactly two quote characters&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;no special characters between the two quote characters, where special is one of: &amp;amp;&amp;lt;&amp;gt;()&amp;#64;^|&lt;/li&gt;
&lt;li&gt;there are one or more whitespace characters between the the two quote characters&lt;/li&gt;
&lt;li&gt;the string between the two quote characters is the name of an executable file.&lt;/li&gt;
&lt;/ul&gt;
&lt;ol class="arabic simple" start="2"&gt;
&lt;li&gt;Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;
&lt;p&gt;I emphasized the condition which my example fails to fulfill. The solution, it turns out, is to induce the behavior described in (2), by wrapping the whole command-line invocation in another pair of quotes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span style="color: #00007f; font-weight: bold"&gt;import&lt;/span&gt; &lt;span style="color: #00007f"&gt;os&lt;/span&gt;

cmdline = &lt;span style="color: #7f007f"&gt;r&amp;#39;&amp;quot;&amp;quot;D:\temp\Spaces dir\dumpfile.bat&amp;quot; &amp;quot;D:\temp\Spaces dir\file.txt&amp;quot;&amp;quot;&amp;#39;&lt;/span&gt;
&lt;span style="color: #00007f; font-weight: bold"&gt;print&lt;/span&gt; &lt;span style="color: #7f007f"&gt;&amp;quot;&amp;gt;&amp;gt; &amp;quot;&lt;/span&gt;, cmdline
os.system(cmdline)
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;This works. Yet another little trick of Windows's &lt;em&gt;wonderful&lt;/em&gt; command line.&lt;/p&gt;
&lt;p&gt;Resources:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;Super User &lt;a class="reference external" href="http://superuser.com/questions/238810/problem-with-quotes-around-file-names-in-windows-command-shell"&gt;question and answer&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Python &lt;a class="reference external" href="http://bugs.python.org/issue11034"&gt;issue 11034&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

    </content><category term="misc"></category><category term="Microsoft"></category><category term="Programming"></category></entry><entry><title>Running several OSes in one - using VirtualBox</title><link href="https://eli.thegreenplace.net/2010/03/27/running-several-oses-in-one-using-virtualbox" rel="alternate"></link><published>2010-03-27T15:38:31-07:00</published><updated>2022-10-04T14:08:24-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2010-03-27:/2010/03/27/running-several-oses-in-one-using-virtualbox</id><summary type="html">
        &lt;p&gt;Being able to run a fully hosted OS in a window is very useful. With today's multi-core CPUs and loads of RAM, it is actually possible to run a guest OS with very reasonable performance. I should mention that my current PC is dual-core, has 2GB of RAM and runs …&lt;/p&gt;</summary><content type="html">
        &lt;p&gt;Being able to run a fully hosted OS in a window is very useful. With today's multi-core CPUs and loads of RAM, it is actually possible to run a guest OS with very reasonable performance. I should mention that my current PC is dual-core, has 2GB of RAM and runs Windows XP Home edition.&lt;/p&gt;
&lt;p&gt;Lately I'm using Sun's (Oracle's these days) &lt;a class="reference external" href="http://www.virtualbox.org/"&gt;VirtualBox&lt;/a&gt; software to accomplish that. VirtualBox works very well and is open-source. It keeps surprising me with its simplicity - all the complex things (like sharing files between the host and the guest OSes, and setting up the network on the guest OS) work easily. The most help I ever needed was quickly found in VirtualBox's own help file, without even digging in online forums and Google. That doesn't happen often!&lt;/p&gt;

&lt;img src="https://eli.thegreenplace.net/images/2010/03/vbox_logo2_gradient.png" align="center" title="vbox_logo2_gradient" width="140" height="180" class="aligncenter size-full wp-image-2134" /&gt;

&lt;p&gt;Being completely free and open-source, VirtualBox is much friendlier than the free version of VMWare that only allows you to run &amp;quot;appliances&amp;quot; prepared in advance by other users using the full versions of VMWare. For VirtualBox I was able to trivially install an OS either from and .iso file or from an installation CD.&lt;/p&gt;
&lt;p&gt;One of its best features is the &amp;quot;seamless mouse integration&amp;quot;. This basically means that the guest OS just runs inside a window, and whenever the mouse is in that window, focus is in the guest OS. To return to the host OS, just move the mouse outside the window and click somewhere. No pesky key-combinations required.&lt;/p&gt;
&lt;p&gt;So what am I using it for? I'm running two operating systems as guests (not all the time, of course, only when I need them).&lt;/p&gt;
&lt;div class="section" id="ubuntu-9-04"&gt;
&lt;h3&gt;Ubuntu 9.04&lt;/h3&gt;
&lt;p&gt;A great and friendly Linux distribution. I'm using it for all my Linux-related development which is mostly trying various tools that are too difficult to compile on Windows (I have a strong dislike for Cygwin), or testing programs I develop on Windows for portability.&lt;/p&gt;
&lt;p&gt;I've installed Ubuntu on VirtualBox from an .iso file downloaded from the Ubuntu repository.&lt;/p&gt;
&lt;/div&gt;
&lt;div class="section" id="windows-xp"&gt;
&lt;h3&gt;Windows XP&lt;/h3&gt;
&lt;p&gt;That's right, another instance of Windows XP, installed from the same Microsoft CD as the host (I suppose this is legal), running inside VirtualBox.&lt;/p&gt;
&lt;p&gt;Why is this useful? Sometimes programs developed on Windows machines have extra dependencies (mostly DLLs) that aren't apparent on the development machine. For example, some DLL may be required by the program and is by chance present on the development box, but the user doesn't have it!&lt;/p&gt;
&lt;p&gt;A virtual Windows XP OS is the perfect solution for testing these problems. It's a completely &amp;quot;virgin&amp;quot; installation of Windows XP, with only the network set up. When I'm creating a new version of a program and want to test its deployment, I copy it to the virtual OS. If it works there - it will work at the user's computer as well! This is very useful for executables created by Pyinstaller, that keep wanting different versions of the MSVC runtime DLLs present.&lt;/p&gt;
&lt;p&gt;What's best about this approach is that taking &amp;quot;snapshots&amp;quot; with VirtualBox is super-simple. Say that as part of my installation process I've installed some DLLs on the guest OS, and it's no longer &amp;quot;a virgin&amp;quot;. No problem - it takes me only a minute to flip back to a snapshot taken before the installation, and I have a pristine Windows box to test against, once again.&lt;/p&gt;
&lt;/div&gt;

    </content><category term="misc"></category><category term="Linux"></category><category term="Microsoft"></category><category term="Software &amp; Tools"></category></entry><entry><title>A useful Excel VBA macro</title><link href="https://eli.thegreenplace.net/2008/08/28/a-useful-excel-vba-macro" rel="alternate"></link><published>2008-08-28T19:06:43-07:00</published><updated>2022-10-04T14:08:24-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2008-08-28:/2008/08/28/a-useful-excel-vba-macro</id><summary type="html">
&lt;p&gt;
Today I've written my first useful macro in Excel VBA. I've had past experience
with Visual Basic (a long, long time ago), and it's a simple language, so the
code wasn't difficult to craft. What was much more difficult is understanding
the required methods and properties of Excel's VBA objects …&lt;/p&gt;</summary><content type="html">
&lt;p&gt;
Today I've written my first useful macro in Excel VBA. I've had past experience
with Visual Basic (a long, long time ago), and it's a simple language, so the
code wasn't difficult to craft. What was much more difficult is understanding
the required methods and properties of Excel's VBA objects.
&lt;/p&gt;&lt;p&gt;

The macro dresses as a function (which you can fill in a cell with
&lt;code&gt;=func_name(params)&lt;/code&gt;). It receives a range, and computes a "special"
sum of the elements in it. The sum is special because sometimes the elements are
added as they are, and sometimes they're doubled, depending on the text in the
adjacent column.
&lt;/p&gt;&lt;p&gt;

This is far from rocket science, but I'm satisfied because in this case Excel is
definitely the right tool for the job, and having the ability to code simple
macros greatly enhances its usability.
&lt;/p&gt;

    </content><category term="misc"></category><category term="Microsoft"></category></entry><entry><title>"Unassigned variable" error in C#</title><link href="https://eli.thegreenplace.net/2007/05/20/unassigned-variable-error-in-c" rel="alternate"></link><published>2007-05-20T18:16:19-07:00</published><updated>2025-05-31T13:46:04-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2007-05-20:/2007/05/20/unassigned-variable-error-in-c</id><summary type="html">
      &lt;p&gt;
        Today I was reading about the "unassigned variable" detection of the C# compiler. If you write this code:
      &lt;/p&gt;&lt;p&gt;
&lt;pre&gt;&lt;code&gt;
int x;
Console.WriteLine(x);
&lt;/code&gt;&lt;/pre&gt;
      &lt;/p&gt;&lt;p&gt;

The compiler generates an error: &lt;i&gt;error CS0165: Use of unassigned local variable 'x'&lt;/i&gt;. Naturally, I wanted to see how this feature fails, and not surprisingly:
      &lt;/p&gt;&lt;p&gt;

&lt;pre&gt;&lt;code&gt;
int x …&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</summary><content type="html">
      &lt;p&gt;
        Today I was reading about the "unassigned variable" detection of the C# compiler. If you write this code:
      &lt;/p&gt;&lt;p&gt;
&lt;pre&gt;&lt;code&gt;
int x;
Console.WriteLine(x);
&lt;/code&gt;&lt;/pre&gt;
      &lt;/p&gt;&lt;p&gt;

The compiler generates an error: &lt;i&gt;error CS0165: Use of unassigned local variable 'x'&lt;/i&gt;. Naturally, I wanted to see how this feature fails, and not surprisingly:
      &lt;/p&gt;&lt;p&gt;

&lt;pre&gt;&lt;code&gt;
int x;
int y = 1;

if (y == 1)
    x = 9;

Console.WriteLine(x);
&lt;/code&gt;&lt;/pre&gt;
      &lt;/p&gt;&lt;p&gt;

Generates the same error. This was expected, of course - since the compiler will have to actually run the program to test for the "unassigned" condition in the general case (which is what happens, kind of, in dynamic interpreted languages like Ruby - where runtime and compile time refer to the same process).
      &lt;/p&gt;&lt;p&gt;

Still, this error in C# is useful, since it can probably detect 99% of the real-world cases.
      &lt;/p&gt;
    </content><category term="misc"></category><category term="Microsoft"></category></entry><entry><title>.NET looks nice</title><link href="https://eli.thegreenplace.net/2004/03/28/net-looks-nice" rel="alternate"></link><published>2004-03-28T23:36:00-08:00</published><updated>2022-10-04T14:08:24-07:00</updated><author><name>Eli Bendersky</name></author><id>tag:eli.thegreenplace.net,2004-03-28:/2004/03/28/net-looks-nice</id><summary type="html">
      &lt;p&gt;
When you look at a programming language/methodology/framework and it seems
"warm, nice and fuzzy" it's usually a good sign.
&lt;/p&gt;&lt;p&gt;
I've been digging a little in .NET 2003 Studio in the past couple of days, and
it looks like a truly great environment. The design of their CLR is …&lt;/p&gt;</summary><content type="html">
      &lt;p&gt;
When you look at a programming language/methodology/framework and it seems
"warm, nice and fuzzy" it's usually a good sign.
&lt;/p&gt;&lt;p&gt;
I've been digging a little in .NET 2003 Studio in the past couple of days, and
it looks like a truly great environment. The design of their CLR is clean and
consistent over several programming languages. Many old things were rewritten
and allow easier coding. It really looks like a great and very rapid tool for
Windows development. In VB 6 it was also possible to throw a reasonable
applications with a GUI very quickly, but in VB.NET (and C#.NET) it looks
cleaner, better thought out and generally more consistent.
&lt;/p&gt;&lt;p&gt;
One big disatvantage is distribution. If the person next to me has no .NET
runtime installed on his PC, I must copy a 20 MB pack to him in order to make
any application work, and this sucks...
&lt;/p&gt;
    </content><category term="misc"></category><category term="Microsoft"></category><category term="Software &amp; Tools"></category></entry></feed>