Help Accessing the NETAPI.DLL

I am attempting to write a program to access and set user information on an NT server from either Win95 or NT workstation.  I am able to get some of the information (though not in the layout I expect)using NETUSERGETINFO, but have been continuously unsuccessful with NETUSERSETINFO.
If anyone knows any methods of calling these functions successfully from VB, or the meaning behind the returned error codes, it would be much appreciated.

I am using VB4-16bit in 95 accessing NETAPI.DLL, VB4-32bit in NT w/ NETAPI32.DLL
maclean070997Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

y96andhaCommented:
I have only used these functions successfully under Windows NT. What is your problem with the functions? I could look at your code and try to get it running if you'd like to post it here.

I have a header file containing definitions for the error codes which I will send to you if you can give me your e-mail address.
0
maclean070997Author Commented:
My problem is with passing and receiving Type User_Info_1,  I have decleared it as a bunch of longs (which become addresses), and attempted to dereference the addresses received, and place addresses for the set using the kernel32 function "lstrcpyW" declred two different ways.

e-mail:  macleao@ecf.toronto.edu
0
y96andhaCommented:

This is working code. As you have not posted your source, I cannot tell you what is wrong with it, but you can take this source and compare them. I have only tested it under Windows NT, VB4-32bit.


Private Type USER_INFO_1
    usri1_name As Long
    usri1_password As Long
    usri1_password_age As Long
    usri1_priv As Long
    usri1_home_dir As Long
    usri1_comment As Long
    usri1_flags As Long
    usri1_script_path As Long
End Type
Private Declare Function NetUserGetInfo Lib "netapi32" (ByVal servername As String, ByVal username As String, ByVal level As Long, usri1ptr As Long) As Long
Private Declare Function NetUserSetInfo Lib "netapi32" (ByVal servername As String, ByVal username As String, ByVal level As Long, usri1 As USER_INFO_1, parmerr As Long) As Long
Private Declare Function NetApiBufferFree Lib "netapi32" (ByVal buffer As Long) As Long
Private Declare Function MoveMemoryPP Lib "kernel32" Alias "RtlMoveMemory" (ByVal ptr1 As Long, ByVal ptr2 As Long, ByVal length As Long) As Long
Private Declare Function MoveMemoryLP Lib "kernel32" Alias "RtlMoveMemory" (lng1 As Long, ByVal ptr2 As Long, ByVal length As Long) As Long
Private Declare Function strcpy Lib "kernel32" Alias "lstrcpyW" (ByVal str1 As String, ByVal ptr2 As Long) As Long
Private Declare Function strcpy2 Lib "kernel32" Alias "lstrcpyW" (ByVal ptr1 As Long, ByVal str2 As String) As Long
Private Declare Function VarPtr Lib "vb40032" (var As Any) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal flags As Long, ByVal bytes As Long) As Long
Private Declare Sub GlobalFree Lib "kernel32" (ByVal ptr As Long)

Private Function getstring(ptr As Long) As String
    Dim strptr As Long
    Dim str As String
    MoveMemoryLP strptr, ptr, 4
    str = String(512, 0)
    strcpy str, strptr
    str = StrConv(str, vbFromUnicode)
    getstring = Left(str, InStr(str, Chr(0)) - 1)
End Function


Private Function getlong(ptr As Long) As Long
    Dim lng As Long
    MoveMemoryLP lng, ptr, 4
    getlong = lng
End Function


Private Function makestring(str As String) As Long
    Dim ptr As Long
    ptr = GlobalAlloc(0, Len(str) * 2 + 2)
    If ptr = 0 Then
        makestring = 0
        Exit Function
    End If
    strcpy2 ptr, StrConv(str & Chr(0), vbUnicode)
    makestring = ptr
End Function

Private Sub Command1_Click()
    Dim status As Long
    Dim bufptr As Long
    Dim ptr As Long
    Dim name As String, pwage As Long, priv As Long
    Dim homedir As String, comment As String, flags As Long, scriptpath As String
    Dim parmerr As Long
    status = NetUserGetInfo(StrConv("\\ANDREAS" & Chr(0), vbUnicode), _
        StrConv("testuser" & Chr(0), vbUnicode), _
        1, bufptr)
    Print "status:"; status
    If status = 0 Then
        name = getstring(bufptr)
        pwage = getlong(bufptr + 8)
        priv = getlong(bufptr + 12)
        homedir = getstring(bufptr + 16)
        comment = getstring(bufptr + 20)
        flags = getlong(bufptr + 24)
        scriptpath = getstring(bufptr + 28)
        Print "Name: "; name
        Print "Password age: "; pwage
        Print "Priv: "; priv
        Print "Home dir:"; homedir
        Print "Comment:"; comment
        Print "Flags: "; flags
        Print "Script path:"; scriptpath
        NetApiBufferFree bufptr
       
        Dim usri1 As USER_INFO_1
        usri1.usri1_name = makestring(name)
        usri1.usri1_home_dir = makestring(homedir)
        usri1.usri1_comment = makestring("New comment")
        usri1.usri1_script_path = makestring(scriptpath)
        usri1.usri1_password_age = pwage
        usri1.usri1_flags = flags
        usri1.usri1_priv = priv
        usri1.usri1_password = 0
        status = NetUserSetInfo(StrConv("\\ANDREAS" & Chr(0), vbUnicode), _
            StrConv("testuser" & Chr(0), vbUnicode), _
            1, usri1, parmerr)
           
        If status = 0 Then
            Print "Success"
        ElseIf status = 87 Then
            Print "Parameter " & parmerr & " is in error"
        Else
            Print "Error: " & status
        End If
        GlobalFree usri1.usri1_name
        GlobalFree usri1.usri1_home_dir
        GlobalFree usri1.usri1_comment
        GlobalFree usri1.usri1_script_path
    End If
   
End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.