KlariSoft Inc.
 About Us | Articles | Examples | Services | Products | TX Text Control 

   home page NEWS | LINKS   
How to return a CSTRING from Clarion function  Articles
Sometimes it might be necessary to return a CSTRING from Clarion function. For instance you need to pass a value to the program which is not written by you and this program expects a dll with the following function: Afunc(USHORT),CSTRING,C,RAW. (In C it looks like: char* afunc(int);)

In Windows and other systems STRING is usually an array of characters ending with the ASCII 0 character. Strings returned by the functions (and parameter strings) are passed as a string address.

Clarion allows to call functions returning 0-terminated strings written in other languages (e.g. Afunc(USHORT),CSTRING,C,RAW ) but does not allow to create such functions. The help explicitly states "Variable return types are provided just for prototyping external library functions written in another language) which return only the address of data they are not valid for use on Clarion language procedures. " The chapter about multi-language programming from the "Programmers Guide" says that if you need to pass a CSTRING to a program written in another language you can not do it via RETURN but should use *CSTRING parameter instead.

If you are not satisfied with that you could try the following approach:
Create a function which return LONG value and return the address of a static CSTRING variable (using Clarion ADDRESS operator).

Afunction (SHORT ID), LONG,C,Name('_afunction')


STATIC attribute for the LOC:RetStr is required in order to place this variable in a static memory. Otherwise this variable will disappear right after RETURN operator. On the calling side this function needs to be described as:

Afuncton(SHORT ID),CSTRING,C,RAW,NAME('_afunction')

in a C program this function needs to be described as:
char* afunction(int);          

  Web-Design © Kirsoft KSNews™, 2001 Copyright © KlariSoft Inc., 2002