{"id":3763,"date":"2013-07-19T07:00:00","date_gmt":"2013-07-19T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/07\/19\/nasty-gotcha-stgm_read-stgm_write-does-not-grant-readwrite-access\/"},"modified":"2013-07-19T07:00:00","modified_gmt":"2013-07-19T07:00:00","slug":"nasty-gotcha-stgm_read-stgm_write-does-not-grant-readwrite-access","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130719-00\/?p=3763","title":{"rendered":"Nasty gotcha: STGM_READ &#124; STGM_WRITE does not grant read\/write access"},"content":{"rendered":"<p>You might think that if you want to get read\/write access, you could pass <code>STGM_READ | STGM_WRITE<\/code>. You would be wrong. You have to pass <code>STGM_READ&shy;WRITE<\/code>.\n The three flags <code>STGM_READ<\/code>, <code>STGM_WRITE<\/code>, and <code>STGM_READ&shy;WRITE<\/code> are mutually exclusive. If you try to combine them, you get a weird mess.\n In particular, since the numerical value of <code>STGM_READ<\/code> is zero, passing <code>STGM_READ | STGM_WRITE<\/code> is numerically equivalent to passing <code>STGM_WRITE<\/code>, which grants write-only access.\n The documentation for the <code>STGM_*<\/code> constants specifically says &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa380337(v=vs.85).aspx\">It is not valid to use more than one element from a single group<\/a>,&#8221; and <code>STGM_READ<\/code> and <code>STGM_WRITE<\/code> belong to the Access group (as does <code>STGM_READ&shy;WRITE<\/code>).\n These values date back to the days of MS-DOS, where function 3Dh (Open File) passed an access mode in the AL register.<\/p>\n<table border=\"1\" style=\"border-collapse: collapse\">\n<tr>\n<th>7<\/th>\n<th>6<\/th>\n<th>5<\/th>\n<th>4<\/th>\n<th>3<\/th>\n<th>2<\/th>\n<th>1<\/th>\n<th>0<\/th>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td>0<\/td>\n<td colspan=\"3\">access<br \/>mode<\/td>\n<\/tr>\n<\/table>\n<p> The bottom three bits specified the requested access (0 = read-only, 1 = write-only, 2 = read\/write), and the remaining bits were reserved.\n Later, when networking support was added in approximately MS-DOS 3.5, three more bits were pressed into service:<\/p>\n<table border=\"1\" style=\"border-collapse: collapse\">\n<tr>\n<th>7<\/th>\n<th>6<\/th>\n<th>5<\/th>\n<th>4<\/th>\n<th>3<\/th>\n<th>2<\/th>\n<th>1<\/th>\n<th>0<\/th>\n<\/tr>\n<tr>\n<td>0<\/td>\n<td colspan=\"3\">share<br \/>mode<\/td>\n<td>0<\/td>\n<td colspan=\"3\">access<br \/>mode<\/td>\n<\/tr>\n<\/table>\n<p> Sharing modes were 0 = compatibility mode, 1 = deny all, 2 = deny write, 3 = deny read, 4 = deny none.<\/p>\n<p> These values were carried forward into Windows as flags to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa365430(v=vs.85).aspx\"> <code>Open&shy;File<\/code> function<\/a>: <\/p>\n<table border=\"1\" style=\"border-collapse: collapse\">\n<tr>\n<th>Value<\/th>\n<th>Description<\/th>\n<\/tr>\n<tr>\n<td><code>OF_READ<br \/>0x00000000<\/code><\/td>\n<td>Opens a file for reading only.<\/td>\n<\/tr>\n<tr>\n<td><code>OF_WRITE<br \/>0x00000001<\/code><\/td>\n<td>Opens a file for write access only.<\/td>\n<\/tr>\n<tr>\n<td><code>OF_READ&shy;WRITE<br \/>0x00000002<\/code><\/td>\n<td>Opens a file with read\/write permissions.<\/td>\n<\/tr>\n<tr>\n<td><code>OF_SHARE&shy;COMPAT<br \/>0x00000000<\/code><\/td>\n<td>Opens a file with compatibility mode, allows any process     on a specified computer to open the file any number of times.<\/td>\n<\/tr>\n<tr>\n<td><code>OF_SHARE&shy;EXCLUSIVE<br \/>0x00000010<\/code><\/td>\n<td>Opens a file with exclusive mode and denies both read\/write     ccess to other processes.<\/td>\n<\/tr>\n<tr>\n<td><code>OF_SHARE&shy;DENY&shy;WRITE<br \/>0x00000020<\/code><\/td>\n<td>Opens a file and denies write access to other processes.<\/td>\n<\/tr>\n<tr>\n<td><code>OF_SHARE&shy;DENY&shy;READ<br \/>0x00000030<\/code><\/td>\n<td>Opens a file and denies read access to other processes.<\/td>\n<\/tr>\n<tr>\n<td><code>OF_SHARE&shy;DENY&shy;NONE<br \/>0x00000040<\/code><\/td>\n<td>Opens a file without denying read or write access to other processes.<\/td>\n<\/tr>\n<\/table>\n<p> These flags were then carried forward into the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/aa380337(v=vs.85).aspx\"> STGM constants<\/a> with the same numerical values. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>You might think that if you want to get read\/write access, you could pass STGM_READ | STGM_WRITE. You would be wrong. You have to pass STGM_READ&shy;WRITE. The three flags STGM_READ, STGM_WRITE, and STGM_READ&shy;WRITE are mutually exclusive. If you try to combine them, you get a weird mess. In particular, since the numerical value of STGM_READ [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-3763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You might think that if you want to get read\/write access, you could pass STGM_READ | STGM_WRITE. You would be wrong. You have to pass STGM_READ&shy;WRITE. The three flags STGM_READ, STGM_WRITE, and STGM_READ&shy;WRITE are mutually exclusive. If you try to combine them, you get a weird mess. In particular, since the numerical value of STGM_READ [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=3763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3763\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=3763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}