xsharp.eu • What happens with W2String() ?
Page 1 of 2

What happens with W2String() ?

Posted: Wed Sep 26, 2018 12:46 pm
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

What happens with W2String() ?

Posted: Wed Sep 26, 2018 5:24 pm
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

What happens with W2String() ?

Posted: Wed Sep 26, 2018 9:40 pm
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

What happens with W2String() ?

Posted: Thu Sep 27, 2018 12:49 pm
by robert
Karl-Heinz,

Can you explain why you need this.

Robert

What happens with W2String() ?

Posted: Thu Sep 27, 2018 4:37 pm
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

What happens with W2String() ?

Posted: Thu Sep 27, 2018 4:48 pm
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.

What happens with W2String() ?

Posted: Thu Sep 27, 2018 4:53 pm
by FFF
Nothing attached...

What happens with W2String() ?

Posted: Thu Sep 27, 2018 5:25 pm
by Karl-Heinz
i see two empty rectangles, that´s all ?


http://www.rauscher-software.de/downloads/Filecopy.ZIP

What happens with W2String() ?

Posted: Thu Sep 27, 2018 9:52 pm
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

What happens with W2String() ?

Posted: Thu Sep 27, 2018 10:09 pm
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