Some help with a dylib, please

Just starting out? Need help? Post your questions and find answers here.
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Some help with a dylib, please

Post 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?
User avatar
mk-soft
Always Here
Always Here
Posts: 5395
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Some help with a dylib, please

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Re: Some help with a dylib, please

Post by simberr »

Thank you, I will try that.
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Re: Some help with a dylib, please

Post 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?
User avatar
mk-soft
Always Here
Always Here
Posts: 5395
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Some help with a dylib, please

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
simberr
User
User
Posts: 46
Joined: Sun Jun 10, 2018 12:54 pm

Re: Some help with a dylib, please

Post by simberr »

Absolutely perfect.

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

You are a star.
Post Reply