What happens with W2String() ?

This forum is meant for questions and discussions about the X# language and tools
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

What happens with W2String() ?

Post by Karl-Heinz »

String2W() already exists, but for compatibility reasons i also need W2String(). Sure, i could add such a func to a lib of my own, but are there any plans to add string2W() to the #X runtime ?

regards
Karl-Heinz
User avatar
Chris
Posts: 4915
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

What happens with W2String() ?

Post by Chris »

Hi karl-Heinz,

I think this function is not needed, because strings are anyway unicode in .Net. So if you are using it with STRINGs, then it is not needed at all, if you use it with PSZs you can just use Psz2String() instead, or define it as

FUNCTION W2String(pStr AS PSZ) AS STRING
RETURN Psz2String( pStr )

Btw, in the vulcan runtime there were implicit conversions from/to STRING<->PSZ, so you could simply just use

cString := pPsz
or
pPsz := cString

and the runtime would take care of the conversions automatically. We haven't done that in x#, not sure we should though, seeing it again I think it was probably too much happening under the hood in the vulcan runtime there.

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

What happens with W2String() ?

Post by Karl-Heinz »

Hi Chris,

Chris wrote:
I think this function is not needed, because strings are anyway unicode in .Net. So if you are using it with STRINGs, then it is not needed at all, if you use it with PSZs you can just use Psz2String() instead, or define it as

FUNCTION W2String(pStr AS PSZ) AS STRING
RETURN Psz2String( pStr )
i think that can´t work because the X# psz2string() truncates - correctly - a widestring as VO does. What i´m currently using is the VO W2String() code. i´ll have to do some tests with structures that hold widestrings, and if there are no problems and no other solution i´ll stay with the VO code.

Code: Select all


_DLL FUNC WideCharToMultiByte(CodePage AS DWORD, dwFlags AS DWORD, lpWideCharStr AS PTR ,;   //  INTPTR
	cchWideChar AS INT, lpMultiByteStr AS PSZ,;
	cchMultiByte AS INT,lpDefaultChar AS PSZ,;
	lpUsedDefaultChar AS LOGIC PTR );   // INTPTR
	AS INT PASCAL:KERNEL32.WideCharToMultiByte	 
		
FUNCTION W2String2       (bstrVal AS PSZ)    AS STRING  
	
	RETURN psz2string ( bstrVal )	
   
FUNCTION W2String       (bstrVal AS PSZ)    AS STRING  // VO-SDK code

	LOCAL pBuff    := NULL  AS PTR  // INTPTR
	LOCAL cRet          AS STRING
	LOCAL nSize         AS INT

	//  Get Size
	nSize := WideCharToMultiByte(CP_ACP, 0,  bstrVal, -1, pBuff, 0, NULL_PSZ, NULL_PTR) 
	
	IF nSize > 0
		pBuff := MemAlloc( DWORD(nSize) + 1)   
		IF pBuff != NULL_PTR
			//  Convert to VO string
			WideCharToMultiByte(CP_ACP, 0,  bstrVal, -1, pBuff, nSize, NULL_PSZ, NULL_PTR)
			cRet := Mem2String(pBuff, DWORD(nSize) -1 )   //  -1 added KHR
		ENDIF
		MemFree(pBuff)
	ENDIF 
	
	
	RETURN cRet	
	

DEFINE CP_ACP := 0 	
	

FUNCTION Start(  ) AS VOID

// The x# runtime already knows String2W().
// /unsafe must be checked to compile.

? "*" + w2string ( string2w ( "Teststring" ) ) + "*"    // "*TestString*"
? "*" + w2string2 ( string2w ( "Teststring" ) ) + "*"   // "*T*"


RETURN

regards
Karl-Heinz
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

What happens with W2String() ?

Post by robert »

Karl-Heinz,

Can you explain why you need this.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

What happens with W2String() ?

Post by Karl-Heinz »

Hi Robert,

in the attached Filecopy.viaef there is a stripped VO func - CopyFromTo() - that detects if during a filecopy a new filename was created - something like "new.pdf" became e.g. "new - kopie (6).pdf". I have access to such a new filename in the CopyFromTo() line:

cMappedNewFile := W2String ( struMapping.pszNewPath )

Without W2String() i get a truncated filename. Only the first char is shown, instead of the full new filename.

To ensure that a correct source dir and filename is used, you are forced to take a look at the start() code, otherwise the only console output will be "done" ;-)

regards
Karl-Heinz
Attachments

[The extension viaef has been deactivated and can no longer be displayed.]

Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

What happens with W2String() ?

Post by Karl-Heinz »

mmmmh, it seems that the attachment isn´t visible ? Next try with a zip file ...
let me know if the attachment is still not visible.
Attachments
Filecopy.ZIP
(2.48 KiB) Downloaded 55 times
FFF
Posts: 1581
Joined: Fri Sep 25, 2015 4:52 pm
Location: Germany

What happens with W2String() ?

Post by FFF »

Nothing attached...
Regards
Karl
(on Win8.1/64, Xide32 2.20, X#2.20.0.3)
Karl-Heinz
Posts: 774
Joined: Wed May 17, 2017 8:50 am
Location: Germany

What happens with W2String() ?

Post by Karl-Heinz »

i see two empty rectangles, that´s all ?


http://www.rauscher-software.de/downloads/Filecopy.ZIP
User avatar
Chris
Posts: 4915
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

What happens with W2String() ?

Post by Chris »

Hi Karl-Heinz,

Ah, thanks, so W2String() does not work with PSZs (at least in the VO sense of PSZ) as its parameter type in the misleading definition in VO implies. If you try this code in VO, then you will not get correct results either:

Code: Select all

FUNCTION Start() AS INT
LOCAL p AS PSZ
p := String2Psz("ABC")
? W2String(p)
Instead of a PSZ, W2String() actually expects a pointer to a memory location containing a unicode string. So, if I am not mistaken, this simpler function should also work fine:

Code: Select all

FUNCTION W2String(p AS PTR) AS STRING
	LOCAL IMPLIED cRet := System.Text.StringBuilder{}
	LOCAL nIndex AS INT
	LOCAL pChar AS WORD PTR
	nIndex := 1
	pChar := (WORD PTR)p
	DO WHILE pChar[nIndex] != 0
		cRet:Append(Convert.ToChar(pChar[nIndex]))
		pChar ++
	END DO
RETURN cRet:ToString()
Please give it a try, if it works as expected we can add it to the runtime.

About the attachments, yeah, I see it as well, it does not work well. But for all your attachments, I could download them by using right-click and use "Save link as" from teh context menu.

Chris
Chris Pyrgas

XSharp Development Team
chris(at)xsharp.eu
User avatar
robert
Posts: 4529
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

What happens with W2String() ?

Post by robert »

Karl-Heinz,
There was a problem in the template for the forum.
It should be fixed now. You should see icons in stead of empty boxes.

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
Post Reply