Attribute VB_Name = "Windows" ' Copyright (C) 2011 Eric Ehlers ' ' This file is part of QuantLib, a free-software/open-source library ' for financial quantitative analysts and developers - http://quantlib.org/ ' ' QuantLib is free software: you can redistribute it and/or modify it ' under the terms of the QuantLib license. You should have received a ' copy of the license along with this program; if not, please email ' . The license is also available online at ' . ' ' This program is distributed in the hope that it will be useful, but WITHOUT ' ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ' FOR A PARTICULAR PURPOSE. See the license for more details. Option Explicit '-------------- For Versions Previous to Excel 2010 -------------------- 'Declare Function WNetGetUser Lib "mpr.dll" _ ' Alias "WNetGetUserA" (ByVal name As String, _ ' ByVal userName As String, bufLen As Long) As Long '-------------- For Versions After and Including Excel 2010 -------------------- Declare PtrSafe Function WNetGetUser Lib "mpr.dll" _ Alias "WNetGetUserA" (ByVal name As String, _ ByVal userName As String, bufLen As Long) As Long Function getUserName() As String Const bufLen As Integer = 255 Dim status As Integer Dim name, userName As String userName = Space$(bufLen + 1) status = WNetGetUser(name, userName, bufLen) If 0 = status Then ' This line removes the null character. Strings in C are null- ' terminated. Strings in Visual Basic are not null-terminated. ' The null character must be removed from the C strings to be used ' cleanly in Visual Basic. getUserName = Left$(userName, InStr(userName, Chr(0)) - 1) Else Call raiseError("Unable to derive windows login ID") End If End Function