3

is there any multiplatform method to identify user account, system or entire computer? Something like:

CreateObject("Scripting.FileSystemObject").GetDrive("C:\").SerialNumber

It may use some combinations of username and other variables to build (quite) unique ID text/number. Maybe should I use Environ? But where can I find complete list multiplatform Environ working variables? It may use OS identification also.

EDIT some starting code to clarify my question

Sub GenerateID()

    #If Mac Then
    '------- Mac ---------------
        MsgBox MacIdNumber
    #Else
    '------- Windows -----------
        MsgBox WindowsIdNumber
    #End If
    End Sub

Function MacIdNumber() As Long

    Dim str1, str2, str3 ' str4, str5...
        str1 = VBA.Environ$("USER")
        str2 = Application.Version
        str3 = Application.OperatingSystem
        ' and here I am looking for another ideas
        ' str4 = ...???
    MacIdNumber = CreateUniqueId(str1, str2, str3)
    End Function

Function WindowsIdNumber() As Long

    Dim str1, str2, str3, str4 ', str5...
        str1 = CreateObject("Scripting.FileSystemObject").GetDrive("C:\").SerialNumber
        str2 = VBA.Environ$("USERNAME")
        str3 = Application.Version
        str4 = Application.OperatingSystem
        ' and here I am looking for another ideas
        ' str5 = ...???
        ' but maybe for Windows disc SerialNumber is enough
    WindowsIdNumber = CreateUniqueId(str1, str2, str3, str4)
    End Function

Function CreateUniqueId(ParamArray str() As Variant) As Long
    
    ' here I'll make checksum
    ' some calculations...
    End Function

I am trying to get as unique ID strings as possible (but I know it probably won't be 100% unique).

It must work in reliable way for MAC 2016 and higher and Windows versions of course. So If you give an idea with apple script- I must be sure that no endless loop/ error occurs.

I am Windows user and I am not able to test it on MAC right now.

0m3r
  • 12,286
  • 15
  • 35
  • 71
Rafał B.
  • 487
  • 1
  • 3
  • 19
  • 3
    One way to id a system is to read the timestamp on a file created during the installation. For instance on Windows: `FileDateTime("C:\Windows\System32\license.rtf")`. – Florent B. Aug 31 '20 at 18:38
  • @FlorentB. Very clever! If you could share universal path for some fixed OSX system file (across all new versions to be universal) feel free to make an anwer here instead of comment – Rafał B. Aug 31 '20 at 21:04
  • have a look at "/private/var/log/OSInstall.custom" or "/private/var/db/.AppleSetupDone". I don't have MacOs to test it. – Florent B. Aug 31 '20 at 21:33
  • @FlorentB. : nice! However, a timestamp can be edited/changed/faked. I guess I'm assuming this is for licensing purposes. I think Rafal B. idea of hard drive serial number is a good one, I had no idea it was readily available from the Scripting Runtime library. Mind you, serial number can also be faked with some clever software I should imagine, just harder. – S Meaden Sep 02 '20 at 15:53
  • @S Meaden, there's no real protection when it comes to VBA since you can easily analyse the code and change it. Not to mention that whatever method you choose, it will fail if the workbook is opened in a Virtual Machine. – Florent B. Sep 02 '20 at 20:06

2 Answers2

0

Looks like the answer is "no".

According to https://learn.microsoft.com/en-us/office/vba/api/overview/office-mac

Office 2016 for Mac is sandboxed

Unlike other versions of Office apps that support VBA, Office 2016 for Mac apps are sandboxed.

Sandboxing restricts the apps from accessing resources outside the app container. This affects any add-ins or macros that involve file access or communication across processes. You can minimize the effects of sandboxing by using the new commands described in the following section.

What this says to me is that you are going to have to use a different, native, scripting language to get the information you want in to Excel.

HackSlash
  • 4,944
  • 2
  • 18
  • 44
  • Thanks for answer. Although I think it may be possible maybe running some script and using conditional compiling or more native VBA methods. – Rafał B. Aug 31 '20 at 07:40
  • Sounds like you are talking about escaping the Sandbox. If you did, that would be a security vulnerability and it would be closed by Apple. The answer I gave it canonical because I cited the manufacturers webpage. You have to put information in to Excel from the outside. You won't be able to reach out from within. This is a basic tenant of security sandboxing. – HackSlash Aug 31 '20 at 15:30
0

Is this what you are trying? tested it on both Excel 2016 (Mac/Windows)

Option Explicit

Sub GenerateID()
    #If Mac Then
    '------- Mac ---------------
        MsgBox GetUniqueID("Mac")
    #Else
    '------- Windows -----------
        MsgBox GetUniqueID("Win")
    #End If
End Sub

Private Function GetUniqueID(sys As String) As String
    Select Case sys
    Case "Mac"
        '~~> Ref: http://www.rondebruin.nl/mac/mac003.htm
        Dim AppleScript As String
        AppleScript = "set uuid to do shell script ""system_profiler SPHardwareDataType" & _
                      " | awk '/Hardware UUID:/ {print $NF}'"""
        
        On Error Resume Next
        GetUniqueID = MacScript(AppleScript)
        On Error GoTo 0
    Case "Win"
        GetUniqueID = CreateObject("Scripting.FileSystemObject").GetDrive("C:\").SerialNumber
    End Select
End Function

Screenshot

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Thank you very much for testing! But what about [information that MacScript is deprecated](https://stackoverflow.com/a/30949324/209942)? If I understand well described AppleScriptTask workaround is not an option for me If I would generate ID in background (without user's knowledge). – Rafał B. Sep 04 '20 at 08:41
  • That is 2015 year post... Like I said I just tested it and it works... – Siddharth Rout Sep 04 '20 at 08:49
  • Ok! I was asking because the same info there is on [Microsoft documentation site- MacScript](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/macscript-function) So deprecated for a few years now, but it seems still working based on your test – Rafał B. Sep 04 '20 at 08:54
  • 1
    I used to be a big advocate of MSDN till couple of years but not anymore. They no longer update their articles even when you give them a well drafted post with code to support your findings. Finally I gave up on them. You will find much better information on stackoverflow. Having said that I still refer people to MSDN but do not swear by it anymore. Well the code that I gave above is tried and tested. Feel free to test it yourself before believing what MSDN or I say :) – Siddharth Rout Sep 04 '20 at 09:05
  • I think you should have waited to test it before you accepted this as an answer... – Siddharth Rout Sep 04 '20 at 09:27
  • I have no mac, must to trust you ;) – Rafał B. Sep 04 '20 at 09:32
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/220963/discussion-between-siddharth-rout-and-rafal-b). – Siddharth Rout Sep 04 '20 at 09:33