code_junkie

Pinvoke call for getting the windows serial number

Nov 14th, 2011
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * From Win32_OperatingSystem");
  2. // ...
  3. // Select number from managementobject mo["SerialNumber"]
  4.  
  5. #include <WINIOCTL.H>
  6. extern "C" __declspec(dllimport)
  7. BOOL KernelIoControl( DWORD dwIoControlCode, LPVOID lpInBuf, DWORD nInBufSize, LPVOID lpOutBuf, DWORD nOutBufSize, LPDWORD lpBytesReturned );
  8. #define IOCTL_HAL_GET_DEVICEID CTL_CODE(FILE_DEVICE_HAL, 21, METHOD_BUFFERED, FILE_ANY_ACCESS)
  9.  
  10. CString GetSerialNumberFromKernelIoControl() {
  11. DWORD dwOutBytes;
  12. const int nBuffSize = 4096;
  13. byte arrOutBuff[nBuffSize];
  14. BOOL bRes = ::KernelIoControl(IOCTL_HAL_GET_DEVICEID, 0, 0, arrOutBuff, nBuffSize, &dwOutBytes);
  15. if (bRes) { CString strDeviceInfo; for (unsigned int i = 0; i<dwOutBytes; i++) {
  16. CString strNextChar; strNextChar.Format(TEXT("%02X"), arrOutBuff[i]); strDeviceInfo += strNextChar;
  17. }
  18. CString strDeviceId = strDeviceInfo.Mid(40,2) + strDeviceInfo.Mid(45,9) + strDeviceInfo.Mid(70,6);
  19. return strDeviceId;
  20. } else {
  21. return _T("");
  22. }
  23. }
  24.  
  25. [DllImport("coredll.dll")]
  26. public static extern bool KernelIoControl(long dwIoControlCode, IntPtr lpInBuff, long dwInBuffSize, IntPtr lpOutBuff, long dwOutBuffSize, IntPtr lpBytesReturned);
  27.  
  28. if(Environment.OSVersion.Platform == PlatformID.WinCE) { ... }
Add Comment
Please, Sign In to add comment