Results 1 to 1 of 1

Thread: VB - Network - GetMacAdresses

  1. #1

    Thread Starter
    Lively Member blaff's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    69

    VB - Network - GetMacAdresses

    For a Project I wanted to get the MAC-Adress of a computer to check wether the program is licenced or not.
    I found several possibilities all over the net, but all of them (all I found) had one major drawback: they didn't work without a network => very annoying for Laptop-users out of their office.

    I wrote this solution using the command "ipconfig /all" which seems to work all the time (at least an windows version >= win2k).

    You need the a reference to "Microsoft VBScript Regular Expressions 5.5".

    Enjoy:

    VB Code:
    1. Private Function GetMacAdresses() As VBScript_RegExp_55.MatchCollection
    2. Dim reg As VBScript_RegExp_55.RegExp
    3. Dim fs As Object
    4. Dim F As Object
    5. Dim temppath As String
    6. Dim resFilename As String, batFilename As String
    7. Dim lret As Long
    8. Dim result As String
    9.  
    10.     Set reg = New VBScript_RegExp_55.RegExp
    11.     reg.Pattern = "..-..-..-..-..-.."
    12.    
    13.     On Error GoTo FailedLicence
    14.     Set fs = CreateObject("Scripting.FilesystemObject")
    15.    
    16.     temppath = fs.GetSpecialFolder(2)
    17.     resFilename = fs.Buildpath(temppath, "mac.txt")
    18.     batFilename = fs.Buildpath(temppath, "ip.cmd")
    19.    
    20.     On Error Resume Next
    21.     Set F = fs.opentextfile(batFilename, 8, True)
    22.     F.writeline "ipconfig /all > " & resFilename
    23.     F.Close
    24.    
    25.     If Err.Number <> 0 Then
    26.         MsgBox "Schreiben der Batch-Datei fehlgeschlagen: " & Err.Description, vbCritical + vbOKOnly
    27.         'frmDebugInfo.AddMessage "Schreiben der IP-Batch-Datei fehlgeschlagen: " & Err.Description
    28.         Err.Clear
    29.         Exit Function
    30.     End If
    31.    
    32.     On Error GoTo 0
    33.  
    34.     ShellAndWait batFilename, 0, lret, "omw", temppath
    35.  
    36.     If lret <> 0 Then
    37.         'frmDebugInfo.AddMessage "Ausführung der IP-Batch-Datei fehlgeschlagen."
    38.         'Err.Clear
    39.         Exit Function
    40.     End If
    41.     Set F = fs.opentextfile(resFilename, 1, False)
    42.    
    43.     result = F.readall
    44.     F.Close
    45.        
    46.     fs.deletefile resFilename
    47.     fs.deletefile batFilename
    48.    
    49.     Set F = Nothing
    50.     Set fs = Nothing
    51.    
    52.     reg.IgnoreCase = True
    53.     reg.Global = True
    54.     reg.MultiLine = True
    55.    
    56.     Set GetMacAdresses = reg.Execute(result)
    57.  
    58. Exit Function
    59. FailedLicence:
    60.  
    61. End Function

    and

    VB Code:
    1. Option Explicit
    2.  
    3.  
    4. Private Const SW_HIDE = 0
    5. Private Const SW_SHOWNORMAL = 1
    6. Private Const SW_NORMAL = 1
    7. Private Const SW_SHOWMINIMIZED = 2
    8. Private Const SW_SHOWMAXIMIZED = 3
    9. Private Const SW_MAXIMIZE = 3
    10. Private Const SW_SHOWNOACTIVATE = 4
    11. Private Const SW_SHOW = 5
    12. Private Const SW_MINIMIZE = 6
    13. Private Const SW_SHOWMINNOACTIVE = 7
    14. Private Const SW_SHOWNA = 8
    15. Private Const SW_RESTORE = 9
    16. Private Const SW_SHOWDEFAULT = 10
    17. Private Const SW_MAX = 10
    18. Private Const NORMAL_PRIORITY_CLASS = &H20&
    19. Private Const INFINITE = -1&
    20.        
    21. Private Type STARTUPINFO
    22. cb As Long
    23. lpReserved As String
    24. lpDesktop As String
    25. lpTitle As String
    26. dwX As Long
    27. dwY As Long
    28. dwXSize As Long
    29. dwYSize As Long
    30. dwXCountChars As Long
    31. dwYCountChars As Long
    32. dwFillAttribute As Long
    33. dwFlags As Long
    34. wShowWindow As Integer
    35. cbReserved2 As Integer
    36. lpReserved2 As Long
    37. hStdInput As Long
    38. hStdOutput As Long
    39. hStdError As Long
    40. End Type
    41.  
    42. Private Type PROCESS_INFORMATION
    43. hProcess As Long
    44. hThread As Long
    45. dwProcessID As Long
    46. dwThreadID As Long
    47. End Type
    48.  
    49. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
    50. hHandle As Long, ByVal dwMilliseconds As Long) As Long
    51.  
    52. Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
    53. lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
    54. lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
    55. ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
    56. ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As String, _
    57. lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    58. PROCESS_INFORMATION) As Long
    59.  
    60. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    61. hObject As Long) As Long
    62.  
    63. Private Declare Function GetExitCodeProcess Lib "kernel32" _
    64. (ByVal hProcess As Long, lpExitCode As Long) As Long
    65.  
    66.  
    67. '______________________________________________________________________
    68.  
    69.  
    70. Public Function ShellAndWait(ByVal strPath As String, _
    71. ByVal iWindowStyle As Integer, ByRef lreturnCode As Long, _
    72. Optional sWinTitle As String = "", _
    73. Optional sDirectoryPath As String = "") _
    74. As Boolean
    75.  
    76. Dim proc As PROCESS_INFORMATION, bSuccess As Long
    77. Dim start As STARTUPINFO
    78. Dim ret As Long
    79.  
    80.  
    81.  
    82. On Error GoTo ShellAndWaiterr
    83.  
    84. ' Initialize the STARTUPINFO structure:
    85. start.cb = Len(start) ' you must set the size
    86. start.dwFlags = &H1& ' STARTF_USESHOWWINDOW Use Show Window
    87. start.wShowWindow = iWindowStyle
    88.  
    89.  
    90. If Not IsMissing(sWinTitle) Then
    91.    ' if there is a title set the window title
    92.    start.lpTitle = sWinTitle
    93. End If
    94.  
    95. ' Start the shelled application:
    96. ret = CreateProcessA(0&, strPath, 0&, 0&, 1&, _
    97. NORMAL_PRIORITY_CLASS, 0&, _
    98. sDirectoryPath, start, _
    99. proc)
    100.  
    101. ' Wait for the shelled application to finish:
    102. ret = WaitForSingleObject(proc.hProcess, 100&)
    103. Do While ret <> 0
    104.    If ret < 0 Then
    105.        ShellAndWait = False
    106.        Exit Function
    107.    End If
    108.  
    109.    DoEvents
    110.  
    111.    ret = WaitForSingleObject(proc.hProcess, _
    112.        100&)
    113. Loop
    114.  
    115. 'get the return code
    116. ret = GetExitCodeProcess(proc.hProcess, _
    117. lreturnCode)
    118.  
    119. 'close the process handles
    120. ret = CloseHandle(proc.hProcess)
    121. ShellAndWait = True
    122. Exit Function
    123.  
    124. ShellAndWaiterr:
    125.    ShellAndWait = False
    126.    Exit Function
    127.    Resume
    128. End Function
    Last edited by blaff; Apr 11th, 2003 at 12:38 PM.
    _____
    blaff

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width