Using a C library
-
- User
- Posts: 74
- Joined: Fri Dec 02, 2005 3:11 pm
- Location: England
- Contact:
Using a C library
Hi Guys,
I've had PB for ages now, but keep refraining from using it. However, I've decided to pick it up and start developing. The issue I have, though, is that I'd like to use some functionality from an existing C library. I have a C .lib file, and I've downloaded the LibImporter, but the library I wish to use has structs and macros that I'd like converted, too. Can I do this with LibImporter, and if not, how do I go about including these in my PB app? I can understand if I have to use the base functions of the macro's rather than the macros themselves if need be.
Thanks,
Laz
I've had PB for ages now, but keep refraining from using it. However, I've decided to pick it up and start developing. The issue I have, though, is that I'd like to use some functionality from an existing C library. I have a C .lib file, and I've downloaded the LibImporter, but the library I wish to use has structs and macros that I'd like converted, too. Can I do this with LibImporter, and if not, how do I go about including these in my PB app? I can understand if I have to use the base functions of the macro's rather than the macros themselves if need be.
Thanks,
Laz
Laz
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Beside porting the needed header file of the .lib to PB, you need to be aware which internal calling conventions of the including symbols are used in the binary/lib file.
Do have a look in the C++ Header .... if no "Extern "C" {....}" is used then you have to apply it if recompiling is possible - otherwise it wont work.
Also do look if in the Function declarations the convention "__stdcall" or its Macro has been used.
Else you have to use "ImportC" in your Purebasic header port.
Do have a look in the C++ Header .... if no "Extern "C" {....}" is used then you have to apply it if recompiling is possible - otherwise it wont work.
Also do look if in the Function declarations the convention "__stdcall" or its Macro has been used.
Else you have to use "ImportC" in your Purebasic header port.
Check out OOP support for PB here!
-
- User
- Posts: 74
- Joined: Fri Dec 02, 2005 3:11 pm
- Location: England
- Contact:
I've looked at using ImportC, but can't see where I specify the location to the lib for importing... ie, where do I enter the directory of my lib file? Are there any examples of using ImportC? Can I import structures and macro's using ImportC?
Thanks,
Laz
Thanks,
Laz
Laz
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
-
- User
- Posts: 74
- Joined: Fri Dec 02, 2005 3:11 pm
- Location: England
- Contact:
Okay, I'm getting further with this, but have hit a stump... Say I have two functions from my lib:
Import
getString( str$ )
returnString( str )
EndImport
Now, getString accepts a string, and returns a C struct. The return string accepts the struct, and returns a string. See? Now, the problem is, how do I store the struct? And how do I represent this in PureBasic? I've tried
val = getString( "somestring" )
result = returnString( val )
Print( result )
But I receive an error saying "String expected" on the Print line...
Any ideas?
Thanks,
Import
getString( str$ )
returnString( str )
EndImport
Now, getString accepts a string, and returns a C struct. The return string accepts the struct, and returns a string. See? Now, the problem is, how do I store the struct? And how do I represent this in PureBasic? I've tried
val = getString( "somestring" )
result = returnString( val )
Print( result )
But I receive an error saying "String expected" on the Print line...
Any ideas?
Thanks,
Laz
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Maybe it works like this:
Also make sure that since you are using Print() you need to open a console first: OpenConsole()
Code: Select all
MyStruct = getString("Some text...")
Result$ = returnString(MyStruct)
Debug Result$
-
- User
- Posts: 74
- Joined: Fri Dec 02, 2005 3:11 pm
- Location: England
- Contact:
That doesn't sound like it would work...
I only need to store the struct. Any operations on the struct would happen in the library. I could return a pointer to the struct if it would help? How would one store a pointer to a value from a C lib?
Thanks,
I only need to store the struct. Any operations on the struct would happen in the library. I could return a pointer to the struct if it would help? How would one store a pointer to a value from a C lib?
Thanks,
Laz
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
The above just would have shown the string, not store the struct..Lazarus404 wrote:That doesn't sound like it would work...
I only need to store the struct. Any operations on the struct would happen in the library. I could return a pointer to the struct if it would help? How would one store a pointer to a value from a C lib?
Thanks,
Could you please show me the C struct? Because you need to make it a PureBasic Structure first before attempting to store the C struct.
-
- User
- Posts: 74
- Joined: Fri Dec 02, 2005 3:11 pm
- Location: England
- Contact:
The struct looks like
where val_type is also a struct. It's all simple data.
Problem is, returnString doesn't return a string, so something is going on somewhere that's not right. My guess (because it's so simple) is that PB isn't storing the struct, so isn't passing it, either.
Thanks,[/code]
Code: Select all
typedef struct {
val_type t;
char* f;
} vstring;
Problem is, returnString doesn't return a string, so something is going on somewhere that's not right. My guess (because it's so simple) is that PB isn't storing the struct, so isn't passing it, either.
Thanks,[/code]
Laz
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
-
- User
- Posts: 74
- Joined: Fri Dec 02, 2005 3:11 pm
- Location: England
- Contact:
When I try to force result as a string type, I get the error "cannot write a numerical value into a string variable". Can I specify in the Import block that the returnString function returns a string? Otherwise, the PB compiler gets its knickers in a knot.
Thanks,
Thanks,
Laz
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
And how does the val_type struct look like? You need to know that too.
Code: Select all
; Still unfinished, missing val_type struct
Structure val_type
EndStructure
Structure vstring
t.val_type
f.s
EndStructure
-
- User
- Posts: 74
- Joined: Fri Dec 02, 2005 3:11 pm
- Location: England
- Contact:
Actually, val_type is a typedef
Code: Select all
typedef enum {
VAL_INT = 0xFF,
VAL_NULL = 0,
VAL_FLOAT = 1,
...
} val_type;
Laz
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Ok... try if this works:
Code: Select all
Structure vstring
t.l
f.s
EndStructure
MyString.vstring = getString("Some text")
Result$ = returnString(MyString)
Debug Result$
-
- User
- Posts: 74
- Joined: Fri Dec 02, 2005 3:11 pm
- Location: England
- Contact:
This returns
"Can't assign a value to a structure"

What about storing C pointers to PB variables?
Thanks,
"Can't assign a value to a structure"

What about storing C pointers to PB variables?
Thanks,
Laz
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component