?
Solved

Workgroup Name

Posted on 2004-04-13
5
Medium Priority
?
583 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
BrianGEFF719 earned 420 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

777 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