Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Help Accessing the NETAPI.DLL

Posted on 1997-07-09
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.

e-mail:  macleao@ecf.toronto.edu

Accepted Solution

y96andha earned 600 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

596 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