1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to send arguments to function pointer

Discussion in 'C++' started by sharmila, Mar 24, 2006.

  1. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi all,
    I never worked on function pointers and so I don't know much about them.At present I am working on BREW.In it's API reference it is menctioned a function pointer as

    typedef void (*PFNSMSSTATUS)(void * pUser, int status);

    I created a variable as
    PFNSMSSTATUS abc;

    How to pass arguments to this variable?If I did not pass any arguments then also it is not showing any errors.
    Thanks in advance.
     
  2. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    PFNSMSSTATUS abc; this creates a pointer function variable. You need to be assigning it an address to a function.
    PFNSMSSTATUS abc = func;

    Func is defined as
    Code:
    void func(void * pUser, int status)
    {
    	cout<<"I am the func"<<endl;
    }
    Now you need to call the function using the parameters as follows
    abc(NULL,1);

    Enjoy coding.
     
  3. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Thanks a lot.I tried that one and I got it.Another doubt is won't it show any error if we does not pass arguments.
    Once again thank you.Thank you very much.
     
  4. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    If you dont pass the arguments then it will show error if there isnt any default parameter as the case is with the normal functions.
     
  5. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    In that API it is given that

    PFNSMSSTATUS:
    Description:
    This is the prototype of the notification function whose address must be passed to ITAPI_SendSMS() if the application wants to know about the status of the MO-SMS just sent. When the SMS msg is sent using ITAPI_SendSMS(), this notifier function is invoked by BREW to inform the application about the status of the msg delivery

    Definition:
    typedef void (*PFNSMSSTATUS)(void * pUser, int status);

    what does it mean.

    In an example they have given they are also not passing any arguments.In C are we having default arguments?B'caz i studied they are in C++.

    ITAPI_SendSMS (pITapi, "8581112222", "Hello World", 0, MyMOSMSNotify, pMe );
    Here MyMOSMSNotify is the function pointer which they passed without arguments.

    Thank you.
     
    Last edited: Mar 24, 2006
  6. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    No they may have passed it with arguments also.

    They might have declared it as.
    PFNSMSSTATUS MyMOSMSNotify(void*, int)
     
  7. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Thanks again.i tried that one.But I got one more doubt.
    how the arguments of a function pointer are passed when the function pointer is passing as an argument to some other function.

    static void sendNotifier(void*,int);
    void* retVal;
    int status;

    PFNSMSSTATUS ptrFunc;
    ptrFunc = sendNotifier;

    (*ptrFunc)(retVal,status); // statement 1

    ITAPI_SendSMS (pMe->pITapi, "1234567890", "Hello world",0, \
    (*ptrFunc)(retVal,status),pMe ); // statement 2

    when I tried for statement 1 it is working where as statement 2 is giving errors.Some one told to me that we should not pass the values like that.Instead of use

    ITAPI_SendSMS (pMe->pITapi, "1234567890", "Hello World",0, \
    ptrFunc,retVal,status,pMe );

    this statement is working.but function is executing after this statement is executed.i.e after this statement when i am trying to print the value of retVal it is printing junk value.After that it is printing statements in function pointer.
    So,how can we get the value of retVal immediatyely after ITAPI_SendSMS();

    Thanks once again.
     
  8. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    As a parameter passed in the function ITAPI_SendSMS. Just post the description of the API <ITAPI_SendSMS> and will let you know how the argument is passed.
     
  9. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    ITAPI_SendSMS()

    Description:

    This method is used to send SMS messages from the handset.This function can be used to send text messages to either a specific BREW application on another handset or a general text message to the handset. It sends messages to a destination mobile number or an email ID. When this function is called, it is likely that it will result in a SUSPEND/RESUME sequence to the BREW application. This is initiated by the handset when it sends the SMS message out.
    The sequential flow of control is as follows:

    1. The application invokes ITAPI_SendSMS()

    2. The handset begins the process of sending the SMS message out

    3. The notification function specified by pfn is invoked with the status of the message delivery


    Associated with this process, the application may receive EVT_APP_SUSPEND and EVT_APP_RESUME events sometime after step 1.


    Prototype:
    int ITAPI_SendSMS
    (
    ITAPI *pITapi,
    const char * pszDst,
    const char * pszMsg,
    AEECLSID clsDst,
    PFNSMSSTATUS pfn,
    void *pUser
    )

    Parameters:
    pITapi : Pointer to the ITAPI Interface object

    pszDst : Number or email ID of the destination where message must be sent to. If this is set to NULL and if clsDst is non-zero, this function sends the EVT_APP_MESSAGE event to the application (clsDst) on the local handset and the dwParam of that event shall contain pszMsg. In this case, the return value of the function is the same as the return value of ISHELL_SendEvent(). The notification function will not be called since this is a local delivery of the message.

    pszMsg : Text message to be sent to the destination mobile. ITAPI_SendSMS() interprets the text message to be in encoding specified by device item AEE_DEVICEITEM_TAPI_SMS_ENCODING.

    clsID : If non-zero, it specifies the class ID of the BREW application on the destination mobile to which this message must be sent

    pfn : Notification function that is invoked to inform the status of the SMS message sent

    pUser : User data to be sent to the notification function

    Examples:

    Following examples demonstrate the use of ITAPI_SendSMS() on a device where
    device item AEE_DEVICEITEM_TAPI_SMS_ENCODING value is AEE_ENC_ISOLATIN1.
    ITAPI_SendSMS (pITapi, "8581112222", "Hello World", 0, MyMOSMSNotify, pMe );
    ITAPI_SendSMS ( pITapi, "foo@sample.com", "Hello World", 0, MyMOSMSNotify, pMe );
    Likewise, following examples demonstrate the use of ITAPI_SendSMS() on a device where
    device item AEE_DEVICEITEM_TAPI_SMS_ENCODING value is AEE_ENC_UNICODE.
    ITAPI_SendSMS (pITapi, "8581112222", (char*)STRTOWSTR("Hello World", pBuffer, nBufSize), 0, MyMOSMSNotify, pMe );
    ITAPI_SendSMS ( pITapi, "foo@sample.com", (char*)STRTOWSTR("Hello World", pBuffer, nBufSize), 0, MyMOSMSNotify, pMe );


    Return Value:
    SUCCESS, if message is accepted for delivery by ITAPI. This success doesn't indicate
    the successful submission of the message to SMSC for delivery.


    EITEMBUSY, if ITAPI determines that the device is busy and cannot send this SMS.
    Generally, a SMS message cannot be sent if the notification function has not yet been
    called for a previously sent SMS message.


    EBADPARM, if pszMsg is NULL or pszDst and clsDst are both set to NULL.


    EBADCLASS, if TAPI is not enabled on this handset.


    EFAILED, if there is a general failure in sending the SMS.



    Comments:

    NOTE: This function may cause some devices to experience difficulties when attempting to send long messages.

    NOTE: The status passed to the notification function will be success if message was successfully submitted to SMSC for delivery. The status passed to the notification function can be EITEMBUSY or EFAILED to indicate problem in submitting the message to SMSC for delivery.


    Version:
    Introduced BREW Client 2.0

    ------------------------------------------------------------
    PFNSMSSTATUS:
    Definition:
    typedef void (*PFNSMSSTATUS)(void * pUser, int status);

    Members:
    pUser: User-specific data that will be passed back to the notification function when invoked by BREW

    status: Status of the msg delivery. This will be set to AEEMOSMS_ERR_*
    AEEMOSMS_ERR_NO_ERR : SUCCESSfully sent. Indicates the message has been sent to network.
    AEEMOSMS_ERR_OUTOF_RESOURCES : Device is temporarily out of resources.
    AEEMOSMS_ERR_NETWORK_NOT_READY : Network is not reachable or ready for device to communicate.
    AEEMOSMS_ERR_TRANSPORT_TEMP : Network temporarily refused message. Application may retry.
    AEEMOSMS_ERR_TRANSPORT_PERM : Network rejected the message.
    AEEMOSMS_ERR_INTERNAL : An internal error occured.



    here I am trying to get the value of pUser which is given by the user depending on the value of status.
     
  10. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    In
    Code:
    int ITAPI_SendSMS
    (
    ITAPI *pITapi, 
    const char * pszDst, 
    const char * pszMsg, 
    AEECLSID clsDst,
    PFNSMSSTATUS pfn, 
    void *pUser
    )
    The last parameter is used to send the data to the function.
    pUser : User data to be sent to the notification function
    Declare a class/structure and pass the pointer to the class/structure or this pointer to the function and the data will be available in your function.
     
  11. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Thanks for telling me my mistake.but the problem remains like that.

    ptrFunc = sendNotifier;

    ITAPI_SendSMS (pMe->pITapi, "1234567890", "Hello World",0,ptrFunc,retVal );
    DBGPRINTF("aaaa----%s",(char*)retVal);

    Inside sendNotifier
    Code:
    static void sendNotifier(void* user,int status)
    {
        DBGPRINTF("inside function sendNotifier--%d",status);
                    switch(status)
    	{
    		case AEEMOSMS_ERR_NO_ERR:
    			STRCPY((char*)user,"Message send sucessesfully");
    			break;
    			
    		case AEEMOSMS_ERR_OUTOF_RESOURCES:
    			STRCPY((char*)user,"Device is temporarily out of resources.");
    			break;
    			
    		case AEEMOSMS_ERR_NETWORK_NOT_READY:
    			STRCPY((char*)user,"Network is not reachable or ready for device to communicate.");
    			break;
    			
    		case AEEMOSMS_ERR_TRANSPORT_TEMP:
    			STRCPY((char*)user,"Network temporarily refused message. Application may retry.");
    			break;
    			
    		case AEEMOSMS_ERR_TRANSPORT_PERM:
    			STRCPY((char*)user,"Network rejected the message.");
    			break;
    			
    		case AEEMOSMS_ERR_INTERNAL:
    			STRCPY((char*)user,"An internal error occured.");
    			break;
    			
    		default:
    			STRCPY((char*)user,"Unknown ERROR");
    	}
    	
    	DBGPRINTF("end of fun.--%s",(char*)user);
    }
    
    This is the code.When I am executing this one
    first DBGPRINTF("aaaa----%s",(char*)retVal); is executing and then
    DBGPRINTF("end of fun.--%s",(char*)user); is executing and so I am not getting any data into retVal.what should i do.
     
  12. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    This is probably because ITAPI_SendSMS function calls the procedure and does not wait until the procedure has processed the message. It returns immediately.
     
  13. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    ok.Thank you.
    In general, how can we write a code like that.
     
  14. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    What type of code.
     
  15. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    How is it possible.Is there any extra code for that.
     
  16. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    Probably some other API. Something like SendMessage and PostMessage API's
     
  17. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi,
    I am not asking about API's.In C as far as I know when a function is calling another function,after executing the called function only the remaining part of calling will execute.How can it execute without wainting for the called one.
    Code:
    void fun2();
    main()
    {
           printf("aaa");
           fun2();
           printf("bbb");
    }
    void fun2()
    {
        printf("fff");
    }
    o/p:aaafffbbb
    i.e after executing fun2() the remaining part of fun1() is executing.It is waiting till fun() completes.But ITAPI_SendSMS is not waiting.
    So, for the above prog. if we want o/p like aaabbbfff what we have to do?
     
  18. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    This is not a simple function call. Its a Message passing and as coderzone said messgae can be sent via 2 methods. One which waits for the message to end and other returns immediately.
     
  19. sharmila

    sharmila New Member

    Joined:
    Mar 24, 2006
    Messages:
    75
    Likes Received:
    1
    Trophy Points:
    0
    Hi,
    Thank you.i.e first I have to study about inter-process communication.I think then I will came to know how is it happening.Thank you coderzone for your great help.Thank you shabbir.
     
  20. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    Its my pleasure.
     

Share This Page