Converting an HRESULT to a Win32 error code: Diagram and answer to exercise


Here's the diagram from How do I convert an HRESULT to a Win32 error code?. If you are offended by VML, cover your ears and hum for a while.

Win32 HRESULT

The little sliver at the top is the mapping of zero to zero. The big white box at the bottom is the mapping of all negative numbers to corresponding negative numbers. And the rainbow represents the mapping of all the positive values, mod 65536, into the range 0x80070000 through 0x8007FFFF.

Now let's take a look at that puzzle I left behind:

Sometimes, when I import data from a scanner, I get the error "The directory cannot be removed." What does this mean?

My psychic powers told me that the customer was doing something like this (error checking deleted):

ReportError(HWND hwnd, HRESULT hr)
{
 DWORD dwError = HRESULT_CODE(hr);
 TCHAR szMessage[256];
 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL,
               dwError, 0, szMessage, 256, NULL);
 MessageBox(hwnd, szMessage, TEXT("Error"), MB_OK);
}

and that the actual HRESULT was WIA_ERROR_COVER_OPEN, which is defined as

#define WIA_ERROR_COVER_OPEN MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIA, 16)

Passing this value to HRESULT_CODE would yield 16, which maps to

//
// MessageId: ERROR_CURRENT_DIRECTORY
//
// MessageText:
//
//  The directory cannot be removed.
//
#define ERROR_CURRENT_DIRECTORY          16L

And that would explain why the customer reported this strange error when reading data from a scanner.

Comments (60)
  1. Peter Ritchie says:

    Both IE6 and IE7 won’t display that WML…

    [Looks like VML doesn’t work any more on Web sites (but works locally). Must be some security thing. My previous pages that use VML stopped working, too. Copy the source between the “<html xmlns:v=”urn:schemas-microsoft-com:vml”> and matching </html> to your hard drive and view it there, it works. -Raymond]
  2. will Sullivan says:

    If you threw this as a Win32Exception (C# roolz) would it map correctly?

  3. anon says:

    It failed to render meaningfully in FF2, Opera 8, and Opera 9 as well. . .

  4. Tom says:

    Not to pick on you, Raymond, but the failure of the VML to render in most of the current popular browsers begs the question: Just what browser do you use?

  5. Anders says:

    I tried Opera9, Firefox2 and IE6 and I can’t see any WML

  6. KTamas says:

    Ain’t working here either. FF2 / IE7.

  7. Mike says:

    Raymond, just use images.  It is pretty much guaranteed to work, and it doesn’t alienate everyone who doesn’t use IE.  Not to mention that that VML in this post doesn’t render in ANY browser.

  8. Dan McCarty says:

    Raymond’s brilliance at computers is sometimes superceded by his silly stubborness at lost causes.

    (Anyone remember the joke about the smartest guy in the world who jumped out of the failing airplane with the hippie’s backpack?…)

    Just upload a damn image, Raymond.  There are n ways to do it; I know a 5 year old who could show you how.  Or is that part of the quirky charm?

    [Images of line art look like crap because they don’t scale. And the image URL can’t be guessable. (Since I write these articles months in advance, people would be able to see images for articles that haven’t been published yet.) Microsoft has its own policies about where images can be stored. And have you read those terms of service? -Raymond]
  9. Guy says:

    I use Outlook 2007 as my RSS aggregator and reading this post in Outlook I am able to see the VML

  10. Dave says:

    Strangely enough, I subscribe to the RSS feed via RSS Popper for Outlook, and the VML shows up fine there. Trust me, it’s not that exciting an image.

    There are plenty of free image upload sites. You’d think Microsoft would give bloggers a place to put images, but whatever.

    But back to the topic…Raymond, is there a reason why HRESULT got created in the first place, rather than having OLE/COM return a Win32 error code? Or did I miss that part of the movie?

    [Because time machines hadn’t been invented yet. -Raymond]
  11. I think IE is choking on the nested-html-tags.  Looking at the vml online works too, as long as the namespace is in the parent <html> tag:

    http://www.geocities.com/mvaneerde/HRESULT-to-Win32-vml.html

    [I tried an XML:NAMESPACE tag but that didn’t work either. -Raymond]
  12. Andy C says:

    “Looks like VML doesn’t work any more on Web sites (but works locally). Must be some security thing.”

    I don’t think it’s a security thing, it appears to be related to the DOCTYPE you are using (leave that in locally and it doesn’t render correctly.) I’m not enough of a web geek to tell you how it can be fixed though.

    [Aha. Too bad I don’t control the DOCTYPE. -Raymond]
  13. data: URIs would be an elegant solution to the hosting problem, because the image would be in the article…

    http://en.wikipedia.org/wiki/Data:_URI_scheme

  14. BryanK says:

    I won’t argue that data: URIs wouldn’t be elegant.  But the problem I see is that there’s no support for them in any version of IE, including 7.

    Good try though.  Maybe in ten or fifteen years, after everybody moves to IE8.  ;-)

    As for the DOCTYPE — they seem to be using the XHTML 1.0 Frameset doctype.  I’m sure somebody thought they had a good reason for that, but I’m not sure what it was.  I wonder if Transitional would work any differently?  Or does it have to have no DOCTYPE at all for VML to work right?

  15. Rick C says:

    I’m with the people wondering why MS doesn’t allow you to upload images.  I’m pretty sure I’ve seen other MSDN blogs using them, tho.

    In fact, here it is:

    http://blogs.msdn.com/ce_base/archive/2006/11/09/Memory-marshalling-in-Windows-CE.aspx

    [Notice that those pictures are hosted on the author’s personal web site. I can’t do that (other restrictions apply to my personal site, plus I want to keep my personal and professional lives separate). -Raymond]
  16. Dan McCarty says:

    Raymond wrote:

    "[Images of line art look like crap because they don’t scale. And the image URL can’t be guessable. (Since I write these articles months in advance, people would be able to see images for articles that haven’t been published yet.) Microsoft has its own policies about where images can be stored. And have you read those terms of service? -Raymond]"

    Like I said, silly stubborness.  Thanks for illustrating my point.

    So if "line art that doesn’t scale" looks like crap, guess what VML that doesn’t render looks like…

  17. Cody says:

    [So if "line art that doesn’t scale" looks like crap, guess what VML that doesn’t render looks like…]

    To be fair, he did right this far before he knew the VML would fail, which, by the way, was not his fault.

  18. Stinky says:

    put the image at Image shack or Photobucket and then link it from your post.  That should work Raymond. :)

  19. Neil says:

    SVG anybody?

  20. BryanK says:

    Nonsense. http://imageshack.us/.

    Might want to re-read this bit of his response — "And the image URL can’t be guessable."

    ;-)

    (Now, I won’t argue about whether or not I think this is necessary, but that is one of his conditions.)

  21. Mike Weiss says:

    I suggest ASCII art.

  22. LB says:

    tinyurl.com would generate an unguessable URL.

  23. Dan McCarty says:

    @Cody: True, not his fault.  It’s just funny to see his peculiar no-image policy make such a public failure–by his own company’s browser, no less–when a simple image like what Dave posted would’ve worked just fine.

    BTW, this is the 3rd or 4th discussion I can remember that was ruined by this VML silliness.  While he may believe that he has genuine reasons not to change his policy, the fact that no one pays attention to the subject matter when this happens is the best reason IMO to change.

  24. rolfhub says:

    VML versus graphics

    A simple solution could be the following:

    Step 1: Raymond posts his article with embedded VML, as usual

    Step 2: The first person that [manages to get the VML rendered correctly] AND [has any webspace to up a picture to] takes a screenshot (but please no JPEG, please use GIF or PNG) and uploads it

    Step 3: Raymond visits the link posted in step 2 and remarks whether the graphic is a correct rendering of the VML

    –done (as soon as there is a correct rendering)–

    Step -1 (most important): Nobody complains about the VML used, because we all don’t want to hear it any more

    This way, we get our graphics, the VML too (for those who can see it rendered locally), and Raymond doesn’t have to upload any pictures. OK?

  25. Step 3: Raymond visits the link posted in step 2 and remarks whether the graphic is a correct rendering of the VML

    Bonus geek points for anyone that can hack their server to show the correct graphic for Raymond’s IP but (let your imagination roam free) for other visitors.

    Switching the image after Raymond approves it is cheating.

  26. BryanK says:

    There is no such thing as a Win32Exception in COM, so you can’t even do that.  It *may* work if your program was C#, but that would depend on the Win32Exception class being able to ferret out the correct description string based on the HRESULT facility code for WIA.

    It would probably end up requiring giving FormatMessage a handle to the "main" WIA DLL (if there is such a thing), instead of using FORMAT_MESSAGE_FROM_SYSTEM.

  27. BryanK says:

    …Raymond can’t “just use images”.  He has nowhere to *upload* them.  (How many times have people said that now?)

    [And line art images look ugly when printed. -Raymond]
  28. 640k says:

    > Copy the source between the … and … to your hard drive and view it there, it works.

    > And line art images look ugly when printed.

    I would like to know how many prints this webpage and complains about the low resolution graphics. I don’t get it. Do you really think it’s better for everyone to manually copy html source code and creating a local file and then browse that, than an insignificant amount of people get a unnoticeable higher resolution in the rare occasions when printing this page?

    The drawings doesn’t display correct for anyone, the page looks corrupt and is hard to read. If you really want to use line art, at least use something like SVG which is supported by almost all browsers (in contrast with what you’re trying to use now, which isn’t supported by any).

    [It’s not so much the printing as it is the “large fonts” and high DPI settings. Images don’t scale. Line art does. But all this complaining has convinced me once again to give up on diagrams. -Raymond]
  29. Dave says:

    Oh yeah Win*32* came after OLE. Nevermind.

    BTW, here is your image.

    http://www.imagebee.org/images/4127tont-20061110.jpg

  30. Ben L says:

    I like to use coreldraw and export to swf. It just works. But that requires file hosting again.

    I’m glad I read the comments, otherwise I’d spend all evening wondering whats wrong with my ie7 install.

  31. Carlos says:

    The printing problem is easily solved.  Create PNGs at 4800 dpi; this should be enough for the glossiest magazine.  PNG compresses line art extremely well, so the file won’t be very large.  Of course, you’d still need somewhere to put it.

  32. Bob says:

    Maurits: Remember the time in 2001 when CNN.com got goatse’d by hotlinking to someone else’s image?

  33. - says:

    Of course you would create a low-res web version and keep the original high-res thing for printing if necessary. It doesn’t make sense to try to have print quality on the web.

    I’m 100% positive there are ways to embed images in HTML that work in almost every recent browser. Perhaps IE’s "save as HTML; single file" works, I can’t test right now (it will surely work for IE and chances are it’ll work for the others).

    Oh! Look here!:

    http://www.bennherrera.com/EmbeddedImage/

    It’s a crazy way to do it, but it does work… using tables! Yet it has RLE compression, and blogs.msdn.com uses gzip compression (that would work on top of that), so I think it would work rather well for diagrams and not-too-huge images.

  34. Mark Steward says:

    But Raymond apparently runs with scripting disabled, and that page’s code is even less understandable for someone without a browser…

  35. A says:

    > …Raymond can’t “just use images”.  He has nowhere to *upload* them.

    Nonsense. http://imageshack.us/. Select your image, click the button, and get an embeddable URL back. What could be easier?

    [There is no service level agreement if I use a free iamge hosting service. The site could vanish tomorrow. (Besides, I just don’t like images.) -Raymond]
  36. image storage says:

    Why can’t you use your employer’s website for storing images?

    http://groups.msn.com/_Secure/0SAAAAJ4WA00uz84t7DzYaoJSm4EASCQ0UpcsNa740jk5*eQ*xMJ6KkzxvHK*4YF9hRyF6CIIwvQdtfUgCL7svJA8JlTpqSt3ZCCOEm9x7d0AAAAAAAAAAA/win32_hresult.png

    [That’s paid for out of a different budget from the blogging servers. I used to have a place on the blogging servers to upload images to (so it came out of the correct budget) but that got shut down a long time ago. But the main reason is that images don’t fit into my content management system. (Besides, I just don’t like images for line art. I learned my lesson back when I did that spam graph. Organizations asked for “a higher resolution version that we can reprint” and there wasn’t one since it was just an image.) Line art should be done with lines, not pixels. -Raymond]
  37. R. Bemrose says:

    I would recommend SVG, but I’m not sure if IE supports them yet.

  38. stegus says:

    Back on topic:

    As I have said before, the problem descibed here has nothing to do with the macro HRESULT_FROM_WIN32. That macro was not involved at all in this problem.

    Instead, the real problem was that the person who wrote the error handling code did not understand how a HRESULT works.

    You can only extract an win32 error code from a HRESULT with FACILITY=FACILITY_WIN32.

    In this case, the facility was FACILITY_WIA, so he would have to look up the error message in the WIA documentation.

    Raymond, this is a common and important problem. Many developers do not understand how HRESULTs work.

    It is a pity that you have failed to give a correct description of the problem so people could have learned something.

  39. Igor says:

    Stop bitching about diagram and give Raymond a break!

    If you can’t visualize things without diagram then you should not be a developer. Period.

  40. stb says:

    [It’s not so much the printing as it is the "large fonts" and high DPI settings. Images don’t scale. Line art does. But all this complaining has convinced me once again to give up on diagrams. -Raymond]

    It seems kind of ridiculous for you to think the lesson in this debacle is "don’t use diagrams", as opposed to "don’t use poorly supported proprietary methods for displaying diagrams that pretty much nobody will be able to view the diagram with".

    If you’re so concerned about scaling an image up, why not just make the image in a vector graphics program and then export it to a GIF or JPEG? You’d have all the scalability you want if you ever wanted to go back and re-render it larger for print, and yet you’d be able to follow the standards that pretty much everyone on the web uses for displaying images.

  41. stb says:

    [It’s not so much the printing as it is the “large fonts” and high DPI settings. Images don’t scale. Line art does. But all this complaining has convinced me once again to give up on diagrams. -Raymond]

    It seems kind of ridiculous for you to think the lesson in this debacle is “don’t use diagrams”, as opposed to “don’t use poorly supported proprietary methods for displaying diagrams that pretty much nobody will be able to view the diagram with”.

    If you’re so concerned about scaling an image up, why not just make the image in a vector graphics program and then export it to a GIF or JPEG? You’d have all the scalability you want if you ever wanted to go back and re-render it larger for print, and yet you’d be able to follow the standards that pretty much everyone on the web uses for displaying images.

    [And then for people who are running high DPI or large web fonts the picture comes out the wrong size or blurry. My dislike for line art images is just one of my quirks. It’s vector graphics or nothing, and it looks like people are voting for nothing. -Raymond]
  42. Nick says:

    >> It’s vector graphics or nothing, and it looks like people are voting for nothing. -Raymond

    Raymond:

    I appreciate the time you put into these entries, and I enjoy reading them each morning.

    I don’t think "people are voting for nothing", however it does appear that VML isn’t working for *anyone* now.  I understand the problems with raster art, but it appears that short of uploading a file somewhere, your only option is SVG. While it may not be preferred, this at least works for everyone (IE users do need an Adobe ActiveX control).

    I hope you continue to provide graphics in the future, it’s just too bad that it’s not easier for you to do so.

  43. BryanK says:

    Wasn’t the issue with SVG that the DOCTYPE didn’t work with it before?  It seems that the DOCTYPE has changed now (or something); it might be worth a try at least.

    (I’d try SVG out in this comment, but I severely doubt it would be accepted.  Actually I’m pretty sure *no* markup is accepted, SVG or otherwise.)

  44. > It’s vector graphics or nothing, and it looks like people are voting for nothing

    o_O

    Do what you want, it’s your blog.  But right now the diagrams aren’t showing up for anyone, which is a poor user experience.

    What if this was an interview question? How would you develop a solution with these requirements:

    * No way for users to get sneak previews

    * Easy on the viewer

    * Easy on the blogger

    * Compatible with Raymond’s content management system

    * Scalable (i.e., vector graphics)

  45. James says:

    Maurits: I’d upload them as both SVG and PNG files (probably with automated generation of the latter from the former – a one-off script), in randomly named directories (I tend to use the MD5 hash of a few K of /dev/random), in a subdirectory which disallows directory indexing.

    Short of guessing 128 bit numbers, no sneak previews; easy on the viewer (it’s just an IMG tag), easy on the blogger (probably 2-3 mouse clicks to upload, then cut&paste the URL into an IMG tag) and scalable provided you have SVG support.

  46. BryanK says:

    James: One of the requirements was “compatible with the CMS” — this
    particular CMS is horribly broken (IMO) in that it does not allow users
    to upload anything except articles.

    I’m not sure there *is* a solution, short of asking the blog
    software authors to add the ability to upload small additional files
    (and put them in randomly-named directories, of course)…

    [The content management system is a directory with
    a bunch of files each named after the scheduled posting date and a
    script that uploads them via this interface. Maybe there’s an interface that lets me upload attachments but I can’t find it. -Raymond
    ]
  47. James says:

    BryanK: It would work fine with that CMS: you upload the files beforehand, when writing the article. (Since the URL isn’t guessable, having it uploaded beforehand isn’t a problem unless you go and change the image – in which case, just upload the new version.) Once the article itself – containing references to the secret URL – gets uploaded, people see the images as required.

  48. BryanK says:

    Oh, I figured "CMS" meant "the blog software".  OK, I get it.

    James: So what happens when the image provider site goes down?  As Raymond has said before, "there is no service-level agreement; the site could vanish tomorrow".

    To fix this right, Raymond needs one of the following:

    (a) Support for data: URIs in IE (to be able to embed an image in HTML) — but note that images are the Wrong Solution.  data: URIs containing SVG might work though, if browsers support that.

    (b) Better support for inline SVG in browsers (I went and looked up how to do that after I suggested it; it’s a major PITA, and requires sending a different Content-Type: header to Firefox versus IE, because IE doesn’t understand XML at all, and Firefox won’t embed SVG in plain HTML) — this would be best — or:

    (c) Somewhere on the blog site to upload files to — but again, images are the Wrong Solution.  Uploading SVG files should be possible, so that may work.

    None of these will happen immediately (certainly not anything that requires changes to IE… sigh).  And maybe somebody has already made a feature request to whoever wrote the blog software, so maybe that’s coming, but I don’t know for sure.

  49. Dean Harding says:

    Actually, a simpler solution would simply be if blogs.msdn.com gave its bloggers FTP access to their "media" directory. That way Raymond could update his CMS-upload script thingy to simply FTP the images before posting the article.

  50. Dean Harding says:

    Oops, I didn’t notice that my suggestion is the same as BryanK’s (c) suggestion… I think getting the blog software updated is more likely than getting IE updated :)

  51. James says:

    “James: So what happens when the image provider site goes down?
     As Raymond has said before, “there is no service-level agreement;
    the site could vanish tomorrow”.”

    Since the SLA for blogs.msdn.com’s present image service appears to
    be a guarantee it is down now and will remain so indefinitely, I don’t
    see that as a step backwards anyway. Indeed, the blog itself has gone
    down more frequently than any commercial hosting provider I’ve seen –
    requiring better than that for the images strikes me as comparable to
    running a beta version of Windows Me on a Tandem NonStop triplex.

    [I’m concerned more about the hosting company
    going out of business. Right now, the continued existence of this web
    site depends on one company (Microsoft). If I hosted images externally,
    that would add a second dependency. -Raymond
    ]
  52. James says:

    “I’m concerned more about the hosting company going out of business.
    Right now, the continued existence of this web site depends on one
    company (Microsoft). If I hosted images externally, that would add a
    second dependency. -Raymond”

    If you use your own domain, moving hosting would be a pretty trivial
    exercise – update the DNS entry, put the files on some other server. Of
    course, uploading to the existing /photos/ area should avoid that issue
    anyway…

    [Except that people can just browse to /photos/
    and see all the pictures for upcoming articles. This violates the
    ‘can’t guess the URL before the article is publsihed’ rule. And even if
    I used my own domain, that would still be a second dependency, namely
    me. I don’t want to be a system administrator. -Raymond
    ]
  53. Stefan Kanthak says:

    "Not to mention that that VML in this post doesn’t render in ANY browser."

    IE5.01SP4 renders it!

    nuff said…

  54. James says:

    Update: Bryan, the blogs.msdn.com server *does* support uploading
    images (under the /photos/ area) – I don’t know what the exact rules
    are, or how the interface operates, but the feature is there.

    If it accepts SVG, that should be sufficient for everyone. PNG would
    be better for everybody else, of course, but SVG would still be an
    improvement over the empty space we have now…

    [If I put the image into the Gallery, anybody can
    see it before the entry is published by just going to the Gallery. And
    I can’t figure out how to get metaWeblog.newMediaObject to work. Once I
    figure that out I’ll have to rewrite my autoposter and previewer. Right
    now, I take advantage of the fact that no image URLs change as part of
    the posting process. Uploading the images as part of the post means
    that the autoposter is going to have to go in and edit the article on
    the fly. All this work for the two times a year that I post images?
    -Raymond
    ]
  55. I had an idea that would allow embedding the VML in the post, but it would also require javascript.  The idea is to have a "Diagram" link (suitably titled) which, when you click on it, spawns a new window via window.open(…) and document.write(…)s the VML to it.

    One catch is IE won’t render javascript on local files, so this would be rather difficult to test.  But I’ll see if I can put together a working model.

  56. Working model posted:

    http://www.geocities.com/mvaneerde/vml.html

    Post and diagram contained in the same file.  Works locally with blogs.msdn.com doctype.  Works on GeoCities – not a true test because Yahoo! adds <script> spew to the front and end of the page, which may throw IE into Quirks mode.

    But I think this should work.

  57. Re: <button> – there’s a type="button|submit|reset" which specifies whether clicking the button is supposed to:

    button => no default behavior

    submit => submit the form

    reset => reset the form to on-page-load values

    There’s a bit of a disagreement as to what the default type is (that is, <button> with no specified type.)

    The HTML spec says that the default type should be submit:

    http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.5

    But MSDN says that the default type is button:

    http://msdn.microsoft.com/workshop/author/dhtml/reference/properties/type_3.asp

    So <button type="button"> is the safe thing to do.

  58. BryanK says:

    Maurits, that works here as well, on a local web server.  (The web server’s in IE6’s "Intranet" zone, though, so if the zone might affect things, that may cause issues.  I don’t think it will, though.)

    One suggestion: Maybe use a button instead of a link for the new-window version?  That way it’s a *little* more obvious to the user that they’re doing something with script.  <input type="button" id="whatever" onclick="whatever();" value="Click to see diagram in a new window (requires IE)" /> should work.  (As long as it’s not in a <form>, the button shouldn’t try to auto-submit anything.  Even if it is in a <form>, I don’t think that type="button" will auto-submit, but I’m not sure on that.)

    I’m not sure what to do about the same-window version; I can’t decide if a link or a button is more appropriate there.

    And of course, the whole thing needs to be cleaned up to be valid XHTML Frameset, but then again, you can’t get valid XHTML with VML included anyway.  So if it’s already a lost cause, that’s probably not that huge of a problem.

    Anyway, good idea!  None of that should require any changes to the headers or DOCTYPE or <head> element, or anything like that (it’s all self-contained), so it should at least be possible.

Comments are closed.

Skip to main content