Esempio n. 1
0
// code to get IMEI
        public static string GetTerminalImeiAddress()
        {
            Telephony t = null;
            Line line = null;
            try
            {
                t = new Telephony();
                t.Initialize();
                line = t.CreateLine(0,MediaMode.AutomatedVoice, CallPrivilege.Monitor);

                byte[] buffer = new byte[512];
                //write size
                BitConverter.GetBytes(512).CopyTo(buffer, 0);

                if (lineGetGeneralInfo(line.hLine, buffer) != 0)
                {
                    throw new System.ComponentModel.Win32Exception(
                        System.Runtime.InteropServices.Marshal.GetLastWin32Error(),
                        "TAPI Error: " + System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString("X"));
                }

                int serialsize = BitConverter.ToInt32(buffer, 36);
                int serialoffset = BitConverter.ToInt32(buffer, 40);
                string imei = System.Text.Encoding.Unicode.GetString(buffer, serialoffset, serialsize);
                imei = imei.Substring(0, imei.IndexOf(Convert.ToChar(0)));

                return imei;

            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
            finally
            {
                try
                {
                    if (line != null)
                        line.Dispose();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                try
                {
                    if (t != null)
                        t.Shutdown();
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

            }

            return string.Empty;
        }