Help Accessing the NETAPI.DLL

Posted on 1997-07-09
Last Modified: 2012-06-27
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
Question by:maclean070997
  • 2

Expert Comment

ID: 1428153
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.

Author Comment

ID: 1428154
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.


Accepted Solution

y96andha earned 150 total points
ID: 1428155

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"
            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


Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now