Re: IUS, External C procedure and string parameter problem
Posted in 1997
dominic (dominic@netcentre.com.sg) wrote:
: I am unable to pass string from stored procedures to
: external c functions. However, I am able to pass integer
: over to. Below are my codes. Please advice. Thanks in advance.
1. When you pass arguments longer than four bytes into a 'C'
function, you need to do so by reference (pointer) rather
than by values. In the case of variable length arguments,
IUS uses a varlena (Variable Length Array) structure, and
provides a set of macros and library functions for dealing
with the varlena.
2. The 'printf' makes less than zero sense in the server. When you
do 'printf', you are actually doing fprintf on the STDOUT
file descriptor. Doing any kind of write to a file handle
using standard POSIX stuff is not encouraged in the server.
Such callscause the OS to block the entire DBMS process.
Operations like fork() and exec() ( and pipe()) are therefore
also difficult. You need to do these in an extent VP.
3. Your function should look like this;
(Note: This is typing. I haven't actually tested or even compiled this.)
mi_lvarchar *
MakeMail (
mi_lvarchar * pvlEmail,
mi_lvarchar * pvlMsg,
mi_lvarchar * pvlSubj,
mi_lvarchar * pvlSender,
mi_integer i,
MI_FPARAM pFParam
)
{
mi_lvarchar * pvlRetVal;
mi_char * pchEmail,
* pchMsg,
* pchSubj,
* pchSender,
* pchRetVal;
pchEmail = mi_get_vardata(pvlEmail); /* Get the appropriate pointers */
pchMsg = mi_get_vardata(pvlMsg);
pchSubj = mi_get_vardata(pvlSubj);
pchSender = mi_get_vardata(pvlSender);
/*
* Allocate the appropriately sized return structure.
**/
pchRetVal = mi_alloc(sizeof("Mail: Subject: From: To: ") +
mi_get_varlen(pvlEmail),
mi_get_varlen(pvlMsg),
mi_get_varlen(pvlSubj),
mi_get_varlen(pvlSender) + 10);
sprintf(pchRetVal,"Subject: %s\\n From: %s\\n To: %s\\n %s %d\\n",
pchSubj, pchSender, pchEmail, pchMsg, i);
pvlRetVal = mi_string_to_lvarchar(pchRetVal);
return (pvlRetVal);
}
CREATE FUNCTION MakeMail ( lvarchar, lvarchar, lvarchar, lvarchar, integer)RETURNS lvarchar
AS EXTERNAL ---
LANGUAGE C NOT VARIANT;
EXECUTE FUNCTION MakeMail();