Don’t forget to pass the current directory along with the command line to your single-instance program


If you make a single-instance program, and somebody runs a second copy of the program and passes a command line, the most common way of handling this is to hand the command line to the first copy of the program and let the first copy deal with it. When you do this, don't forget about the current directory.

If somebody passes a relative path to the second copy of the program, that relative path needs to be resolved against the current directory of the second program. I've seen programs that fail to take this into account. Instead, they pass the command line to the first copy of the program, and the first copy resolves the relatives paths against its current directory.

Allow me to give a concrete example.

C:\Directory1> start LitWare file1.lit
... runs LitWare with the file C:\Directory1\file1.lit ...
C:\Directory1> cd ..\Directory2
C:\Directory2> start LitWare file2.lit

What you expect to happen is that LitWare opens the file C:\Directory2\file2.lit, since the relative path file2.lit should be resolved against the current directory, C:\Directory2. Unfortunately, I see some programs◊ try to open the file C:\Directory1\file2.lit since they passed the command line to the first copy, and the first copy then resolved the relative path file2.lit against the current directory of the first copy, namely C:\Directory1.

Result: "File not found error."

Moral of the story: Be mindful of the current directory when parsing the command line. You can either have the second copy parse the command line (and resolve the relative paths against its own current directory), or you can pass the current directory to the first copy (and resolve the relative paths against that directory). Either works. What doesn't work is passing the relative paths to the first copy and having the first copy resolve it against its own current directory.

My workaround for dealing with these programs is using the %CD% pseudo-variable.

C:\Directory2> start LitWare %CD%\file2.lit

The %CD% pseudo-variable expands to the command prompt's current directory. (Don't forget to enclose it in quotation marks if the path contains a space.)

Nitpicker's corner

◊Remember that the phrase "some programs" means "some programs" and Microsoft programs fall under the category of "programs".

Yes, I'm using strange symbols as note markers. My hope is that the people who nitpick over my choice of note markers will get so upset that their heads will explode and they therefore will be unable to nitpick further. It appears that some people, on the other hand, suffer from some medical condition that disables the "fun" center of the brain.

[Update: Give IE6 a little font help.]

Comments (30)
  1. Anonymous says:

    Your comments about nitpickers (exploding heads etc.) are a little bit rude!

  2. Anonymous says:

    Hi Raymond.

    It seems like the nitpickers are getting you down a bit.  I don’t mean today, particularly:  the "exploding heads" was obviously meant in fun.  However there seems to be a feeling in the recent days/weeks that you are getting fed up.

    I’m sure you know that nothing you do will please everybody.  However I suspect that most readers (like me) greatly enjoy your writing.  The tech stuff is excellent and the little bits of light-heartedness break it up nicely.

    I guess that my point is: take satisfaction in the thousands of readers who come back regularly to read your stuff, and don’t be dragged down by the small number who’s life is so dull that they find a need to complain about the character you use to designate a footnote.

    God bless.

    Michael J

  3. Anonymous says:

    "Yes, I’m using strange symbols as note markers. My hope is that the people who nitpick over my choice of note markers will get so upset that their heads will explode and they therefore will be unable to nitpick further."

    That’s godly

  4. Anonymous says:

    Not directly related to the article, but I was rather amused that my newsreader interpreted %CD% as Í% (Unicode 00CD as the first character).

    I personally rather enjoy finding new and exciting footnote symbols in your posts.

  5. Anonymous says:

    Awwwe, but I quite enjoy the random footnote symbols.

    I don’t read comments much, so I wondered why you went away from the dagger/doubledagger footnotes, I see now you’re taunting someone who’s obviously been reinforced with a wooden stick.

  6. JamesNT says:

    Mr. Chen,

    I second Michael J.’s remarks.  I thoroughly enjoy your blog and you are truly my programming god.  Please do not let the undead rotting evil of the slashdotter get you down.  There are those of us who greatly appreciate all you do to make our lives easier and to educate us with the things you have learned (many times at great expense) over the years.

    JamesNT

  7. Anonymous says:

    XYZ has this bug. I first noticed it when I  created a doskey macro to run it from the command line. I’ve already added the %CD% workaround to my cmd startup script.

    [Edited to conform to ground rules. -Raymond]
  8. Anonymous says:

    "Your comments about nitpickers (exploding heads etc.) are a little bit rude!"

    As is the nitpicking and general pedantry displayed by these people: it’s clear they’re only interested in proving how clever they are in finding a flaw that no-one really cares about, but not clever enough to be able to take the posts as intended.

  9. Anonymous says:

    Are we allowed to nominate new footnote characters?  I’d like to propose U+00B6 (¶), U+266B (♫), and U+2642 (♂).  These little guys need some love and I think they’d make great friends with the prescriptivists.

  10. Anonymous says:

    You know, until I read this post, I’d never considered this behaviour (for single-instance programs). It strikes me as a potential attack vector that should be carefully analysed in practice.

    Scenario: program that takes no user input once running, but handles command-line inputs, and runs single-instance. The first instance is run with elevated privilege (perhaps as part of a secured process), and is given a current directory within some restricted area (the Windows directory or some such). A second copy, run unelevated, with relative file references, could therefore modify the behaviour of the first instance.

    Thanks for the heads-up on this one. One more attack surface to be aware of!

  11. Anonymous says:

    I propose the following: ☺ ☻ . They’ll brighten the reader’s day and have the added benefit of being in most non-Unicode fonts!

  12. Anonymous says:

    Seriously, just turn off the comments and let’s be done with it.  I’ve read your blog(comments and all) religiously for almost 2 years now and I thoroughly enjoy your writing(including the Nitpicker’s Corner), but if it gets in the way of your writing I’m fine with everyone keeping their comments to themselves(or on their own blogs).

    I know that it has been mentioned in the comments before(I did a search) and I see the irony.

  13. Anonymous says:

    Dusty and everyone else who makes this suggestion:

    A Blog Without Comments Is Not a Blog:

    http://www.codinghorror.com/blog/archives/000538.html

  14. JamesNT says:

    Michael,

    Amen to that.  What the nitpickers and undead rotting slashdotters fail to realize is that their behavior is self-destructive.  If Mr. Chen decides he no longers wishes to endure the responsibility of maintaining this blog, then all the nitpickers and slashdotters will go around belly-aching about how unfair Mr. Chen is when they themselves drove Mr. Chen to his decision.

    In other words, some people here are clearly still 16 years old.

    JamesNT

  15. Anonymous says:

    Reading through your suggestions for footnote characters pointed something out to me : IE isn’t rendering most of them (I find it easiest to come straight to a post via my newsreader which insists on ignoring my browser preference and opening with IE).

    That’s on XP, IE6, and Complex Script and East Asian Language support both switched on. Guess it just hadn’t caught up with unicode properly at this version.

  16. Anonymous says:

    Be mindful of your thoughts… er… directories, Anakin.

  17. Anonymous says:

    "Result: ‘File not found error.’"

    If you’re lucky.

  18. Anonymous says:

    Man, I swear I did a Google search like two days ago because I was looking for a way to resolve the current directory from a batch file and that old post was the one I found. It’s uncanny!

  19. Anonymous says:

    It seems that opinions on the etiquette of blogs varies considerably.  Being old & wise (in my own mind anyway), I offer the following:

    1. If you don’t want or don’t like feedback, a newsletter is what you want to write, not a blog.

    2. A blog is a newsletter with feedback.  If you only want feedback you like, then delete what you don’t like.

    3. If the feedback makes you angry, tense, upset, etc., then you should disable it; or just delete what you don’t like.

    4. If you like complaining about the feedback you don’t like, that’s fine too, but you should expect that to have a chilling effect on future comments.  Of course it has no effect at all on those who never read the comments.

    5. If readers’ opinions of your choice of footnote markers matter to you — and evidently you consider it worth debating here — then presumably their opinion is as good as anybody’s (same as, e.g., belly-buttons).  Hardly an emotional issue for many, I’d bet.

    6. Numerals have a lot of pedigree and are never controversial.  

    7. If you manage to go say, a year without repeating a footnote symbol that would be pretty cool.

    For the ./-bashers:  WTF?  Don’t you think there might be something to a geek website that attracts about 10,000 times the number of visitors as this one?  Not to mention that they have actually found an economical* way of dealing with idiocy and spam.

    *That’s <i>economical</i> not <i>perfect</i>.

  20. Anonymous says:

    "For the ./-bashers:  WTF?  Don’t you think there might be something to a geek website that attracts about 10,000 times the number of visitors as this one?  Not to mention that they have actually found an economical way of dealing with idiocy and spam."

    Actually, I believe you’ve just demonstrated one of the problems with Slashdot. "Idiocy", as you use it here, must be defined as "opinions that run contrary to the opinions held by the majority", as that’s the only effect I’ve noticed from the ratings mechanism there. In an extremely broad population, that might not be a bad thing. But in a narrowly self-selected (and self-reinforced) group like the regular members of Slashdot, I’d venture to say it diverges widely from any acceptable definition of "idiocy".

    In other words, while I’m sure it’s entertaining and possibly informative to the regular readers of Slashdot, it’s essentially an echo chamber producing its own brand of consensus reality. The trouble really comes when Slashdot denizens venture out into other areas, and carry with them their unshakable convictions nurtured over thousands of conversations with like minds. They tend to troll a lot. And they often don’t even realise they’re doing it — after all, it’s the "truth" to them.

    Traffic volume does not lead to correctness, just consensus.

    In the spirit of Raymond’s Nitpickers Corner, I’ll take the time to point out that not *all* Slashdot denizens are loud, narrowminded, and vindictive. I’ll also point out that there are plenty of loud, narrowminded, and vindictive sorts in other web populations as well.

  21. Anonymous says:

    @Wolf Logan

    Very well said.

  22. Anonymous says:

    I continue to be impressed by Raymond’s footnote characters, particularly that he hasn’t had to resort to anything off Led Zep IV – I wouldn’t be surprised to find that there’s a dialect of Unicode that includes them.

    Incidentally, most of the subsequent suggestions don’t display in IE6 for me either – this may be considered a feature of the footnote markers rather than a bug though.

  23. Anonymous says:

    Just curious. You recently published a book based on past entries to this blog. It was edited and expanded upon. Assuming a second book were to be published in the future, how would you deal with the footnotes? Would you leave them as they are or edit them to conform to standard practices of English writing and publishing?

  24. Dean Harding says:

    Ryan: my guess is the footnotes would be removed. They’re pretty much there because of pointless nitpicking in the comments; something you don’t have to worry about in a book.

    It’d be interesting to see how you get the wierd footnote markers past the editors, though :-)

  25. Anonymous says:

    Many apps doesn’t handle current dir very nice, like Visual Studio, which when compiling source files that links to strong name key files, then VS tries to locate the snk-files relative the the process’ current path, not relative to the source file. And VS’ current path is of course changed by the Open/SaveAs dialog.

    Why do Open/Save dialog change current dir anyway? .net framework have a flag to restore the original dir, and it appears to be a "patch" to prevent windows default behaviour. APIs that changes the process’ current dir are treacherous, imho.

  26. Dean Harding says:

    Open Dialog: when compiling, paths are relative to the project file, not to the source file. And it’s got nothing at all to do with Visual Studio’s current working directory.

  27. Anonymous says:

    Whoops.  I’m fairly sure that one of my programs made this mistake.  Fortunately it hasn’t been released yet, so this post came just in time :)

    "Scenario: program that takes no user input once running, but handles command-line inputs, and runs single-instance. The first instance is run with elevated privilege (perhaps as part of a secured process), and is given a current directory within some restricted area (the Windows directory or some such). A second copy, run unelevated, with relative file references, could therefore modify the behaviour of the first instance."

    IIRC, non-elevated processes can’t access shared memory or send Windows messages to elevated processes (which are two of the main techniques used to talk between instances), so in most cases this shouldn’t be a problem — you’d end up with one ‘elevated’ instance that all similarly elevated sub-instances talked to, and one ‘non-elevated’ instance that all similarly non-elevated instance talked to.

    I’m sure there are exceptions, though, so it is something to bear in mind, you’re right.

  28. Anonymous says:

    I do like the phrase ‘the undead rotting evil of the slashdotters’. It conjures up and image of Raymond making a heroic last stand against a horde of brain eating zombies armed with only with NTSD and a sharp sense of humour.

  29. Anonymous says:

    I don’t think Raymond wants to explode the nitpicker’s head so much as to Blow. Their. Minds!

  30. Anonymous says:

    Get the owner to share it with you.

Comments are closed.