Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #ifdef _WIN32
  2.     namespace win{
  3.         #include <windows.h>
  4.         #include <intrin.h>
  5.         #include <iphlpapi.h>
  6.         #include <stdio.h>
  7.         #include <stdlib.h>
  8.         #pragma comment(lib, "IPHLPAPI.lib")
  9.        
  10.        
  11.         // we just need this for purposes of unique machine id. So any one or two mac's is
  12.         // fine.
  13.         unsigned short hashMacAddress( PIP_ADAPTER_INFO info )
  14.         {
  15.             unsigned short hash = 0;
  16.             for ( unsigned int i = 0; i < info->AddressLength; i++ )
  17.             {
  18.                 hash += ( info->Address[i] << (( i & 1 ) * 8 ));
  19.             }
  20.             return hash;
  21.         }
  22.        
  23.         void getMacHash( unsigned short& mac1, unsigned short& mac2 )
  24.         {
  25.             IP_ADAPTER_INFO AdapterInfo[32];
  26.             ULONG dwBufLen = sizeof( AdapterInfo );
  27.            
  28.             ULONG dwStatus = GetAdaptersInfo( AdapterInfo, &dwBufLen );
  29.             if ( dwStatus != ERROR_SUCCESS )
  30.                 return; // no adapters.
  31.            
  32.             PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
  33.             mac1 = hashMacAddress( pAdapterInfo );
  34.             if ( pAdapterInfo->Next )
  35.                 mac2 = hashMacAddress( pAdapterInfo->Next );
  36.            
  37.             // sort the mac addresses. We don't want to invalidate
  38.             // both macs if they just change order.
  39.             if ( mac1 > mac2 )
  40.             {
  41.                 unsigned short tmp = mac2;
  42.                 mac2 = mac1;
  43.                 mac1 = tmp;
  44.             }
  45.         }
  46.        
  47.         unsigned short getVolumeHash()
  48.         {
  49.             DWORD serialNum = 0;
  50.            
  51.             // Determine if this volume uses an NTFS file system.
  52.             GetVolumeInformation( "c:\\", NULL, 0, &serialNum, NULL, NULL, NULL, 0 );
  53.             unsigned short hash = (unsigned short)(( serialNum + ( serialNum >> 16 )) & 0xFFFF );
  54.            
  55.             return hash;
  56.         }
  57.        
  58.         unsigned short getCpuHash()
  59.         {
  60.             int cpuinfo[4] = { 0, 0, 0, 0 };
  61.             __cpuid( cpuinfo, 0 );          
  62.             unsigned short hash = 0;          
  63.             unsigned short* ptr = (unsigned short*)(&cpuinfo[0]);
  64.             for ( unsigned int i = 0; i < 8; i++ )  
  65.                 hash += ptr[i];    
  66.            
  67.             return hash;          
  68.         }        
  69.        
  70.         const char* getMachineName()      
  71.         {        
  72.             static char computerName[1024];
  73.             DWORD size = 1024;    
  74.             GetComputerName( computerName, &size );          
  75.             return &(computerName[0]);      
  76.         }
  77.  
  78.     }
  79. #endif//_WIN32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement