Page 1 of 1

PureBASIC .lib files

Posted: Tue Aug 28, 2007 12:05 pm
by freddix
Hi,

When I create a new DLL using PureBASIC, it also create 2 other files.
I'd like to know if the generated .LIB file is compatible with VS 2005 ?

Thank you
Freddix

Posted: Tue Aug 28, 2007 12:09 pm
by milan1612
PureBasic DLL Sample wrote:;
; ------------------------------------------------------------
;
; PureBasic - DLL example file
;
; (c) 2002 - Fantaisie Software
;
; ------------------------------------------------------------
;
; This example is a skeleton to build easely a DLL using PureBasic
; The dll is created in the 'Compilers' directory, under the
; 'purebasic.dll' name. An associated '.lib' is generated to use
; with LccWin32 or VisualC++
.
;
;
; Rules to follow:
; - Never write code outside a procedure, except for variables
; or structure declaration.
;
; - DirectX Init routines must not be initialized in the the
; AttachProcess() procedure
;
; - There is 4 procedures automatically called: AttachProcess(),
; DetachProcess(), AttachThread() and DetachThread(). If you don't
; need them, just remove them.
So, yes. They are compatible... :)

Posted: Tue Aug 28, 2007 12:32 pm
by freak
Note that the generated file is only an import library. If you use it you will still need the dll.

Posted: Tue Aug 28, 2007 12:34 pm
by freddix
thank you.

So, now,
Does someone have a sample showing how to call a .LIB generated by PureBASIC with VC++ ?

Thanks again :p

Posted: Tue Aug 28, 2007 5:42 pm
by jack
compile this to "PBdll.dll"

Code: Select all

ProcedureDLL.l IsPowerOf2(x.l) 
  t.l=0 
  If (x&(x-1))=0 
     t=1 
  EndIf                      
  ProcedureReturn t 
EndProcedure 

ProcedureDLL.l IsPowerOf(power.l, base.l) 
  Protected basePow.l 
  Protected count.l 

  If base < 0 Or power < 0 
    ProcedureReturn -1 
  EndIf 
  If base = 0 
    If power = 1 
      ProcedureReturn 0 
    Else 
      ProcedureReturn 1 
    EndIf 
  EndIf 
  If base = 1 
    If power = 1 
      ProcedureReturn 1 
    Else 
      ProcedureReturn -1 
    EndIf 
  EndIf 
  If power = 1 
    ProcedureReturn 0 
  EndIf 

  basePow = base 
  While $7FFFFFFF / basePow > 1 
    count+1 
    If basePow = power 
      ProcedureReturn count 
    EndIf 
    basePow * base 
  Wend 
  ProcedureReturn -1 
EndProcedure 


ProcedureDLL.s Round2n(n.f,places.l) 
    If places < 0 : places = 0 : EndIf 
    r.f = 0.5 * Pow(10,-1*places) : T.f = Pow(10,places) 
    ProcedureReturn StrF(Int((n+r)*T)/T,places) 
EndProcedure
make a new vc win32 console project, call it "PB dll test"
use notepad to save this as "PB dll test.c" and place it into the project folder, add the file to your project, (project/Add Existing Item)
put "PBdll.lib" into project folder and the dll in the app's path
follow the tip in the C file to add the lib to your project.

Code: Select all

/*<note> 
in project properties,
expand linker, click on commandline,
add: full path \PBdll.lib
example: "C:\Documents and Settings\John Doe\My Documents\Visual Studio 2005\Projects\PB dll test\pbdll.lib"
*/

#include <stdio.h> 

extern __declspec(dllimport) int __stdcall IsPowerOf2(int n);
extern __declspec(dllimport) int __stdcall IsPowerOf(int power, int base);
extern __declspec(dllimport) char* __stdcall Round2n(float n,int places);

void main() 
{ 
	char ch;
	int n,m;
	n=IsPowerOf2(33);
	m=IsPowerOf(49,7);
	printf("%d\n",n);
	printf("%d\n",m);
	printf("%s\n",Round2n((float) 3.1415927,4));
	printf("%s\n","press return to end");
	ch=getchar();
}

Posted: Tue Aug 28, 2007 6:32 pm
by freddix
Thank you for this great sample :)

Do you think the declarations :
extern __declspec(dllimport) int __stdcall IsPowerOf2(int n);
extern __declspec(dllimport) int __stdcall IsPowerOf(int power, int base);
extern __declspec(dllimport) char* __stdcall Round2n(float n,int places);
Can be moved in an include file like :
#include <MyInclude.h>
?

Another question now,
Is it possible to use existing .lib files from PureBASIC ?


Thank you.

Posted: Tue Aug 28, 2007 6:37 pm
by jack
about the include, yes of course, don't know about the PB libs :)

Posted: Tue Aug 28, 2007 6:52 pm
by Fred
You can use the existings libs with the Import/EndImport keywords.

Posted: Tue Aug 28, 2007 6:53 pm
by freddix
Fred, do you have a sample (even theoric) about this ?

Posted: Tue Aug 28, 2007 6:55 pm
by Fred
The doc ? ;)

Posted: Wed Aug 29, 2007 2:37 pm
by freddix
If a function is a .lib return a value, etc .. Should I declare it in the import or will the importer detect it ?
For example in the .h file associated to the .lib I have this definition :
int dbChecklistQuantity ( void );
Should I do this ? :
Import
Retour.l = dbChecklistQuantity()
EndImport
Or Simply this and compiler will detect the returned value :
Import
dbChecklistQuantity()
EndImport

Posted: Wed Aug 29, 2007 2:49 pm
by ts-soft
>> Or Simply this
yes :wink:
Returnvalue is always a long!

Posted: Wed Aug 29, 2007 4:25 pm
by freddix
Ok.

Thank you.