Page 1 of 1

[DLL/SO/DYLIB] Unicode to Ascii via Structure

Posted: Sat Nov 19, 2016 10:38 am
by es_91
What is the best way to get a string inside a structure out of a Unicode DLL? If the caller is ASCII caller will always read the string in ASCII which leads to erroneous strings.

For example i try to return "Hello World" from a DLL, the returned string is read and displayed in the ASCII caller as only "H".

Code: Select all

; DLL UNICODE

structure myInfo
  str$
endStructure

procedureDLL getInfo ()
  
  *inf. myInfo = allocateStructure (myInfo)
  *inf \ str$ = "Hello World"
  
  procedureReturn *inf
  
endProcedure

; ExecutableFormat = Shared dll
; EnableUnicode

Code: Select all

; CALLER ASCII

structure myInfo
  str$
endStructure

lb = openLibrary (#pb_Any, "unicode.dll")

*inf.myInfo = callFunction (lb, "getInfo")

debug *inf \ str$
Compile with PB 5.31 please.

Re: [DLL] Unicode to Ascii via Structure

Posted: Sat Nov 19, 2016 11:21 am
by mk-soft
Part of VariantHelper....

Code: Select all

;-T_BSTR
Procedure helpSysAllocString(*Value)
  ProcedureReturn SysAllocString_(*Value)
EndProcedure
Prototype.l ProtoSysAllocString(Value.p-unicode)

Global T_BSTR.ProtoSysAllocString = @helpSysAllocString()
For free memory use SysFreeString into main program or into dll.

Re: [DLL] Unicode to Ascii via Structure

Posted: Sat Nov 19, 2016 5:56 pm
by es_91
How would it be done under Linux or MacOS? I suppose there is no PureBasic function like ConvertString () to switch between Ascii and Unicode?

Thanks for now. :)

Re: [DLL/SO/DYLIB] Unicode to Ascii via Structure

Posted: Sun Nov 20, 2016 9:50 am
by Fred
You can use PeekS/PokeS() to convert between string format

Re: [DLL/SO/DYLIB] Unicode to Ascii via Structure

Posted: Sun Nov 20, 2016 10:43 am
by User_Russian
Fred wrote:You can use PeekS/PokeS() to convert between string format
This greatly complicates the development of the program. I about it wrote. http://www.purebasic.fr/english/viewtop ... 73#p489173

Re: [DLL/SO/DYLIB] Unicode to Ascii via Structure

Posted: Sun Nov 27, 2016 9:25 pm
by es_91
Fred wrote:You can use PeekS/PokeS() to convert between string format
Thank you, I see. But this requires the 'length' parameter of PeekS () to be set. I did not intend to return a string length as part of the structure. Is it save to give that some 50,000 just to make sure all characters are read until '\0'?

Re: [DLL/SO/DYLIB] Unicode to Ascii via Structure

Posted: Mon Nov 28, 2016 3:09 pm
by mk-soft
The paramter for undefined lenght is "-1"

Code: Select all

r1.s = PeekS(*mem, -1, #PB_Unicode)

Re: [DLL/SO/DYLIB] Unicode to Ascii via Structure

Posted: Tue Nov 29, 2016 10:15 am
by es_91
Much thanks. :)