using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace SerkanOzcan
{
public class DeviceInfo
{
[DllImport("coredll.dll")]
private static extern bool KernelIoControl(Int32 IoControlCode, IntPtr
InputBuffer, Int32 InputBufferSize, byte[] OutputBuffer, Int32
OutputBufferSize, ref Int32 BytesReturned);
private static Int32 FILE_DEVICE_HAL = 0x00000101;
private static Int32 FILE_ANY_ACCESS = 0x0;
private static Int32 METHOD_BUFFERED = 0x0;
private static Int32 IOCTL_HAL_GET_DEVICEID =
((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14)
| ((21) << 2) | (METHOD_BUFFERED);
public static string GetDeviceID()
{
byte[] OutputBuffer = new byte[256];
Int32 OutputBufferSize, BytesReturned;
OutputBufferSize = OutputBuffer.Length;
BytesReturned = 0;
bool retVal = KernelIoControl(IOCTL_HAL_GET_DEVICEID,
IntPtr.Zero,
0,
OutputBuffer,
OutputBufferSize,
ref BytesReturned);
if (retVal == false)
{
return null;
}
Int32 PresetIDOffset = BitConverter.ToInt32(OutputBuffer, 4);
Int32 PlatformIDOffset = BitConverter.ToInt32(OutputBuffer, 0xc);
Int32 PlatformIDSize = BitConverter.ToInt32(OutputBuffer, 0x10);
StringBuilder sb = new StringBuilder();
for (int i = PresetIDOffset;
i < PlatformIDOffset + PlatformIDSize;
i++)
{
sb.Append(String.Format("{0:X2}", OutputBuffer[i]));
}
return sb.ToString();
}
[DllImport("coredll")]
public static extern int lineInitializeEx(out IntPtr lpm_hLineApp, IntPtr hInstance, IntPtr lpfnCallback, string lpszFriendlyAppName, out int lpdwNumDevs, ref int lpdwAPIVersion, ref LINEINITIALIZEEXPARAMS lpLineInitializeExParams);
[DllImport("coredll")]
public static extern int lineOpen(IntPtr m_hLineApp, int dwDeviceID, out IntPtr lphLine, int dwAPIVersion, int dwExtVersion, IntPtr dwCallbackInstance, int dwPrivileges, int dwMediaModes, IntPtr lpCallParams);
[DllImport("coredll")]
public static extern int lineNegotiateAPIVersion(IntPtr m_hLineApp, int dwDeviceID, int dwAPILowVersion, int dwAPIHighVersion, out int lpdwAPIVersion, out LINEEXTENSIONID lpExtensionId);
[DllImport("cellcore")]
public static extern int lineGetGeneralInfo(IntPtr hLine, byte[] bytes);
[DllImport("cellcore")]
public static extern int lineGetGeneralInfo(IntPtr hLine, ref LINEGENERALINFO lineGenerlInfo);
[DllImport("coredll")]
public static extern int lineClose(IntPtr hLine);
[DllImport("coredll")]
public static extern int lineShutdown(IntPtr m_hLineApp);
public static string GetIMEI()
{
IntPtr hLine;
int dwNumDev;
int num1 = 0x20000;
LINEINITIALIZEEXPARAMS lineInitializeParams = new LINEINITIALIZEEXPARAMS();
lineInitializeParams.dwTotalSize = (uint)Marshal.SizeOf(lineInitializeParams);
lineInitializeParams.dwNeededSize = lineInitializeParams.dwTotalSize;
lineInitializeParams.dwOptions = 2;
lineInitializeParams.hEvent = IntPtr.Zero;
lineInitializeParams.hCompletionPort = IntPtr.Zero;
int result = lineInitializeEx(out hLine, IntPtr.Zero,
IntPtr.Zero, null, out dwNumDev, ref num1, ref lineInitializeParams);
if (result != 0)
{
throw new ApplicationException(string.Format("lineInitializeEx failed!\n\nError Code:{0}", result.ToString()));
}
int version;
int dwAPIVersionLow = 0x10004;
int dwAPIVersionHigh = 0x20000;
LINEEXTENSIONID lineExtensionID;
result = lineNegotiateAPIVersion(hLine, 0, dwAPIVersionLow, dwAPIVersionHigh, out version, out lineExtensionID);
if (result != 0)
{
throw new ApplicationException(string.Format("lineNegotiateAPIVersion failed!\n\nError Code: {0}", result.ToString()));
}
IntPtr hLine2 = IntPtr.Zero;
result = lineOpen(hLine, 0, out hLine2, version, 0, IntPtr.Zero, 0x00000002, 0x00000004, IntPtr.Zero);
if (result != 0)
{
throw new ApplicationException(string.Format("lineNegotiateAPIVersion failed!\n\nError Code: {0}", result.ToString()));
}
int structSize = Marshal.SizeOf(new LINEGENERALINFO());
byte[] bytes = new byte[structSize];
byte[] tmpBytes = BitConverter.GetBytes(structSize);
for (int index = 0; index < tmpBytes.Length; index++)
{
bytes[index] = tmpBytes[index];
}
result = lineGetGeneralInfo(hLine2, bytes);
// get the needed size
int neededSize = BitConverter.ToInt32(bytes, 4);
// resize the array
bytes = new byte[neededSize];
// write out the new allocated size to the byte stream
tmpBytes = BitConverter.GetBytes(neededSize);
for (int index = 0; index < tmpBytes.Length; index++)
{
bytes[index] = tmpBytes[index];
}
// fetch the information with properly size buffer
result = lineGetGeneralInfo(hLine2, bytes);
if (result != 0)
{
throw new ApplicationException(Marshal.GetLastWin32Error().ToString());
}
int size;
int offset;
//// manufacture
//size = BitConverter.ToInt32(bytes, 12);
//offset = BitConverter.ToInt32(bytes, 16);
//string manufacturer = Encoding.Unicode.GetString(bytes, offset, size);
//manufacturer = manufacturer.Substring(0, manufacturer.IndexOf('\0'));
//// model
//size = BitConverter.ToInt32(bytes, 20);
//offset = BitConverter.ToInt32(bytes, 24);
//string model = Encoding.Unicode.GetString(bytes, offset, size);
//model = model.Substring(0, model.IndexOf('\0'));
//// revision
//size = BitConverter.ToInt32(bytes, 28);
//offset = BitConverter.ToInt32(bytes, 32);
//string revision = Encoding.Unicode.GetString(bytes, offset, size);
//revision = revision.Substring(0, revision.IndexOf('\0'));
// serial number
size = BitConverter.ToInt32(bytes, 36);
offset = BitConverter.ToInt32(bytes, 40);
string serialNumber = Encoding.Unicode.GetString(bytes, offset, size);
serialNumber = serialNumber.Substring(0, serialNumber.IndexOf('\0'));
//// subscriber id
//size = BitConverter.ToInt32(bytes, 44);
//offset = BitConverter.ToInt32(bytes, 48);
//string subsciberId = Encoding.Unicode.GetString(bytes, offset, size);
//subsciberId = subsciberId.Substring(0, subsciberId.IndexOf('\0'));
//tear down
lineClose(hLine2);
lineShutdown(hLine);
return serialNumber;
}
}
public class LINEGENERALINFO
{
public int dwManufacturerOffset;
public int dwManufacturerSize;
public int dwModelOffset;
public int dwModelSize;
public int dwNeededSize;
public int dwRevisionOffset;
public int dwRevisionSize;
public int dwSerialNumberOffset;
public int dwSerialNumberSize;
public int dwSubscriberNumberOffset;
public int dwSubscriberNumberSize;
public int dwTotalSize;
public int dwUsedSize;
}
[StructLayout(LayoutKind.Sequential)]
public struct LINEEXTENSIONID
{
public IntPtr dwExtensionID0;
public IntPtr dwExtensionID1;
public IntPtr dwExtensionID2;
public IntPtr dwExtensionID3;
}
[StructLayout(LayoutKind.Sequential)]
public struct LINEINITIALIZEEXPARAMS
{
public uint dwTotalSize;
public uint dwNeededSize;
public uint dwUsedSize;
public uint dwOptions;
public System.IntPtr hEvent;
public System.IntPtr hCompletionPort;
public uint dwCompletionKey;
}
} |