{"id":110251,"date":"2024-09-12T07:00:00","date_gmt":"2024-09-12T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=110251"},"modified":"2024-09-12T10:12:36","modified_gmt":"2024-09-12T17:12:36","slug":"20240912-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240912-00\/?p=110251","title":{"rendered":"How can I tell whether two programs will share drive letter mappings?"},"content":{"rendered":"<p>A customer wanted to know how to determine whether two programs will share drive letter mappings. If one program creates a drive letter mapping, say via <code>WNetAddConnection3<\/code>, will it be visible to the other program?<\/p>\n<p>This information is documented under the topic &#8220;<a href=\"https:\/\/learn.microsoft.com\/windows\/win32\/fileio\/defining-an-ms-dos-device-name\">Defining an MS-DOS Device Name<\/a>&#8220;. There are two types of drive letter mappings: Global and Local. Only processes running under the LocalSystem account can manipulate Global drive letter mappings. Everybody else manipulates the Local mapping that is associated with its token&#8217;s authentication ID.<\/p>\n<p>So how do you get the authentication ID? You ask for the <code>Token\u00adStatistics<\/code>.<\/p>\n<pre>#include &lt;wil\/token_helpers.h&gt;\r\n\r\nLUID GetAuthenticationIdForCurrentThread()\r\n{\r\n    wil::get_token_information&lt;TOKEN_STATISTICS&gt;().AuthenticationId;\r\n}\r\n\r\nLUID GetAuthenticationIdForProcess(HANDLE process)\r\n{\r\n    wil::unique_handle token;\r\n    THROW_IF_WIN32_BOOL_FALSE(OpenProcessToken(process, TOKEN_QUERY, token.put()));\r\n    return wil::get_token_information&lt;TOKEN_STATISTICS&gt;(token.get()).AuthenticationId;\r\n}\r\n<\/pre>\n<p>If you&#8217;d rather not use wil, or if you just want to see how it works inside:<\/p>\n<pre>HRESULT GetAuthenticationIdForToken(HANDLE token, LUID* luid)\r\n{\r\n    HRESULT hr;\r\n    *luid = {};\r\n\r\n    TOKEN_STATISTICS stats;\r\n    DWORD length;\r\n    if (GetTokenInformation(token, TokenStatistics,\r\n            &amp;stats, sizeof(stats), &amp;length)) {\r\n        *luid = stats.AuthenticationId;\r\n        hr = S_OK;\r\n    } else {\r\n        hr = HRESULT_FROM_WIN32(GetLastError());\r\n    }\r\n    return hr;\r\n}\r\n\r\nHRESULT GetAuthenticationIdForCurrentThread(LUID* luid)\r\n{\r\n    return GetAuthenticationIdForToken(\r\n            GetCurrentThreadEffectiveToken(), luid);\r\n}\r\n\r\nBOOL GetAuthenticationIdForProcess(HANDLE process, LUID* luid)\r\n{\r\n    HRESULT hr;\r\n    *luid = {};\r\n\r\n    HANDLE token;\r\n    if (OpenProcessToken(process, TOKEN_QUERY, &amp;token)) {\r\n        hr = GetAuthenticationIdForToken(token, luid);\r\n        CloseHandle(token);\r\n    } else {\r\n        hr = HRESULT_FROM_WIN32(GetLastError());\r\n    }\r\n\r\n    return hr;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>You can compare the authentication IDs.<\/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-110251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can compare the authentication IDs.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110251","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=110251"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/110251\/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=110251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=110251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=110251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}