How to access this C function defined in an external library

Post your Gambas programming questions here.
Post Reply
dmontaine
Posts: 5
Joined: Thursday 3rd February 2022 3:05pm

How to access this C function defined in an external library

Post by dmontaine »

The external library is defined as: Library "./.hidden/qmclilib"

The c function definition is:

char * QMExecute(char * Cmnd , int * Err )

What would be the Gambas3 EXTERN statement be and how would I use the function in code?

I have tried a lot of things and always get a segmentation fault error.

The command is executed but then the segmentation fault is triggered.

The documentation defines the correct VB.Net code as:

QMExecute(ByVal Cmnd as String, ByRef Err as Integer) as String
S = QMExecute("RUN MYPROG", Err)
dmontaine
Posts: 5
Joined: Thursday 3rd February 2022 3:05pm

Re: How to access this C function defined in an external library

Post by dmontaine »

What I have tried is:

Extern QMExecute(Cmnd As String, ByRef ErrNo As Pointer) As String

dim RetVal as string
dim Cmnd as String
Dim ErrNoPtr as pointer

RetVal = QMExecute(Cmnd, ErrNoPtr)
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: How to access this C function defined in an external library

Post by BruceSteers »

Sorry i am not great with libraries.

but this path...
"./.hidden/qmclilib"

You should know anything in the .hidden folder will not pack with the executable. it is only used by the IDE

place in any non hidden folder or in a folder called ".public". items in a hidden .public folder will be included.

Respects
BruceS
If at first you don't succeed , try doing something differently.
BruceS
User avatar
thatbruce
Posts: 168
Joined: Saturday 4th September 2021 11:29pm

Re: How to access this C function defined in an external library

Post by thatbruce »

Even with that I fail to see how anything "inside" the executable archive could be "executed"?
Have you ever noticed that software is never advertised using the adjective "spreadable".
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: How to access this C function defined in an external library

Post by vuott »

dmontaine wrote: Monday 11th December 2023 12:04am the c function definition is:

char * QMExecute(char * Cmnd , int * Err )
We'ld need to know the work of each parameter, however "de visu" l suggest:
Private Extern QMExecute(Cmnd As Pointer, Err As Pointer) As Pointer

..use in routine...

Dim comm, RetVal As Pointer
Dim ErrNoPtr As Integer

comm = Alloc("RUN MYPROG")

RetVal = QMExecute(comm, VarPtr(ErrNoPtr))

Print String@(RetVal), ErrNoPtr

Free(comm)
dmontaine
Posts: 5
Joined: Thursday 3rd February 2022 3:05pm

Re: How to access this C function defined in an external library

Post by dmontaine »

Thank You. That worked.

QMExecute is part of the API for the ScarletDME and open source, NoSQL, multi-user, multi-value database released by the creators of the commercial OpenQM database in 2007. It is one of the family of Pick-like databases that use BASIC as the backend database language. It has recently been modified to be 64 bit as well as the original 32 bit. It runs only on current Debian,Redhat or Arch based Linux distros.

QMExecute runs a command like "CREATE-FILE" or any of the other terminal command level (TCL) commands that are defined for the database.

ReturnValue = QMExecute(<The Command To Be Executed>, <The Error Code if Any>)

There is a command in ScarletDME that returns a list of files (LISTF). In that case the ReturnValue string would contain the list of files. The error code only returns a value if the command fails.
It returns the name of each database file along with additional information such as FIle Type, Description, etc.

It can also be used to run queries like SORT ONLY VOC WITH F1 = 'F', which is the same as LISTF except returns only a sorted list of file names without other information.

I am writing a front-end graphical app to manage the backend ScarletDME database, kinda' like pgadmin for postgresql, but was having a bit of a problem figuring out how to use pointers in Gambas3. I have been using BASIC dialects for 35 years, but Gambas3 is new for me.

If any one is curious, you can download an easy installer (from source code) available at https://www.sdme64.com.
vuott
Posts: 263
Joined: Wednesday 5th April 2017 6:07pm
Location: European Union

Re: How to access this C function defined in an external library

Post by vuott »

dmontaine wrote: Monday 11th December 2023 8:31am ReturnValue = QMExecute(<The Command To Be Executed>, <The Error Code if Any>)

There is a command in ScarletDME that returns a list of files (LISTF). In that case the ReturnValue string would contain the list of files. The error code only returns a value if the command fails.
It returns the name of each database file along with additional information such as FIle Type, Description, etc.
Explaination:

The external function "QMExecute()" requires that a character pointer variable be passed in its first argument.
In this case, when data is to be passed to an external function using a character Pointer, in Gambas it is necessary to write that data into a reserved memory area, pointed to by a variable of type Pointer. We will then pass this Pointer.
In Gambas, we obtain a reserved memory area by means of the Alloc() function.
We could have passed a String directly to the first argument, if it had provided a type "const char * " as a formal parameter.

With regard to the second parameter, as is the case in other similar cases, the function returns the numeric value of the error for "address". We can therefore collect that value, passed by the external function via a Pointer to 'Integer'. So we will pass the external function the memory address (Pointer) of a variable of type "Integer", but we will then read the value directly from the variable of type "Integer".

Finally, the function returns a character Pointer 'by value'.
To read that character string, we will use the Gambas function "String@()" to dereference the character Pointer.

In any case, I suggest these pages on the subject of external functions and EXTERN:
* http://gambaswiki.org/wiki/lang/extdecl
* http://gambaswiki.org/wiki/doc/extern
* http://gambaswiki.org/wiki/lang/externfunc
* https://www.gambas-it.org/wiki/index.ph ... nte_EXTERN
Europaeus sum !

Amare memorentes atque deflentes ad mortem silenter labimur.
dmontaine
Posts: 5
Joined: Thursday 3rd February 2022 3:05pm

Re: How to access this C function defined in an external library

Post by dmontaine »

Actually, I was able to find another example and this works.

Private Extern QMExecute(Cmnd As String, ByRef ErrNo As Pointer) As String
...
Dim Str_FullCommand as String = "CREATE-FILE TEST1 NO.QUERY"
Dim ErrNo As Integer = 0
Dim RetStr As String = ""
RetStr = QMExecute(Str_FullCommand, VarPtr(ErrNo))
RetStr = Replace$(RetStr, Chr$(254), Chr$(10))
Message(RetStr)

Pick-like databases use what they call field marks chr$(254) to separate lines. I had to convert them to line feeds.
Post Reply