Programatically forwarding calls using TAPI

Download the source code

Recently one of my friend asked me how to forward calls using the USSD commands. This link describes how to forward calls using USSD codes. I tried a lot using my previous article code but ended up with no success. The USSD command are sending successfully but always the result is “UNKNOWN APPLICATION”. Then I decided to work on a sample using the TAPI API lineForward.

The lineForward API is used to programatically forward calls. The syntax of the API is defined as:

LONG WINAPI lineForward(HLINE hLine,DWORD bAllAddresses,
        DWORD dwAddressID,LPLINEFORWARDLIST const lpForwardList,
        DWORD dwNumRingsNoAnswer,LPHCALL lphConsultCall,LPLINECALLPARAMS const lpCallParams);

The first parameter is the handle to the line (Cellular Line), second a boolean value indicating all originating addresses of the line or a single address of the line. Third is the address id of the line. Fourth is a pointer to variably sized structure that describes the forwarding instructions. Fifth is number of rings that has to be considered as no answer. Sixth is a pointer to handle to a call which will be filled with handle to a consultation call (only used in some telephony environments). Seventh is pointer to LINECALLPARAMS, if NULL specified default call parameter will be used.

Following steps are needed to forward calls:

1. Initialize TAPI
2. Open the Cellular Line
3. Allocate and initialize the LINEFORWARDLIST variable sized structure
4. Call lineForward API

Allocating and initializing the LINEFORWARDLIST structure is little bit complex. This is a pointer to a variable sized structure and is defined as:

typedef struct lineforwardlist_tag {
  DWORD dwTotalSize;
  DWORD dwNumEntries;
  LINEFORWARD ForwardList[1];
} LINEFORWARDLIST, FAR* LPLINEFORWARDLIST;

The dwTotalSize member is the total size of the structure, dwNumEntries member is the number of ForwardList array. The LINEFORWARD structure is defined as:

typedef struct lineforward_tag {  
DWORD dwForwardMode;  
DWORD dwCallerAddressSize;  
DWORD dwCallerAddressOffset;  
DWORD dwDestCountryCode;  
DWORD dwDestAddressSize;  
DWORD dwDestAddressOffset;  
DWORD dwCallerAddressType;  
DWORD dwDestAddressType;
} LINEFORWARD,  *LPLINEFORWARD;

The dwDestAddressOffset specifies the offset of the destination address from the starting of the structure. The dwDestAddressSize specifies the size of the destination address in bytes. Same in the case of dwCallerAddressOffset and dwCallerAddressSize and specifies the caller address and size. To allocate memory for the forward list, first we have to find out the total number of bytes needed. This will be:

sizeof(LINEFORWARDLIST) + (sizeof(LINEFORWARD) * number of entries - 1) + size of all phone numbers

After allocating the memory we need to initialize the members of the structure and append the phone numbers. The API lineForward returns a positive integer if successful otherwise a negative value indicating the error. To get the current status of the call forward, lineGetAddressStatus API is used. This returns the result in a variable sized array. The sample application presented here retrieves the call forward status and display it is dialog. Users can change the settings and upon exit the application sets new call forward information. Hope this gives you an introduction on how to programatically forward calls.

7 Comments

Add yours →

  1. I have no words to express my gratitude to you. You have done a fabulous work for me and I thank you from my soul. I searched deep in the internet about this but not a single site had a solution to it. But you are the only single person who had made it possible. I am sure that you’ll be a great name in the near future. I did’nt had any hope to find success in it so I was depressed.

    Thanx to U once again my dear friend for helping me.

  2. I have no words to express my gratitude to you. You have done a fabulous work for me and I thank you from my soul. I searched deep in the internet about this but not a single site had a solution to it. But you are the only single person who had made it possible. I am sure that you’ll be a great name in the near future. I did’nt had any hope to find success in it so I was depressed.

    Thanx to U once again my dear friend for helping me.

  3. Rahul Jaiswal

    May 13, 2010 — 6:22 am

    Hi krishnaraj,

    Its really a nice work done by you.
    Can you help me in converting this to C# or you have C# version of this code, as i wont have any work experience in C++, so its a bit difficult for me.

    Thanks in advance.

  4. Rahul Jaiswal

    May 13, 2010 — 6:22 am

    Hi krishnaraj,

    Its really a nice work done by you.
    Can you help me in converting this to C# or you have C# version of this code, as i wont have any work experience in C++, so its a bit difficult for me.

    Thanks in advance.

  5. Hi krishnaraj,

    Its really a nice work done by you.
    Can you help me in converting this to C# or you have C# version of this code, as i wont have any work experience in C++, so its a bit difficult for me.

    Thanks in advance.

  6. Hi krishnaraj,

    Its really a nice work done by you.
    Can you help me in converting this to C# or you have C# version of this code, as i wont have any work experience in C++, so its a bit difficult for me.

    Thanks in advance.

  7. Hi Sir,

    Its really very helpful.

    Can you please provide us make a call sorce code in C# like call forwarding.

    Thank You,
    Indrajit

Leave a Reply