{"id":28,"date":"2014-07-19T15:18:51","date_gmt":"2014-07-19T14:18:51","guid":{"rendered":"http:\/\/banfi.co.uk\/blog\/?p=28"},"modified":"2014-07-19T15:19:08","modified_gmt":"2014-07-19T14:19:08","slug":"get-current-logged-on-username-vb-net","status":"publish","type":"post","link":"https:\/\/banfi.co.uk\/blog\/2014\/07\/get-current-logged-on-username-vb-net\/","title":{"rendered":"Get Current logged on Username &#8211; vb.net &#8211; using advapi32.dll"},"content":{"rendered":"<p>Note this codes about 6 years old as of 2014 &#8211; I&#8217;m sure there are better ways of doing this now but this worked at the time and is still live in one of my projects.<\/p>\n<p>I needed to identify the currently logged on user running my application to make an audit file and also show only user options that the logged on user was setup for.<\/p>\n<p>Here&#8217;s the code&#8230;<\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\nDeclare Function GetUserName Lib &quot;advapi32.dll&quot; Alias _\r\n&quot;GetUserNameA&quot; (ByVal lpBuffer As String, _\r\nByRef nSize As Integer) As Integer\r\n\r\n   Public Function GetUserName() As String\r\n        'Gets currently logged on user - username\r\n        Dim iReturn As Integer\r\n        Dim userName As String\r\n        'put the username in a string limit 50 chars\r\n        userName = New String(CChar(&quot; &quot;), 50)\r\n        'put the length of the username in an Integer iReturn\r\n        iReturn = GetUserName(userName, 50)\r\n        'Tidy up various formatting if required with the replace function like remove the .\r\n        userName = userName.Replace(&quot;.&quot;c, &quot; &quot;c)\r\n        \r\n        'Return the username back\r\n        GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))\r\n\r\n    End Function\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Note this codes about 6 years old as of 2014 &#8211; I&#8217;m sure there are better ways of doing this now but this worked at the time and is still live in one of my projects. I needed to identify the currently logged on user running my application to make an audit file and also &hellip; <a href=\"https:\/\/banfi.co.uk\/blog\/2014\/07\/get-current-logged-on-username-vb-net\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Get Current logged on Username &#8211; vb.net &#8211; using advapi32.dll<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4,5,3],"tags":[],"class_list":["post-28","post","type-post","status-publish","format-standard","hentry","category-net","category-vb-net","category-visual-basic"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/posts\/28","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=28"}],"version-history":[{"count":2,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/posts\/28\/revisions"}],"predecessor-version":[{"id":30,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/posts\/28\/revisions\/30"}],"wp:attachment":[{"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=28"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=28"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/banfi.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=28"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}