PB Strings from OBJ/LIB

Just starting out? Need help? Post your questions and find answers here.
GJ-68
User
User
Posts: 32
Joined: Sun Jun 23, 2013 1:00 pm
Location: France (68)

PB Strings from OBJ/LIB

Post by GJ-68 »

Hi to all,

I'm not often here because I think that my use of PureBasic is not very interesting as you will see below.

In most of my projects I use helper functions compiled in an object file (C language).
Sometimes I need to return a PB String from a function but I do not want to write a UserLib for it, as this functions are project specific.

So, I'm using this PB code: (the C code is almost the same as for an UserLib)

Code: Select all

EnableExplicit

Prototype.s ProtoGetString()

Import "CUtils.obj"
	GetString(a.l) ; 'a.l' is here for the 'PrevPosition' parameter
EndImport

Global StrTest.ProtoGetString, s1.s

StrTest = @GetString()
!PUSH dword [_PB_StringBasePosition] ; without this I get a memoty access error (see below).
s1 = StrTest()
Debug s1
When PureBasic calls a function that returns a string from my conventional UserLibs, this is the ASM code:

Code: Select all

; s1 = StrTest()
  MOV    edx,[_PB_StringBasePosition]
  PUSH   edx
  PUSH   edx
  CALL  _PB_StrTest_UNICODE@4
  PUSH   dword v_s1
  CALL  _SYS_AllocateString4@8
When PureBasic calls a function that returns a string from a .LIB/.OBJ file, this is the ASM code:

Code: Select all

; s1 = StrTest()
  PUSH   dword [_PB_StringBasePosition]
  CALL   dword [v_StrTest]
  PUSH   dword v_s1
  CALL  _SYS_AllocateString4@8
So, I have to add the line '!PUSH dword [_PB_StringBasePosition]' and all is OK.
Is it normal or is it a PB bug?
User avatar
idle
Always Here
Always Here
Posts: 6048
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: PB Strings from OBJ/LIB

Post by idle »

your'e using the prototype without a parameter, have you tried making it optional

Prototype.s ProtoGetString(prev.l=0)
GJ-68
User
User
Posts: 32
Joined: Sun Jun 23, 2013 1:00 pm
Location: France (68)

Re: PB Strings from OBJ/LIB

Post by GJ-68 »

Thank you idle for your suggestion.

Tested with optional parameter and it works.
ASM output is now:

Code: Select all

; s1 = StrTest()
  PUSH   dword [_PB_StringBasePosition]
  PUSH   dword 0 ; <- Optionnal parameter (always 0)
  CALL   dword [v_StrTest]
  PUSH   dword v_s1
  CALL  _SYS_AllocateString4@8
That means that the 'PreviousPosition' parameter passed to the LIB function is always 0.
As the LIB function needs to call 'SYS_GetOutputBuffer(int Length, int PreviousPosition)', I'm not really sure it's safe to use.

I tested a OBJ/LIB function with a string as parameter, with composed string (like a$ + b$) as parameter, after calling other string functions,
and it always work, but as I don't understand why and how, I think I will not use it.

Thank you anyway and Happy New Year to all!
Post Reply