WinAPI question

J

Jim Covington

I want to add a "Domain\Domain Admins" global group to each pc's Local
Administrator group.

There is a WinAPI "NetLocalGroupAdd" that does this.

I found some code as follows but I get an error:

'------------------------------------------------
Private Structure Group_Info_1
Dim lgrpi1_name As String
Dim lgrpi1_comment As String
End Structure
'-----------------------------------------------
Private Declare Function NetLocalGroupAdd _
Lib "netapi32.dll" _
(ByVal ServerName As String, _
ByVal Level As Long, _
ByVal Buffer As Group_Info_1, _
ByVal parm_err As Long) As Long
'-----------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim lRetVal As Long 'result of the API functions
Dim hKey As Long 'handle of opened key
Dim vValue As Object 'setting of queried value
Dim Name As Group_Info_1 'setting of local group name
' and comment
Dim bServer As String 'null terminated string
' containing server name

Dim sNameLen As String 'setting of variables for length
Dim sCommentLen As String ' of local group name and
' comment for ANSI to Unicode
' conversion

' ---------------------------------------------
' Conversion of 1 bit ansi VB output to the 2 bit
' unicode required by NT/W2K
' ---------------------------------------------

sNameLen = Len("MyDomain\Domain Admins")
sCommentLen = Len("MyDomain Administrators")

'Do While Not sNameLen = 0

'Name.lgrpi1_name = Mid("MyDomain\Domain Admins", sNameLen, 1) &
_
' vbNullChar & Name.lgrpi1_name

'sNameLen = sNameLen - 1
' Loop
Name.lgrpi1_name = "MyDomain\Domain Admins" & vbNullChar
'Do While Not sCommentLen = 0

'Name.lgrpi1_comment = Mid("MyDomain\Domain Admins",
sCommentLen, 1) & _
' vbNullChar & Name.lgrpi1_comment

'sCommentLen = sCommentLen - 1
'Loop
' Name.lgrpi1_comment = "MyDomain\Domain Admins" & vbNullChar

bServer = "\\MyPC" & vbNullChar 'null terminated
'string

' ---------------------------------------------
' Local Group Creation
' ---------------------------------------------
Try
lRetVal = NetLocalGroupAdd(bServer, 1, Name, 0)

Catch ex As Exception
Dim Event1 As New EventLog
Event1.Log = "Application"
Event1.Source = "QuickCheck"
Event1.WriteEntry("Error in NetLocalGroupAdd. Message is: "
& Chr(13) & ex.ToString)
End Try

MsgBox("Function Complete")

End Sub
'-----------------------------------------------------
Whin I execute the Procedure, I get an error in my eventlog
as follows:

Error in NetLocalGroupAdd. Message is:
System.NullReferenceException: Object reference not set to an instance
of an object.
at LogonOnUser.Form1.NetLocalGroupAdd(String& ServerName, Int64
Level, Group_Info_1 Buffer, Int64 parm_err)
at LogonOnUser.Form1.Button1_Click(Object sender, EventArgs e) in
E:\VisualStudio\LOGONUser\LogonOnUser\Form1.vb:line 184

Can anyone help?

JIm

YBraker
 
I

Imran Koradia

Try changing the parameter 'Level' to integer. I believe DWORD converts to
Int32 (Integer).
Also, make the return type as Integer since NET_API_STATUS is also defined
as a DWORD.

hope that helps..
Imran.
 
H

Herfried K. Wagner [MVP]

* Jim Covington said:
There is a WinAPI "NetLocalGroupAdd" that does this.

I found some code as follows but I get an error:

'------------------------------------------------
Private Structure Group_Info_1
Dim lgrpi1_name As String
Dim lgrpi1_comment As String
End Structure
'-----------------------------------------------
Private Declare Function NetLocalGroupAdd _
Lib "netapi32.dll" _
(ByVal ServerName As String, _
ByVal Level As Long, _

Replace 'As Long' with 'As Int32'.
ByVal Buffer As Group_Info_1, _
ByVal parm_err As Long) As Long
Dito.

Dim lRetVal As Long 'result of the API functions
Dim hKey As Long 'handle of opened key
Dito.

Dim vValue As Object 'setting of queried value

Why 'Object'?
Name.lgrpi1_name = "MyDomain\Domain Admins" & vbNullChar

Explicit null-termination is in general not required.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

System icons in Listview 6
Problem with WinAPI 1
Please Help me Convert This VB60 Code 1
IsRemoteAdmin function 1
BackgroundWorker thread locking UI 3
ldap lookup from window form 2
VB6 to VB.NET 3
File Owner 2

Top