Solved

Workgroup Name

Posted on 2004-04-13
5
557 Views
Last Modified: 2011-10-03
Hi,
 Ive read in many places how to get the local computer name, but how do i get the workgroup name? It needs to work on all windows OSs.

Matt.
0
Comment
Question by:pyromatt
  • 2
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 105 total points
Comment Utility
Here you go:


=====================================
1. Start a new Standard.Exe Project.
2. Add 2 Command Buttons to Form1.
   Change their Name properties to
   Command1 and Command2 respectively.
3. Add 2 Labels to Form1.
   Change their Name properties to
   Label1 and Label2 respectively.
4. Add a Textbox (Text1) to Form1.
   Change the Multiline property to True.
5. Click on Vb Menu -> Project -> References
   And click on the reference to the
   "Windows Scripting Runtime"
6. Copy/Paste the following into the Form1 code window.
7. Press F5 to run.

<----- Code Begin ----->

Option Explicit

Private m_strOriginalComputerName As String
Private m_strOriginalWorkgroupName As String

Private Sub Form_Load()

   Form1.Move _
      Screen.Width * 0.1, _
      Screen.Height * 0.1, _
      Screen.Width * 0.8, _
      Screen.Height * 0.8

   Command1.Caption = "Update Computer Name"
   m_strOriginalComputerName = xGetComputerName
   Label1.Caption = m_strOriginalComputerName
   If Len(Label1.Caption) = 0 _
   Then
      Label1.Caption = "An Error Occurred"
   End If
   Label1.BorderStyle = 1
   Label1.FontBold = True
   
   Command2.Caption = "Update Workgroup Name"
   m_strOriginalWorkgroupName = xGetWorkgroupName
   Label2.Caption = m_strOriginalWorkgroupName
   If Len(Label2.Caption) = 0 _
   Then
      Label2.Caption = "An Error Occurred"
   End If
   Label2.BorderStyle = 1
   Label2.FontBold = True
   
   Text1.FontBold = True
   Text1.Locked = True
   Text1.Text _
      = "Caution:  While we believe that changing the Computer Name " _
      & "and/or the Workgroup Name will NOT be harmful to your computer, " _
      & "YOU DO SO AT YOUR OWN RISK!" & vbCrLf & vbCrLf _
      & "If YOU do decide to change these parameters, YOU will be prompted " _
      & "to restart your computer for these changes to take effect at the " _
      & "conclusion of this session." & vbCrLf & vbCrLf _
      & "ENJOY!!!"
     
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   
   If m_strOriginalComputerName = Label1.Caption _
   And m_strOriginalWorkgroupName = Label2.Caption _
   Then
      Exit Sub
   End If
   
   Dim strMessage As String
   Dim intReturn As Integer
   Dim booReboot As Boolean
   Dim booSuccess As Boolean
     
   If m_strOriginalComputerName <> Label1.Caption _
   Then
      strMessage _
         = "From: " & m_strOriginalComputerName & vbCrLf _
         & "To  : " & Label1.Caption
      intReturn = MsgBox(strMessage, vbOKCancel, "Save COMPUTER NAME?")
      If intReturn = vbOK _
      Then
         Call xPutComputerName(Label1.Caption)
         booReboot = True
      End If
   End If
   
   If m_strOriginalWorkgroupName <> Label2.Caption _
   Then
      strMessage _
         = "From: " & m_strOriginalWorkgroupName & vbCrLf _
         & "  To: " & Label2.Caption
      intReturn = MsgBox(strMessage, vbOKCancel, "Save WORKGROUP NAME?")
      If intReturn = vbOK _
      Then
         Call xPutWorkgroupName(Label2.Caption)
         booReboot = True
      End If
   End If
   
   If booReboot _
   Then
      strMessage = "Please REBOOT your Computer for these change(s) to take effect"
      MsgBox (strMessage)
   End If
   
End Sub

Private Sub Form_Resize()

   Command1.Move _
      ScaleWidth * 0.1, _
      ScaleHeight * 0.1, _
      ScaleWidth * 0.3, _
      ScaleHeight * 0.1
   
   Label1.Move _
      ScaleWidth * 0.5, _
      ScaleHeight * 0.1, _
      ScaleWidth * 0.4, _
      ScaleHeight * 0.1

   Command2.Move _
      ScaleWidth * 0.1, _
      ScaleHeight * 0.25, _
      ScaleWidth * 0.3, _
      ScaleHeight * 0.1
   
   Label2.Move _
      ScaleWidth * 0.5, _
      ScaleHeight * 0.25, _
      ScaleWidth * 0.4, _
      ScaleHeight * 0.1
   
   Text1.Move _
      ScaleWidth * 0.1, _
      ScaleHeight * 0.4, _
      ScaleWidth * 0.8, _
      ScaleHeight * 0.5

End Sub
   
Private Sub Command1_Click()
   
   Dim strNewName As String
   strNewName = InputBox("Current Computer Name:  " & Label1.Caption, _
      "Please Enter The NEW Computer Name", Label1.Caption)
   strNewName = Trim(strNewName)
   If Len(strNewName) > 0 _
   Then
      Label1.Caption = strNewName
   End If

End Sub

Private Sub Command2_Click()
   
   Dim strNewName As String
   strNewName = InputBox("Current Workgroup Name:  " & Label2.Caption, _
      "Please Enter The NEW Workgroup Name", Label2.Caption)
   strNewName = Trim(strNewName)
   If Len(strNewName) > 0 _
   Then
      Label2.Caption = strNewName
   End If

End Sub

Private Function xGetComputerName() _
As String
   
   Dim wshShell As New IWshShell_Class
   On Error GoTo RegError
   xGetComputerName = wshShell.RegRead _
      ("HKEY_LOCAL_MACHINE\" _
      & "System\" _
      & "CurrentControlSet\" _
      & "Control\" _
      & "ComputerName\" _
      & "ComputerName\" _
      & "ComputerName")
   On Error GoTo 0
   Exit Function

RegError:
   MsgBox ("xGetComputerName - Error Reading from Registry")

End Function

Private Function xGetWorkgroupName() _
As String
   
   Dim wshShell As New IWshShell_Class
   On Error GoTo RegError
   xGetWorkgroupName = wshShell.RegRead _
      ("HKEY_LOCAL_MACHINE\" _
      & "System\" _
      & "CurrentControlSet\" _
      & "Services\" _
      & "Vxd\" _
      & "VnetSup\" _
      & "WorkGroup")
   On Error GoTo 0
   Exit Function

RegError:
   MsgBox ("xGetWorkgroupName - Error Reading from Registry")

End Function

Private Function xPutComputerName _
(ByVal strComputerName As String) _
As Boolean
   
   Dim wshShell As New IWshShell_Class
   On Error GoTo RegError
   wshShell.RegWrite _
      "HKEY_LOCAL_MACHINE\" _
      & "System\" _
      & "CurrentControlSet\" _
      & "Control\" _
      & "ComputerName\" _
      & "ComputerName\" _
      & "ComputerName" _
      , strComputerName
   On Error GoTo 0
   xPutComputerName = True
   Exit Function

RegError:
   MsgBox (strComputerName & " - Error Writing To Registry")

End Function

Private Function xPutWorkgroupName _
(ByVal strWorkgroupName As String) _
As Boolean
   
   Dim wshShell As New IWshShell_Class
   On Error GoTo RegError
   wshShell.RegWrite _
      "HKEY_LOCAL_MACHINE\" _
      & "System\" _
      & "CurrentControlSet\" _
      & "Services\" _
      & "Vxd\" _
      & "VnetSup\" _
      & "WorkGroup" _
      , strWorkgroupName
   On Error GoTo 0
   xPutWorkgroupName = True
   Exit Function

RegError:
   MsgBox (strWorkgroupName & " - Error Writing To Registry")

End Function

<----- Code End ----->




Good Luck
-Brian
0
 

Author Comment

by:pyromatt
Comment Utility
so will reading "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\Vxd\VnetSup\WorkGroup" and "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\ComputerName\ComputerName\ComputerName" work on all windows OSs and OS configurations? and is the Windows scripting runtime available on every windows pc?

Matt.
0
 

Author Comment

by:pyromatt
Comment Utility
also the windows scripting runtime didnt show in my refernces, i had to use Windows Script Host Object Model. so does this change between different OSs and configurations and will this affect the program?
0
 
LVL 19

Expert Comment

by:BrianGEFF719
Comment Utility
It should work Fine on Windows 98, Windows 2000, Windows NT >4, Windows XP

It will only work on Windows 95 if the user has Windows Scripting Installed.


-Brian
0
 

Expert Comment

by:dzumwalt
Comment Utility
I am running XP Home.

The code above references a Vxd\VnetSup\WorkGroup branch inside of the services branch in order to obtain the workgroup. What if there IS no vxd branch there? Where is the workgroup stored now?

Dan Zumwalt
dzumwalt@symmetricsoftware.com
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now