Results 1 to 6 of 6

Thread: I need the help of a True Master :)

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    79

    Question

    OK everyone I have just pulled out my last strand of hair and I'm not a happy bloke.

    I'm trying to get my little program to go into the Windows NT registry and take the Username of the current user so that my program can make a log in the form of:

    "Username read the message at date and time"

    eg

    if my NT Boat ID is Paul:

    "Paul read the message at 26/9/00 21:00"


    But it isn't printng the Username, just the remainder of the message.

    Here's the code I've tried:


    'getting the username whenever the user is logged in a windows network or a novellnetwork

    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
    Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
    Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
    Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

    Private Const KEY_ALL_CLASSES As Long = &HF0063
    Private Const REG_SZ As Long = 1
    Private Const ERROR_SUCCESS = 0&

    Private Function LooseSpace(invoer$) As String
    Dim p%

    p% = InStr(invoer$, Chr(0))
    If p% <> 0 Then
    LooseSpace$ = Left$(invoer$, p% - 1)
    Exit Function
    End If
    LooseSpace$ = invoer$ 'used in getting the logged in username

    End Function

    Public Function GetNovellName() As String
    Dim res&, lpBuffer$, nSize&

    nSize = 8
    lpBuffer = String(8, 0)
    res& = GetUserName(lpBuffer, nSize)
    GetNovellName$ = LooseSpace(Left$(lpBuffer, nSize))

    'user is not logged on to a windows-network
    'the username is kept in the HKEY_LOCAL_MACHINE\NETWORK\LOGON\username 'on WIN 98 using this path works
    If GetNovellName$ = "" Then _
    GetNovellName = RegGetString$(&H80000002, "Software\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultUserName") 'this is the NTregistry ocation of the Username

    End Function

    Private Function RegGetString$(hInKey As Long, ByVal subkey$, ByVal valname$)
    Dim RetVal$, hSubKey As Long, dwType As Long, SZ As Long, v$, r As Long

    RetVal$ = ""

    r = RegOpenKeyEx(hInKey, subkey$, 0, KEY_ALL_CLASSES, hSubKey)
    If r <> ERROR_SUCCESS Then GoTo Quit_Now
    SZ = 256: v$ = String$(SZ, 0)
    r = RegQueryValueEx(hSubKey, valname$, 0, dwType, ByVal v$, SZ)
    If r = ERROR_SUCCESS And dwType = REG_SZ Then
    RetVal$ = Left(v$, SZ - 1)
    Else
    RetVal$ = ""
    End If
    If hInKey = 0 Then r = RegCloseKey(hSubKey)

    Quit_Now:
    RegGetString$ = RetVal$ 'defines regstring for geting logged in username

    End Function


    I've also tried:


    Private Declare Function win32_nlWNetGetUserA Lib "mpr.dll" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpUserName As String, lpnLength As Long) As Long

    Public Function duwinsys_sWNetGetUser() As String

    'Retrieve the logged-in username.

    Dim nPos As Integer
    Dim nlBufLength As Long
    Dim sUserId As String
    Dim sBuf As String

    sUserId = ""
    sBuf = Space$(256)
    nlBufLength = Len(sBuf)

    If win32_nlWNetGetUserA("", sBuf, nlBufLength) = 0 Then
    nPos = InStr(sBuf, Chr$(0))
    If nPos > 0 Then
    sUserId = Left$(sBuf, nPos - 1)
    Else
    sUserId = sBuf
    End If
    End If

    duwinsys_sWNetGetUser = sUserId

    But this also fails to put in the Username


    I have also been told that our NT system has a system varialble called %username%, which, in NT script automatically gets the logged in username, but me being a complete novice, can't work out how to use this in my program.


    Please help me try to get this sorted out, it the last part of this project that I'm working on and its the part thats giving me the most grief.

    Any help would be most gratefully appreciated by this stuck newbie


    Cheers


    Paul
    The problem with designing something completely foolproof is to underestimate the ingenuity of a complete fool. - Douglas Adams

    I know the human being and fish can coexist peacefully. - GWB

    I think we agree, the past is over. - GWB

  2. #2
    Member
    Join Date
    Jan 2000
    Location
    Quantico, VA, USA
    Posts
    41

    Cool Try this...

    All right, quick explanation. The registry returns values using old-school string type expressions (I don't remember the technical term - but each byte is separated by a null (chr(0)). So you've got to use every other byte, and you've still got to test for the end of the string which is a chr(0). Not only that, your buffer was too small. Other than that, good initiative :-)


    Public Declare Function GetUserNameW Lib "advapi32.dll" (lpBuffer As Byte, nSize As Long) As Long

    'this parses the buffer into a legible string
    Public Function ParseName(sName() As Byte) As String
    Dim strHolder As String
    strHolder = ""
    For i = 0 To UBound(sName)
    If (i Mod 2 = 0) And (sName(i) > 0) Then strHolder = strHolder & Chr(sName(i))
    Next i
    ParseName = Trim$(strHolder)
    End Function


    Public Function UserName() As String
    Dim sName() As Byte
    ReDim sName(255)
    GetUserNameW sName(0), 255
    UserName = ParseName(sName)
    End Function
    To err is human, but to apologize frequently is embarassing.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    79

    Talking IT WORKS!!!!!!!!!!

    Cheers very musck Chuck!!!!!


    Its working !!!!!!



    Much appreciated
    The problem with designing something completely foolproof is to underestimate the ingenuity of a complete fool. - Douglas Adams

    I know the human being and fish can coexist peacefully. - GWB

    I think we agree, the past is over. - GWB

  4. #4
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Re: Try this...

    Originally posted by Chuck Sweet
    The registry returns values using old-school string type expressions (I don't remember the technical term - but each byte is separated by a null (chr(0)).
    Unicode strings, or DBCS/MBCS (Double/Multi Byte Character Set) strings, or wide strings.

    They aren't always separated with Chr(0). The 2nd byte says something about the 1st byte (for example, what language it is). If it's zero, the 1st byte is completely normal.

    I think.

  5. #5
    Member
    Join Date
    Jan 2000
    Location
    Quantico, VA, USA
    Posts
    41

    Talking

    Good to know :-)
    chuck
    To err is human, but to apologize frequently is embarassing.

  6. #6
    Lively Member
    Join Date
    Aug 2000
    Location
    quebec
    Posts
    81

    Arrow

    Great. But I tried your second example with an added:
    Code:
    Private Sub Command1_Click()
        MsgBox duwinsys_sWNetGetUser & " read the message at 26/9/00 21:00"
    End Sub
    and had no problem. Maybe I didn't get what you really wanted to do?

    Hint. Check the FAQ sheet at the top of the forum to see how to format you post's code inserts, add smilies ,bold, italics etc. as above.

    Don't forget to indent your code for readability.

    have fun
    C/C++,Delphi,VB6,Java,PB (blech!),ASP,JSP,SQL...bla bla bla and bla
    I love deadlines. I like the whooshing sound they make as they fly by.
    —Douglas Adams

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width