Page 1 of 1

Some help with a dylib, please

Posted: Tue Jun 12, 2018 12:38 pm
by simberr
I have been successful in creating a dylib which takes input parameters and exports numeric data (all types well).

However, I am having great difficulties in exporting (Returning from procedure) a string.

A code snippet is as follows:

Code: Select all

Global ReturnString.s

ProcedureDLL.s GetString() ;Create unicode executable gives only first character
  
	ReturnString = "More then H"
	ProcedureReturn ReturnString

EndProcedure
This compiles correctly into a dylib.

The declaration in my source code (Xojo) is:

Code: Select all

Declare Function GetString Lib libname As CString
The returned value that I get from the procedure is just the first character - "M".

Can anybody help me?

Re: Some help with a dylib, please

Posted: Tue Jun 12, 2018 1:30 pm
by mk-soft
Purebasic v5.6x is Unicode

Code: Select all

Threaded ReturnString.s

ProcedureDLL.i GetString()
 
   ReturnString = "More then H"
   ProcedureReturn @ReturnString

EndProcedure
I donĀ“t know this right "WString as Result
Declare Function GetString Lib libname As WString

Re: Some help with a dylib, please

Posted: Wed Jun 13, 2018 12:52 am
by simberr
Thank you, I will try that.

Re: Some help with a dylib, please

Posted: Wed Jun 13, 2018 9:22 am
by simberr
The code that you posted works inasmuch as I now get a string returned from my dylib.

Unfortunately, this string is a 4 byte string rather than a two byte string so I get every other byte as a zero. This, of course, is not readable when displayed as the string

"More then H"

is returned as

"M*o*r*e* *t*h*e*n* *H*" (asterisks are Chr(0)).

This is because MacOS sees a String as 4 bytes. I tried using CString in my declare but this did not work, only String works (as you suggested).

Any further advice?

Re: Some help with a dylib, please

Posted: Wed Jun 13, 2018 9:31 am
by mk-soft
I think go back to CString...

Edit

Code: Select all

;-TOP
CompilerIf #PB_Compiler_Version < 550
  Procedure Ascii(string.s)
    Protected *result
    *result = AllocateMemory(Len(String) + 1)
    If *result
      PokeS(*result, String, -1, #PB_Ascii)
    EndIf
    ProcedureReturn *result
  EndProcedure
  
  Procedure UTF8(string.s)
    Protected *result
    *result = AllocateMemory(StringByteLength(String, #PB_UTF8) + 1)
    If *result
      PokeS(*result, String, -1, #PB_UTF8)
    EndIf
    ProcedureReturn *result
  EndProcedure
CompilerEndIf

Threaded *ReturnString

ProcedureDLL.i GetString()
  If *ReturnString
    FreeMemory(*ReturnString)
  EndIf
  *ReturnString = Ascii("Hello World")
  ProcedureReturn *ReturnString
EndProcedure

Re: Some help with a dylib, please

Posted: Wed Jun 13, 2018 3:17 pm
by simberr
Absolutely perfect.

Thank you so much, I have been agonising over this for four whole days.

You are a star.