Call Pinvoke to get a Windows serial number?

The usual way to get a Windows serial number is through WMI.

 ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * From Win32_OperatingSystem");
 // ...
 // Select number from managementobject mo["SerialNumber"]

I do not want to use WMI because the compact structure does not support it. The assembly should work on the desktop and on the side of the compact frame, so I can not add the link.

How can I get the same result using pinvoke call?

0
source share
2 answers

You will need to call KernelIOControl for WindowsCE.

Here's the C ++ code, doesn't have the time to convert it to C #

#include <WINIOCTL.H> 
extern "C" __declspec(dllimport) 
BOOL KernelIoControl( DWORD dwIoControlCode, LPVOID lpInBuf, DWORD nInBufSize, LPVOID lpOutBuf, DWORD nOutBufSize, LPDWORD lpBytesReturned ); 
#define IOCTL_HAL_GET_DEVICEID CTL_CODE(FILE_DEVICE_HAL, 21, METHOD_BUFFERED, FILE_ANY_ACCESS) 

CString GetSerialNumberFromKernelIoControl() { 
    DWORD dwOutBytes; 
    const int nBuffSize = 4096; 
    byte arrOutBuff[nBuffSize]; 
    BOOL bRes = ::KernelIoControl(IOCTL_HAL_GET_DEVICEID, 0, 0, arrOutBuff, nBuffSize, &dwOutBytes); 
    if (bRes) { CString strDeviceInfo; for (unsigned int i = 0; i<dwOutBytes; i++) { 
        CString strNextChar; strNextChar.Format(TEXT("%02X"), arrOutBuff[i]); strDeviceInfo += strNextChar; 
    } 
    CString strDeviceId = strDeviceInfo.Mid(40,2) + strDeviceInfo.Mid(45,9) + strDeviceInfo.Mid(70,6); 
    return strDeviceId; 
    } else { 
        return _T(""); 
    } 
} 

Edit: (pinvoke kernelIOControl C #)

[DllImport("coredll.dll")]
    public static extern bool KernelIoControl(long dwIoControlCode, IntPtr lpInBuff, long dwInBuffSize, IntPtr lpOutBuff, long dwOutBuffSize, IntPtr lpBytesReturned);
+1
source

-, , . . , :

if(Environment.OSVersion.Platform == PlatformID.WinCE) { ... }

.

, . Windows Mobile 5.0 GetDeviceUniqueID, KernelIoControl, , .FOr Pocket PC 2003 , KernelIoControl P/Invoke , , , .

Windows CE . , IOCTL_HAL_GET_DEVICEID, - ( OEM- ID API). CE 6.0 KernelIoControl , , API- OEM.

0

Source: https://habr.com/ru/post/1746141/


All Articles