Solved

Workgroup Name

Posted on 2004-04-13
5
567 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
ID: 10816984
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
ID: 10818610
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
ID: 10818644
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
ID: 10820305
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
ID: 13737431
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

861 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