Using a C library

Just starting out? Need help? Post your questions and find answers here.
Lazarus404
User
User
Posts: 74
Joined: Fri Dec 02, 2005 3:11 pm
Location: England
Contact:

Using a C library

Post by Lazarus404 »

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
Laz

Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Post by tinman »

Try using the header converter to convert the structures. I don't know if it converts macros too, but it might.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
inc.
Enthusiast
Enthusiast
Posts: 406
Joined: Thu May 06, 2004 4:28 pm
Location: Cologne/GER

Post by inc. »

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.
Check out OOP support for PB here!
Lazarus404
User
User
Posts: 74
Joined: Fri Dec 02, 2005 3:11 pm
Location: England
Contact:

Post by Lazarus404 »

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
Laz

Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Lazarus404
User
User
Posts: 74
Joined: Fri Dec 02, 2005 3:11 pm
Location: England
Contact:

Post by Lazarus404 »

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,
Laz

Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

I'm not sure if there's a limitation on returning C structs from DLLs, but you might want to create a equivalent Structure in Purebasic and pass it as a pointer to receive the returning data.

Some of the pros can help you better than I can.
But I say trial and error is never bad. :wink:
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post by Irene »

Maybe it works like this:

Code: Select all

MyStruct = getString("Some text...")
Result$ = returnString(MyStruct)
Debug Result$
Also make sure that since you are using Print() you need to open a console first: OpenConsole()
Lazarus404
User
User
Posts: 74
Joined: Fri Dec 02, 2005 3:11 pm
Location: England
Contact:

Post by Lazarus404 »

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,
Laz

Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post by Irene »

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,
The above just would have shown the string, not store the struct..
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.
Lazarus404
User
User
Posts: 74
Joined: Fri Dec 02, 2005 3:11 pm
Location: England
Contact:

Post by Lazarus404 »

The struct looks like

Code: Select all

typedef struct {
	val_type t;
	char* f;
} vstring;
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]
Laz

Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Lazarus404
User
User
Posts: 74
Joined: Fri Dec 02, 2005 3:11 pm
Location: England
Contact:

Post by Lazarus404 »

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,
Laz

Registered PureBasic user since Nov 2005
Check out FlashML at www.designrealm.co.uk... The ultimate Flash Component
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post by Irene »

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
Lazarus404
User
User
Posts: 74
Joined: Fri Dec 02, 2005 3:11 pm
Location: England
Contact:

Post by Lazarus404 »

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
Irene
Enthusiast
Enthusiast
Posts: 461
Joined: Thu Oct 04, 2007 12:41 pm

Post by Irene »

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$
Lazarus404
User
User
Posts: 74
Joined: Fri Dec 02, 2005 3:11 pm
Location: England
Contact:

Post by Lazarus404 »

This returns

"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
Post Reply