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

Everything else that doesn't fall into one of the other PB categories.
es_91
Enthusiast
Enthusiast
Posts: 298
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

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

Post 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.
Last edited by es_91 on Sat Nov 19, 2016 5:57 pm, edited 1 time in total.
:mrgreen:
User avatar
mk-soft
Always Here
Always Here
Posts: 6416
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: [DLL] Unicode to Ascii via Structure

Post 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.
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
es_91
Enthusiast
Enthusiast
Posts: 298
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

Re: [DLL] Unicode to Ascii via Structure

Post 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. :)
:mrgreen:
Fred
Administrator
Administrator
Posts: 18394
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

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

Post by Fred »

You can use PeekS/PokeS() to convert between string format
User_Russian
Addict
Addict
Posts: 1603
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

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

Post 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
es_91
Enthusiast
Enthusiast
Posts: 298
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

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

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

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

Post by mk-soft »

The paramter for undefined lenght is "-1"

Code: Select all

r1.s = PeekS(*mem, -1, #PB_Unicode)
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
es_91
Enthusiast
Enthusiast
Posts: 298
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

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

Post by es_91 »

Much thanks. :)
:mrgreen:
Post Reply