[Gambas-user] Working with .so library

Tobias Boege taboege at ...626...
Thu Jun 15 11:19:14 CEST 2017


> All your help was very important for me, I now have completed my cash
> register software to the point where it does everything my company needs. I
> must say Gambas is a great language, it's very easy to learn from scratch,
> I'm surprised how obvious everything is. But there is a lot of work for me
> left to do mostly in terms of managing wrong human actions. My software
> works good if the employee doesn't do any mistakes, but that's unrealistic,
> so there's a lot of things I want to control and check. And that's where I'm
> stuck.
> 
> This library (which still calls itself a driver) theoretically is able to
> return a lot of values that I need, but I can't understand basic rules of
> how do we take output from a C-lib in Gambas.
> 
> From http://gambaswiki.org/wiki/howto/extern I understood that I need to
> locate a space in memory and pass a pointer to a library so that it can
> write data into that place in ram, which I would then read and set free.
> 
> So I have to declare a pointer, then Alloc(8) it, then pass it to my library
> and then read from it like it is a stream. Does this principle still work in
> current version of Gambas?
> 

If you do Alloc(8), then you get 8 bytes of memory. You most likely *don't*
want to read that like a stream, but use Integer@() or similar functions.

> What I don't understand is how I construct the code in my particular case.
> 
> To make an interface to the library I declare external pointer like this:
> 
>     Extern CreateFptrInterface(ver As Integer) As Pointer
> 
> Then I declare some pointers that I'll use with help of the interface I
> created:
> 
>     Extern put_DeviceEnable(p as Pointer, mode as Integer)
> 
>     Extern GetStatus(p as Pointer, StatRequest as String)
> 
> Then I declare the pointer which will be that interface:
> 
>     Public kkmDrv as Pointer
> 
> So then in sub I can do
> 
> kkmDrv = CreateFptrInterface(12) ' this establishes the interface
> 
> put_DeviceEnabled(kkmDrv, 1) ' this transfers the comand to the library
> through the interface.
> 
> And it works great.
> 
> But then If I want to get some data from the library, as I understand, I
> have to declare another pointer, allocate ram for it and pass my request.
> 
> I don't understand how should I pass that pointer to GetStatus() while also
> passing my interface pointer to it, let alone reading data back. Totally
> confused.
> 

This entirely depends on how the C functions in your library are declared.
I don't know about your specific library but commonly the occurence of an
error is indicated by an integer return code, e.g. this might be the
signature of one of the functions in your library:

  int myfunction(void *interface, int argument)

If the documentation says that the return value (int) of this function
indicates an error, then you just need to get that return value back into
your Gambas program, which you accomplish by declaring the function in
Gambas as

  Extern myfunction(interface As Pointer, argument As Integer) As Integer

(notice the trailing "As Integer"). Then you can use "myfunction" in your
Gambas code like any other function and get and interpret its return value.

So, if this convention for error reporting is used, it is much simpler to
get information about errors, without using Alloc() and co. Your library
may use a different convention which actually involves pointers, but
I wouldn't know.

Regards,
Tobi

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list