By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,234 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,234 IT Pros & Developers. It's quick & easy.

WinAPI question

P: n/a
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:lin e 184

Can anyone help?

JIm

YBraker

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

"Jim Covington" <ji*@firstbankms.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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:lin e 184

Can anyone help?

JIm

YBraker

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #2

P: n/a
* Jim Covington <ji*@firstbankms.com> scripsit:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.