CF utilizzare le api di Windows Mobile, per effettuare le telefonate o rilevare informazioni alla SIM tramite C#

Questo esempio di codice, illustra come utilizzare le api di Windows Mobile, per effettuare delle telefonate e rilevare informazioni della SIM.

Effettuare una telefonata:

Namespace:

 

using

 

System.Runtime.InteropServices;

A livello di classe creare la seguente struttura e i due campi.

 

 

 

 

 

private static long PMCF_DEFAULT = 0×00000001;private static long PMCF_PROMPTBEFORECALLING = 0×00000002;private struct PhoneMakeCallInfo{

 

 

 

 

 

 

}

 

public IntPtr cbSize;public IntPtr dwFlags;public IntPtr pszDestAddress;public IntPtr pszAppName;public IntPtr pszCalledParty;public IntPtr pszComment;

Dichiarazione di api per effettuare la telefonata

 

[

 

 

DllImport("phone.dll")]private static extern IntPtr PhoneMakeCall(ref PhoneMakeCallInfo ppmci);

Funzione che effettua la chiamata

 

unsafe

{

 

 

 

 

private void Telefona(string numero)bool avvisa = true;IntPtr res;//numero += ”; 

 

{

 

info.cbSize = (

info.pszDestAddress = (

 

{

info.dwFlags = (

}

 

 

char[] cPhoneNumber = numero.ToCharArray();fixed (char* pAddr = cPhoneNumber)PhoneMakeCallInfo info = new PhoneMakeCallInfo();IntPtr)Marshal.SizeOf(info);IntPtr)pAddr;if (avvisa)IntPtr)PMCF_PROMPTBEFORECALLING;else{

info.dwFlags = (

}

res = PhoneMakeCall(

}

}

 

IntPtr)PMCF_DEFAULT;ref info);

Eseguire la funzione per effettuare la chiamata

 

 

{

Telefona(

}

 

private void btnChiama_Click(object sender, EventArgs e)“4250010001″);

 

Rilevare il numero di telefono, individuare gli sms che si possono ricevere ed il numeri di quelli ricevuti.

Dichiarazione a livello di classe

 

//Identificazione numero

 

 

 

public enum AddressType{

 

 

Unknown,

 

 

International,

 

 

National,

 

 

NetworkSpecific,

 

 

Subscriber,

 

 

Alphanumeric,

 

 

Abbreviated

}

 

 

//informazioni numero 

 

public struct PhoneAddress{

 

 

/// <summary>The address type.</summary> 

 

 

public AddressType AddressType;/// <summary>The phone number in string format.</summary> 

}

 

[

 

 

public String Address;private static long SERVICE_PROVIDER = 0x00006F46;StructLayout(LayoutKind.Sequential)]private struct SimRecord{

 

 

 

 

 

}

 

public IntPtr cbSize;public IntPtr dwParams;public IntPtr dwRecordType;public IntPtr dwItemCount;public IntPtr dwSize;Dichiarazione api

 

[

 

[DllImport("cellcore.dll")]

public static extern int SimInitialize(uint dwFlags, int lpfnCallBack, uint dwParam, ref int lphSim);

//private static extern IntPtr SimInitialize(IntPtr dwFlags, IntPtr lpfnCallBack, IntPtr dwParam, out IntPtr lphSim);[DllImport("cellcore.dll")]

private static extern IntPtr SimGetRecordInfo(IntPtr hSim, IntPtr dwAddress, ref SimRecord lpSimRecordInfo);

[DllImport("cellcore.dll")]

private static extern IntPtr SimReadRecord(IntPtr hSim, IntPtr dwAddress, IntPtr dwRecordType, IntPtr dwIndex, byte[] lpData, IntPtr dwBufferSize, ref IntPtr lpdwBytesRead);

[DllImport("cellcore.dll")]

private static extern IntPtr SimDeinitialize(IntPtr hSim);

[DllImport("cellcore.dll")]

public static extern int SimGetSmsStorageStatus(int hSim, uint dwStorage, ref uint lpdwUsed, ref uint lpdwTotal);

 

 

DllImport(“sms.dll”)]private static extern IntPtr SmsGetPhoneNumber(IntPtr psmsaAddress);Funzione che rileva il numero del telefono e tipo

 

unsafe private PhoneAddress GetPhoneNumber()

{

PhoneAddress phoneaddr = new PhoneAddress();

Byte[] buffer = new Byte[516];

fixed (byte* pAddr = buffer)

{

IntPtr res = SmsGetPhoneNumber((IntPtr)pAddr);

if (res != IntPtr.Zero)

throw new Exception(“Could not get phone number from SIM”);

byte* pCurrent = pAddr;

phoneaddr.AddressType = (AddressType)Marshal.ReadInt32((IntPtr)pCurrent);

pCurrent += Marshal.SizeOf(phoneaddr.AddressType);

phoneaddr.Address = Marshal.PtrToStringUni((IntPtr)pCurrent);

}

return phoneaddr;

}

 

Richiamare la funzione da un evento click del pulsante (numero telefono e tipo)

 

private void btnnumero_Click(object sender, EventArgs e)

{

string informazioni = “Numero: “ + GetPhoneNumber().Address + ” Tipo: “ + GetPhoneNumber().AddressType;

MessageBox.Show(informazioni);

}

 

Richiamare la funzione da un evento click di un pulsante per la gestione degli sms (totali e ricevuti)

 

private

{

 

 

 

 

SimInitialize(0, 0, 0,

SimGetSmsStorageStatus(hSim, SIM_SMSSTORAGE_SIM,

 

 

}

 

void btnSms_Click(object sender, EventArgs e)int hSim = 0;uint smsUsed = 0;uint smsTotal = 0;uint SIM_SMSSTORAGE_SIM = 0×2;ref hSim);ref smsUsed, ref smsTotal);MessageBox.Show(“Sms Usati: “ + smsUsed.ToString());MessageBox.Show(“Sms Totali: “ + smsTotal.ToString());Tramite la parola download è possibile scaricare il file di esempio

Download

       

Lascia un commento