{"id":94055,"date":"2016-08-10T07:00:00","date_gmt":"2016-08-10T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=94055"},"modified":"2019-03-13T11:05:18","modified_gmt":"2019-03-13T18:05:18","slug":"20160810-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20160810-00\/?p=94055","title":{"rendered":"How to create a file mapping that allows others to open the file in exclusive mode?"},"content":{"rendered":"<p>A customer had a tool that opens files like DLLs and TLBs in order to extract information from them. <\/p>\n<blockquote CLASS=\"q\">\n<p>We currently use <code>Create&shy;File<\/code>, <code>Create&shy;File&shy;Mapping<\/code>, and <code>Map&shy;View&shy;Of&shy;File<\/code> to access the file. The problem is that the <code>Create&shy;File&shy;Mapping<\/code> prevents the file from being renamed or deleted, even if the <code>Create&shy;File<\/code> opened the file with a sharing mode that permits those operations. It is our understanding that this is expected behavior. <\/p>\n<p>Since DLLs and TLBs under active development are frequently deleted or overwritten, our use of <code>Create&shy;File&shy;Mapping<\/code> interferes with developer workflow because the user&#8217;s build will fail with a sharing violation. We were wondering if there is an alternative to <code>Create&shy;File&shy;Mapping<\/code> that would allow the file to be renamed, deleted, or written to. We know that we could slurp the entire file into memory and operate on the in-memory copy, but we were hoping for something less drastic. <\/p>\n<\/blockquote>\n<p>Yes, there is something less drastic. In fact, this is the type of scenarios for which opportunistic locks were created: You want to access a file, but you don&#8217;t want your access to interfere with anybody else who wants to access the file after you get access. <\/p>\n<p>For a code sample, I defer to <a HREF=\"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/20130415-00\/?p=4663\">my earlier discussion of opportunistic locks<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is a job for opportunistic locks.<\/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-94055","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>This is a job for opportunistic locks.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94055","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=94055"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/94055\/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=94055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=94055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=94055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}