Click to See Complete Forum and Search --> : Using NetValidateName


Hari Prasad
June 14th, 2001, 09:47 AM
Hi,
Anyone know How we can use the NetValidateName in VB. My problem is how to declate the function and the last parameter of it which is of NETSETUP_NAME_TYPE. This seems to be an enumeratwed type and i am not sure how to use enumerated type in VB. Your help will be greatly appreciated

cksiow
June 14th, 2001, 07:26 PM
try this


public Enum NameType
NetSetupUnknown = 0
NetSetupMachine = 1
NetSetupWorkgroup = 2
NetSetupDomain = 3
NetSetupNonExistentDomain = 4
NetSetupDnsMachine = 5
End enum

declare function NetValidateName(.......,NETSETUP_NAME_TYPE as NameType)





HTH

cksiow
http://vblib.virtualave.net - share our codes

El Nino
June 15th, 2001, 08:56 AM
I'd like to know how I can use this function in VB as well, if you could provide me w/ some informations (declarations and types) I'd be incredibly grateful ! I've been looking for this for 2 weeks w/o success !
You'd made my day if u can provide me them !

=)

Regards,

El Nino

Hari Prasad
June 18th, 2001, 09:53 AM
Hi, Here is how you can use the NetValidateName.
Open a new form add three text boxes with names txtUser, txtPassword, txtMachine and one command button cmdOk . Paste the code into the declaration section. Hope this will be useful

Option Explicit
Private Enum NameType
NetSetupUnknown = 0
NetSetupMachine = 1
NetSetupWorkgroup = 2
NetSetupDomain = 3
NetSetupNonExistentDomain = 4
NetSetupDnsMachine = 5
End Enum

Const FORMAT_MESSAGE_FROM_HMODULE = &H800
Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000

Const NERR_BASE = 2100
Const MAX_NERR = NERR_BASE + 899 ' This is the last error in
' NERR range.
Const LOAD_LIBRARY_AS_DATAFILE = &H2

Private Declare Function LoadLibraryEx Lib "kernel32" Alias _
"LoadLibraryExA" (ByVal lpLibFileName As String, _
ByVal hFile As Long, ByVal dwFlags As Long) As Long

Private Declare Function FreeLibrary Lib "kernel32" _
(ByVal hLibModule As Long) As Long

Private Declare Function NetApiBufferFree& Lib "netapi32" _
(ByVal Buffer As Long)

Private Declare Sub lstrcpyW Lib "kernel32" _
(dest As Any, ByVal src As Any)

Private Declare Function FormatMessage Lib "kernel32" Alias _
"FormatMessageA" (ByVal dwFlags As Long, _
ByVal lpSource As Long, _
ByVal dwMessageId As Long, _
ByVal dwLanguageId As Long, ByVal lpBuffer As String, _
ByVal nSize As Long, Arguments As Any) As Long

Private Declare Function NetGetDCName Lib "netapi32.dll" ( _
ServerName As Long, DomainName As Byte, bufptr As Long) As Long

Private Declare Function NetValidateName Lib "netapi32.dll" ( _
ByVal ServerName As String, ByVal DomainName As String, _
ByVal AccountName As String, ByVal PassWord As String, ByVal NameType As NameType) As Long
Public Function GetPrimaryDCName(ByVal DName As String) As String

Dim DCName As String, DCNPtr As Long
Dim DNArray() As Byte, DCNArray(100) As Byte
Dim result As Long

DNArray = DName & vbNullChar
' Lookup the Primary Domain Controller
result = NetGetDCName(0&, DNArray(0), DCNPtr)
If result <> 0 Then
MsgBox "Error: " & result
Exit Function
End If
lstrcpyW DCNArray(0), DCNPtr
result = NetApiBufferFree(DCNPtr)
DCName = DCNArray()
GetPrimaryDCName = Left(DCName, InStr(DCName, Chr(0)) - 1)

End Function

Private Sub cmdOK_Click()
Dim sServer As String, sUser As String
Dim sPassWord As String
Dim sDomainName As String 'Computer name also can be used


Dim dwLevel As Long
Dim lRet As String
Dim sNew As String
Dim sNameType As NameType

' StrConv Functions are necessary since VB will perform
' UNICODE/ANSI translation before passing strings to the NETAPI
' functions

MousePointer = vbHourglass
sUser = StrConv(txtUser, vbUnicode)
sPassWord = StrConv(txtPassword, vbUnicode)
sDomainName = StrConv(txtMachine, vbUnicode)


'See if this is Domain or Computer referenced
If Left(txtMachine, 2) = "\\" Then
sServer = StrConv(txtMachine, vbUnicode)
Else
' Domain was referenced, get the Primary Domain Controller
sServer = StrConv(GetPrimaryDCName(txtMachine), vbUnicode)
End If

'Based on this parameter value we can decide what is to be validated
sNameType = NetSetupDomain Or NetSetupMachine Or NetSetupUnknown
lRet = NetValidateName(sServer, sDomainName, sUser, sPassWord, sNameType)

MousePointer = vbDefault
If lRet <> 0 Then
DisplayError lRet
Else
MsgBox "NetValidateName succeeded"
End If



End Sub

Private Sub DisplayError(ByVal lCode As Long)
Dim sMsg As String
Dim sRtrnCode As String
Dim lFlags As Long
Dim hModule As Long
Dim lRet As Long

hModule = 0
sRtrnCode = Space$(256)
lFlags = FORMAT_MESSAGE_FROM_SYSTEM

' if lRet is in the network range, load the message source

If (lCode >= NERR_BASE And lCode <= MAX_NERR) Then
hModule = LoadLibraryEx("netmsg.dll", 0&, _
LOAD_LIBRARY_AS_DATAFILE)

If (hModule <> 0) Then
lFlags = lFlags Or FORMAT_MESSAGE_FROM_HMODULE
End If

End If

' Call FormatMessage() to allow for message text to be acquired
' from the system or the supplied module handle.
'

lRet = FormatMessage(lFlags, hModule, lCode, 0&, _
sRtrnCode, 256&, 0&)
If lRet = 0 Then
MsgBox "FormatMessage Error : " & Err.LastDllError
End If

' if you loaded a message source, unload it.
'
If (hModule <> 0) Then
FreeLibrary (hModule)
End If

'//... now display this string
sMsg = "ERROR: " & lCode & " - " & sRtrnCode

MsgBox sMsg

End Sub