473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

WinAPI question

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
2 1620
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
* 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: KNS | last post by:
Hello, Can someone please suggest how to test for a mouse click using the WinAPI? I have dispatched a windows application and would like to detect any mouseclicks (and keyboard entry for that...
1
by: Gutek | last post by:
Hello, I have written a simple application, in which WinMain contains single line only: return DialogBox (0, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc) ; All messages are processed by...
2
by: Gustlik | last post by:
Hi I want to send to com at command but every command should be finish by enter, so my question is how to do it my first idea dosnt work: LPCTSTR query ="atd+48505634554;\n"; Enyone konw? ...
5
by: NJSG | last post by:
In english ========== It exists any way to copy directly the content of a file to ano- ther file using a Win32 (WinAPI) C application, that doesn't uses ms-dos "copy" and doesn't needs to ...
6
by: Joel ELISE | last post by:
Hi, I'd like to know if it's possible to show a picture in a dialog box. I'm programming in C language using WinAPI (for MS Windows). If it's possible, please, let me know how. Everything I...
3
by: Yura Tigiev | last post by:
Hello! Is it possible to write (classical windows with My computers, My Desctop, My networks Places end etc ) FolderBrowseDialog without use WinApi? If yes, can you send source code ? Yuri
1
by: MuZZy | last post by:
HI, I would really appreciate some hwlp with this: Here is the WinAPI struct used in call to winapi function wavInGetDevCaps: typedef struct { WORD wMid; WORD wPid; MMVERSION...
1
by: Qindong Zhang | last post by:
I have a API function which declared in VC++ like this: DLLimport BOOL WINAPI bl_HERA16_SetPlayBuffer(int iChannel,BYTE **bpBuffer); How could I declare and call this function in VB.NET. I...
7
by: jg | last post by:
or would I have to use C# or C++?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.