Userlib Howto

Just starting out? Need help? Post your questions and find answers here.
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Userlib Howto

Post by Karbon »

Could someone point me in the right direction to find some information about making a userlib in PB?

I have a few DLLs that I'd like to wrap up and have access without having to use Callfuntion() and friends if possible...

Thanks!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
User avatar
tinman
PureBasic Expert
PureBasic Expert
Posts: 1102
Joined: Sat Apr 26, 2003 4:56 pm
Location: Level 5 of Robot Hell
Contact:

Re: Userlib Howto

Post by tinman »

Karbon wrote:Could someone point me in the right direction to find some information about making a userlib in PB?

I have a few DLLs that I'd like to wrap up and have access without having to use Callfuntion() and friends if possible...
The library SDK is the only current source of information I think. Do you really need to write a user library or do you just need to get access to the DLL functions?

If you want to code the library (insead of importing the DLL) and create your own commands then you only really need to write a .desc file (which tells PureBasic what commands and things there are), keep to the naming convention, and play nicely with the global variables which you have access to. As long as your compiler / assembler / whatever can create a standard format of library (.lib) file (sometimes called static library or import library) then it should be possibleto write a PureLibrary with it.

Functions in your code which are to be used as commands in PureBasic should have their names start with "PB_".

Commands which have multiple forms (for example, for ones which have optional parameters) should have the second and additional functions end with the number of which version of the command it is, i.e.:

PB_MyCommand <- First set of parameters
PB_MyCommand2 <- second
PB_MyCommand3 <- third

The number which you give them is the order in which the parameter descriptions appear in the .desc file.

There are some global variables which you use to interact with the debugger.

Your debug code must be a function for each command function (this is called before your command function). It must have the same parameters as the command function, but does not return anything. Its name must be the same as the command function, with "_DEBUG" on the end. In the above case of multiple optional parameters, the debug functions would be:

PB_MyCommand_DEBUG
PB_MyCommand2_DEBUG
PB_MyCommand3_DEBUG

If you just want to get access to the DLL functions then you can use the DLLImport tool. All you need to do for this is to write a text file (with a .pbl extension) and in there you need to put the library name on the first line and then the names of the functions, a space, then the number of parameters for that function. Each function is on it's own line. Example:

Code: Select all

library_with_cool_stuff.dll
first_function 0
count_chicken 4
That means that the DLL which will be used is named "library_with_cool_stuff.dll". There are two functions being imported, one called "first_function" which has no parameters. The second function is called "count_chicken" and has 4 parameters.

You then need to run the DLLImport tool where you tell it the directory where your .pbl file is located and where you want to install the library that is created - normally PureBasic\PureLibraries\UserLibraries.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Excellent info! Thank you!

I do only need access to the DLL, but it's something I'd likely use a lot so having a userlib might be nice... That and I was really curious about how to do it anyway! :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
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 »

Karbon wrote:I do only need access to the DLL, but it's something I'd likely use a lot so having a userlib might be nice... That and I was really curious about how to do it anyway! :-)
If you import the DLL then it behaves like the API commands, so it really is like having a userlib. Unless you really want the pain of coding and testing a userlib *and* your application ;)
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Well knowing how to do a userlib is helpful but I'm not going to do it in this situation..

Do have one more question if you don't mind.

I'm doing all this with libcurl, and it just got a little strange. The function prototype in question is

Code: Select all

CURLFORMcode curl_formadd(struct curl_httppost **httppost,
                 struct curl_httppost **last_post,
                 ...);
How would I describe the number of arguments there? There needs to be at least 3, but up to X I suppose.

Thanks again for your help, my apologies if these are clubie questions!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
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 »

Karbon wrote:I'm doing all this with libcurl, and it just got a little strange. The function prototype in question is

Code: Select all

CURLFORMcode curl_formadd(struct curl_httppost **httppost,
                 struct curl_httppost **last_post,
                 ...);
How would I describe the number of arguments there? There needs to be at least 3, but up to X I suppose.
Don't know, but does libcurl come as a DLL? I could not find a DLL version of it, although it does look like there is an import library for it in the developer package.

I don't think I've ever seen a DLL with variable arguments, although I suppose there is no reason it couldn't be done.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

Yea, that's pretty much what I was going to use.. Bad idea? The mingw devel version comes with a ready made DLL that I was able to open with PB, haven't tried to actually *use* it yet though..

What's the significance of an import library - another way of saying static lib? (guessing)..

I'm flying blind with this windows programming stuff, very new too it.. Thanks for having the patience to deal with another cluebie. Must... Have... FreeBSD. :-)
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
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 »

Karbon wrote:Yea, that's pretty much what I was going to use.. Bad idea? The mingw devel version comes with a ready made DLL that I was able to open with PB, haven't tried to actually *use* it yet though..
Had a look at it and ran it through a tool I have here for importing DLLs for use with DevC++. The curl_formadd function does not have a @x after it so it looks like it might be a CDECL function (C style) rather than STDCALL (Windows standard style). I think PureBasic should be able to handle CDECL functions, because the Library library has functions for it. But I have no idea how to get it imported.
What's the significance of an import library - another way of saying static lib? (guessing)..
Import libraries are usually static libraries which are used to be able to call functions from dynamic libraries. Typically contains information and/or code which calls the function in the DLL - your code in your application calls the code in the import library.
If you paint your butt blue and glue the hole shut you just themed your ass but lost the functionality.
(WinXPhSP3 PB5.20b14)
Karbon
PureBasic Expert
PureBasic Expert
Posts: 2010
Joined: Mon Jun 02, 2003 1:42 am
Location: Ashland, KY
Contact:

Post by Karbon »

I think I'll try getting it to work using the Library library and see if it's even usable... I'll ask how to import CDECL functions in the Windows forum..

Thanks again for your help!
-Mitchell
Check out kBilling for all your billing software needs!
http://www.k-billing.com
Code Signing / Authenticode Certificates (Get rid of those Unknown Publisher warnings!)
http://codesigning.ksoftware.net
Post Reply