It is sometimes beneficial to have a unique identifier for the system on which we are currently running. For example, if you sell software, you may wish to verify that the computer is licensed or activated. A question on CSSM today reminded me of this issue.
A trivial solution to this question is to use the built-in license function. Unfortunately, this does not work with a multi-system network/floating Matlab license, nor on deployed (compiled) systems.
While Matlab doesn’t have a built-in solution, we can use simple Java to access system information. There are several possible approaches. Here are several alternatives:
A Windows-specific approach is to return the Window Domain Controller’s SID (Security ID). This is a unique identifier that changes with each computer/user. Java enables direct access to this identifier, and we can run this directly in Matlab:
>> sid = get(com.sun.security.auth.module.NTSystem,'DomainSID') sid = S-1-5-21-292311649-1610625687-3346456317
The exact same value can also be gotten directly from the Windows Registry:
% Note: scanning HKEY_-- USERS node names is better, but Matlab's winqueryreg() can't do that... rootkey = 'HKEY_CURRENT_-- USER'; subkey = 'Software\Microsoft\Windows\CurrentVersion\Group Policy\GroupMembership'; count = winqueryreg(rootkey,subkey,'Count'); for idx = 0 : double(count)-1 % Note: double() is needed for Matlab 6 compatibility val = winqueryreg(rootkey,subkey,['Group' char('0'+idx)]); dashes = find(val=='-'); if length(dashes) > 4, sid = val(1:dashes(end)-1); break; end % short ids are phoney end
This later version, although less simple than the first alternative (Java-based approach) above, has the benefit of working on old Matlab releases such as R12 (6.0) where the Java approach fails. For this reason, my getsid utility on the File Exchange uses the registry approach.
For non-Windows systems, both of the above approaches fail. For such platforms, we can use the Ethernet addresses of the computer’s network cards. This should be pretty unique for any practical effect:
sid = ''; ni = java.net.NetworkInterface.getNetworkInterfaces; while ni.hasMoreElements addr = ni.nextElement.getHardwareAddress; if ~isempty(addr) addrStr = dec2hex(int16(addr)+128); sid = [sid, '.', reshape(addrStr,1,2*length(addr))]; end end >> sid sid = .89ECC872C85C.8AFE928FEDB4.8262278A1CCA.899919B50FC9
This again uses Java (which unfortunately fails on R12 aka 6.0). The benefit is that it is entirely cross-platform, working wherever Matlab runs (including Windows), on any Matlab release that supports Java (I think R13 aka 6.5 should be the earliest).
Do you have another way to generate unique identifiers? If so, please share your experience in a comment.