Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: NT
Expertise: Beginner
Feb 18, 1999

Querying NT

Question:
Is there a way to query NT 4.0 (programmatically or manually) to have it tell me whether it is NT workstation or NT server? Is the answer in the system registry or in the OS itself?

Answer:
Yes, the following code should work for you. Note that this assumes that anything that isn't a NT server is a NT workstation, so if that's not true, you might want some further checks and a return of other than a boolean value.

************************************************

Option Explicit

' Constants for type of computer
Private Enum enumServerType
   SV_TYPE_WORKSTATION = &H1&
   SV_TYPE_SERVER = &H2&
   SV_TYPE_SQLSERVER = &H4&
   SV_TYPE_DOMAIN_CTRL = &H8&
   SV_TYPE_DOMAIN_BAKCTRL = &H10&
   SV_TYPE_NOVELL = &H80&
   SV_TYPE_NT = &H1000&
   SV_TYPE_WFW = &H2000&
   SV_TYPE_SERVER_NT = &H8000&
   SV_TYPE_WINDOWS = &H400000
   SV_TYPE_ALL = &HFFFF
End Enum

' Type used by NetServerGetInfo and NetServerSetInfo
Private Type SERVER_INFO_101
    sv101_platform_id As Long
    sv101_name As Long
    sv101_ver_major As Long
    sv101_ver_minor As Long
    sv101_type As Long
    sv101_comment As Long
End Type
      
' API declarations
Private Declare Function NetServerGetInfo _
   Lib "Netapi32" _
      (sServerName As Byte, _
       ByVal lLevel As Long, _
       vBuffer As Long) As Long
Private Declare Function NetApiBufferFree _
   Lib "netapi32.dll" _
   (ByVal lngPtrBuffer As Long) As Long
Private Declare Function lstrlenW _
   Lib "kernel32" _
   (ByVal lpString As Long) As Long
Public Declare Sub CopyMem _
   Lib "kernel32" Alias "RtlMoveMemory" _
   (dest As Any, vSrc As Any, ByVal lSize As Long)

Public Sub Main()
   Debug.Print IsServer("")
   Debug.Print IsServer("PDCNew")
End Sub

Public Function IsServer(Optional ByVal xi_strServerName As String = "") As Boolean
   Dim p_bytServerName()         As Byte
   Dim p_lngRtn                  As Long
   Dim p_lngSrvInfoRtn           As Long
   Dim p_typServerInfo101        As SERVER_INFO_101
   Dim p_lngServEnumLevel        As Long
   
   ' Initialize the variables
   If Trim$(xi_strServerName) = vbNullString Then
      p_bytServerName = vbNullChar
   Else
      p_bytServerName = Trim$(xi_strServerName) & vbNullChar
   End If
   p_lngServEnumLevel = 101
  
   ' Call NetServerEnum to get a list of Servers
   If Trim$(xi_strServerName) = vbNullString Then
      p_lngRtn = NetServerGetInfo(sServerName:=ByVal p_bytServerName(0), _
                                  lLevel:=p_lngServEnumLevel, _
                                  vBuffer:=p_lngSrvInfoRtn)
   Else
      p_lngRtn = NetServerGetInfo(sServerName:=p_bytServerName(0), _
                                  lLevel:=p_lngServEnumLevel, _
                                  vBuffer:=p_lngSrvInfoRtn)
   End If
   
   If p_lngRtn = 0 Then
      ' xxx
      CopyMem p_typServerInfo101, _
              ByVal p_lngSrvInfoRtn, _
              Len(p_typServerInfo101)
      If p_typServerInfo101.sv101_type And enumServerType.SV_TYPE_SERVER_NT Then
         IsServer = True
      ElseIf p_typServerInfo101.sv101_type And enumServerType.SV_TYPE_DOMAIN_BAKCTRL Then
         IsServer = True
      ElseIf p_typServerInfo101.sv101_type And enumServerType.SV_TYPE_DOMAIN_CTRL Then
         IsServer = True
      Else
         IsServer = False
      End If
      
   Else
      Debug.Print "Error: " & Err.LastDllError
   End If
   
End Function
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap