[Gambas-devel] EXTERN Calls

Scott, Vince Vince.Scott at ...440...
Mon Mar 20 22:33:56 CET 2006


On Mon, 20 Mar 2006, Scott, Vince wrote:

> 1.) Sample 1
>
>
> int GetName(char* pName)
> {
>   sprintf(pName, "%s", "TEST");
>
>   return 0;
> }

If you are using strPtr it is best to also pass the string length and
use
snprintf rather than regular printf.  This code is vulnerable to memory
thrashing if the supplied string is too short.

> 2.) Sample 2
>
> char* GetAName()
> {
>   Char* buf = malloc(5);
>
>   sprintf(buf, "%s", "TEST");
>
>   return buf;
>
> }

You are leaking memory by doing it this way becuase your Gambas program
has no way to free the memory consumed by malloc().  I'd stick to
passing
the string (and length) by reference.

****

I know this is sloppy...just hacking to get a string to be passed by to
Gambas.

> 1.) Sample 1
>
> Extern GetName(ptr AS Pointer) AS Integer IN "libtest"
>
> Public Sub Test1()
>
>   Dim ret   AS Ingteger
>   Dim ptr   AS Pointer
>   Dim sTest AS String

Try:
    sTest=STRING(".",5)
  
    *** THIS GIVES AN ERROR... IT EXPECTS sTest to be a Pointer Not a
String.

    ptr=strPtr(sTest)
In place of:
    ptr = Alloc(sTest, 5)

>   ret = GetName(ptr)
>   Message("String= " & sTest)

Delete this line if using STRING(".",5):
    Free(ptr)

> End

.=================================================.
|  Christopher BRIAN Jack aka "Gau of the Veldt"  |
+================================================='
| brian _AT_ brians-anime _DOT_ com
`=================================================-
Hi Spambots, my email address is sputnik at ...418...


-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting
language
that extends applications into web and mobile media. Attend the live
webcast
and join the prime developer group breaking into this new coding
territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Gambas-devel mailing list
Gambas-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gambas-devel


This communication is confidential and may be legally privileged.  If you are not the intended recipient, (i) please do not read or disclose to others, (ii) please notify the sender by reply mail, and (iii) please delete this communication from your system.  Failure to follow this process may be unlawful.  Thank you for your cooperation.




More information about the Devel mailing list