Page 1 of 1

PB Strings from OBJ/LIB

Posted: Sat Dec 31, 2016 5:21 pm
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?

Re: PB Strings from OBJ/LIB

Posted: Sat Dec 31, 2016 8:32 pm
by idle
your'e using the prototype without a parameter, have you tried making it optional

Prototype.s ProtoGetString(prev.l=0)

Re: PB Strings from OBJ/LIB

Posted: Sun Jan 01, 2017 10:07 am
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!