Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   How to send arguments to function pointer (http://www.go4expert.com/forums/send-arguments-function-pointer-t658/)

sharmila 24Mar2006 13:09

How to send arguments to function pointer
 
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.

coderzone 24Mar2006 14:37

Re: How to send arguments to function pointer
 
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: CPP

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.

sharmila 24Mar2006 14:55

Re: How to send arguments to function pointer
 
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.

coderzone 24Mar2006 15:01

Re: How to send arguments to function pointer
 
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.

sharmila 24Mar2006 15:58

Re: How to send arguments to function pointer
 
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.

coderzone 24Mar2006 19:11

Re: How to send arguments to function pointer
 
Quote:

Here MyMOSMSNotify is the function pointer which they passed without arguments.
No they may have passed it with arguments also.

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

sharmila 28Mar2006 09:58

Re: How to send arguments to function pointer
 
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.

coderzone 28Mar2006 10:58

Re: How to send arguments to function pointer
 
Quote:

Originally Posted by sharmila
how the arguments of a function pointer are passed when the function pointer is passing as an argument to some other function

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.

sharmila 28Mar2006 11:17

Re: How to send arguments to function pointer
 
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.

coderzone 28Mar2006 12:12

Re: How to send arguments to function pointer
 
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.


All times are GMT +5.5. The time now is 23:01.