{"id":1608,"date":"2024-11-26T22:05:39","date_gmt":"2024-11-26T14:05:39","guid":{"rendered":"http:\/\/www.jusesgod.com\/?p=1608"},"modified":"2024-11-27T09:27:46","modified_gmt":"2024-11-27T01:27:46","slug":"%e8%8e%b7%e5%8f%96userprincipalname","status":"publish","type":"post","link":"http:\/\/www.jusesgod.com\/?p=1608","title":{"rendered":"\u83b7\u53d6UserPrincipalName"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\nPrivate Declare PtrSafe Function GetUserNameEx Lib &quot;secur32.dll&quot; Alias &quot;GetUserNameExA&quot; ( _\n    ByVal nameFormat As Long, _\n    ByVal nameBuffer As String, _\n    ByRef nameSize As Long) As Long\n \nPrivate Declare PtrSafe Function TranslateName Lib &quot;secur32.dll&quot; Alias &quot;TranslateNameA&quot; ( _\n    ByVal lpName As String, _\n    ByVal nameFormat As Long, _\n    ByVal desiredFormat As Long, _\n    ByVal translatedName As String, _\n    ByRef lpnLength As Long) As Long\n \nPrivate Const NAME_FORMAT_USER_PRINCIPAL_NAME = &amp;H2\nPrivate Const TRANSLATED_NAME_FORMAT_CANONICAL = &amp;H1\nPrivate Const TRANSLATED_NAME_USER_PRINCIPAL = &amp;H8\n \nPublic Function GetUserPrincipalName() As String\n    Dim userNameBuffer As String * 256\n    Dim userNameSize As Long\n    Dim translatedNameBuffer As String * 256\n    Dim translatedNameSize As Long\n    Dim result As Long\n    \n    userNameSize = 256\n    result = GetUserNameEx(NAME_FORMAT_USER_PRINCIPAL_NAME, userNameBuffer, userNameSize)\n    \n    If result Then\n        translatedNameSize = 256\n        result = TranslateName(userNameBuffer, NAME_FORMAT_USER_PRINCIPAL_NAME, TRANSLATED_NAME_USER_PRINCIPAL, translatedNameBuffer, translatedNameSize)\n        \n        If result Then\n            GetUserPrincipalName = Left$(translatedNameBuffer, InStr(translatedNameBuffer, Chr$(0)) - 1)\n        Else\n            GetUserPrincipalName = &quot;&quot;\n        End If\n    Else\n        GetUserPrincipalName = &quot;&quot;\n    End If\nEnd Function\n\nPublic Sub show()\n    MsgBox GetUserPrincipalName\nEnd Sub\n<\/pre><\/div>\n\n\n<p>\u6d4b\u8bd5\u6210\u529f\uff0c\u53ef\u4ee5\u663e\u793a\u7528\u6237\u90ae\u7bb1\u683c\u5f0f\u7684User Principal Name<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"645\" height=\"482\" src=\"http:\/\/www.jusesgod.com\/wp-content\/uploads\/2024\/11\/image.png\" alt=\"\" class=\"wp-image-1613\" srcset=\"http:\/\/www.jusesgod.com\/wp-content\/uploads\/2024\/11\/image.png 645w, http:\/\/www.jusesgod.com\/wp-content\/uploads\/2024\/11\/image-300x224.png 300w\" sizes=\"auto, (max-width: 645px) 100vw, 645px\" \/><\/figure>\n\n\n\n<p>\u53c2\u8003\u6587\u732e\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/zh-cn\/windows\/win32\/ad\/naming-properties?redirectedfrom=MSDN\">\u7528\u6237\u547d\u540d\u5c5e\u6027 &#8211; Win32 apps | Microsoft Learn<\/a>  api\u67e5\u770b<\/p>\n\n\n\n<p><a href=\"https:\/\/learn.microsoft.com\/zh-tw\/windows\/win32\/api\/secext\/ne-secext-extended_name_format\">EXTENDED_NAME_FORMAT (secext.h) &#8211; Win32 apps | Microsoft Learn<\/a>   \u7ffb\u8bd1\u65b9\u6cd5\u7684\u53c2\u6570<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6d4b\u8bd5\u6210\u529f\uff0c\u53ef\u4ee5\u663e\u793a\u7528\u6237\u90ae\u7bb1\u683c\u5f0f\u7684User Princ&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":1553,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[],"class_list":["post-1608","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vba"],"_links":{"self":[{"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/posts\/1608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1608"}],"version-history":[{"count":3,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/posts\/1608\/revisions"}],"predecessor-version":[{"id":1614,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/posts\/1608\/revisions\/1614"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=\/wp\/v2\/media\/1553"}],"wp:attachment":[{"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1608"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jusesgod.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}