dbmsghandle

Description

Install a user function to handle server messages.

Syntax

int (*dbmsghandle(handler))()
 
int              (*handler)();

Parameters

handler

A pointer to the user function that will be called whenever DB-Library receives an error or informational message from the server. DB-Library calls this function with eight parameters listed in Table 2-21.

Table 2-21: Message handler parameters

Parameter

Meaning

dbproc

The affected DBPROCESS.

msgno

The current message’s number (datatype DBINT). These numbers are documented in the sysmessages table.

msgstate

The current message’s error state number (datatype int). These numbers provide Sybase Technical Support with information about the context of the error.

severity

The current message’s information class or error severity (datatype int). These numbers are documented in the Adaptive Server documentation.

msgtext

The null-terminated text of the current message (datatype char *).

srvname

The null-terminated name of the server that generated the message (datatype char *). A server’s name is stored in the srvname column of its sysservers system table. It is used in server-to-server communication; in particular, it is used when one server logs into another server to perform a remote procedure call. If the server has no name, srvname will be a length of 0.

procname

The null-terminated name of the stored procedure that generated the message (datatype char *). If the message was not generated by a stored procedure, procname will be a length of 0.

line

The number of the command batch or stored procedure line that generated the message (datatype int). Line numbers start at 1. The line number pertains to the nesting level at which the message was generated. For instance, if a command batch executes stored procedure A, which then calls stored procedure B, and a message is generated at line 3 of B, then the value of line is 3. line will be 0 if there is no line number associated with the message. Circumstances that could generate messages without line numbers include a login error or a remote procedure call (performed using dbrpcsend) to a stored procedure that does not exist.

The message handler must return a value of 0 to DB-Library.

Message handlers on Microsoft Windows must be declared with CS_PUBLIC, as shown in the following example. For portability, callback handlers on other platforms should be declared CS_PUBLIC as well.

The following example shows a typical message handler routine:

#include   <sybfront.h> 
#include   <sybdb.h> 
 
 int CS_PUBLIC msg_handler(dbproc, msgno, msgstate,
     severity, msgtext, srvname, procname, line) 
 
 DBPROCESS         *dbproc; 
 DBINT             msgno; 
 int               msgstate; 
 int               severity; 
 char              *msgtext; 
 char              *srvname; 
 char              *procname; 
 int               line; 
 
 { 
      printf ("Msg %ld, Level %d, State %d\n",  
          msgno, severity, msgstate); 
       if (strlen(srvname) > 0) 
           printf ("Server ’%s’, ", srvname); 
       if (strlen(procname) > 0) 
           printf ("Procedure ’%s’, ", procname); 
       if (line > 0) 
           printf ("Line %d", line); 
 
       printf("\n\t%s\n", msgtext); 
 
       return(0); 
 } 

Returns

A pointer to the previously installed message handler or NULL if no message handler was installed before.

Usage

See also

dberrhandle, dbgetuserdata, dbsetuserdata