April 17, 2024, 07:21:44 PM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Unicode to string and back

Started by Andy, August 21, 2018, 11:39:36 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Well,

I've tried for hours now and getting nowhere on this.

This is the unicode conversion program by Fasecero (slightly modified and much appreciated):


'------------------------------------------
' BUFFER SIZE
'------------------------------------------

CONST BUFF_SIZE = 10000 ' if something goes wrong because you r using a looong text, increase this value

'------------------------------------------
' WINAPI
'------------------------------------------

$INCLUDE "windowssdk.inc"

DECLARE RtlInitUnicodeString(pointer DestinationString, wstring SourceString)
DECLARE RtlFreeUnicodeString(pointer DestinationString)

type UNICODE_STRING
USHORT Length
USHORT MaximumLength
PWSTR  Buffer
endtype

'------------------------------------------
' VARIABLES
'------------------------------------------

ISTRING buffer[2*BUFF_SIZE]
iwstring buff[BUFF_SIZE]
UNICODE_STRING unic_str
istring bytes[2*BUFF_SIZE]
INT guiFont = 0

'------------------------------------------
' INTERFACE
'------------------------------------------

CONST STATIC_1 = 1
CONST EDIT_2 = 2
CONST STATIC_3 = 3
CONST EDIT_5 = 5
CONST STATIC_6 = 6
CONST EDIT_7 = 7
CONST STATIC_8 = 8
CONST EDIT_9 = 9
DIALOG d1
CREATEDIALOG d1,0,0,716,485,0x80C80080,0,"String bytes",&d1_handler
CONTROL d1,@STATIC,"Type your text here",21,15,120,18,0x5000010B,STATIC_1
'CONTROL d1,@EDIT,"",143,12,549,59,0x50800004,EDIT_2
CONTROL d1,@STATIC,"String",28,84,70,20,0x5000010B,STATIC_3
CONTROL d1,@EDIT,"",142,82,549,116,0x50A00804,EDIT_5
CONTROL d1,@STATIC,"Wstring",25,212,70,20,0x5000010B,STATIC_6
CONTROL d1,@EDIT,"",139,210,552,118,0x50A00804,EDIT_7
CONTROL d1,@STATIC,"RETURN STRING",23,341,114,20,0x5000010B,STATIC_8
'CONTROL d1,@EDIT,"",140,340,551,20,0x50800800,EDIT_9

'------------------------------------------
' ENTRY POINT
'------------------------------------------

DOMODAL d1

'------------------------------------------
' PROCEDURE
'------------------------------------------

SUB d1_handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW d1
/* Initialize any controls here */
InitUnicodeString(unic_str, L"")
guiFont = CreateGuiFont()
UnicodeEditCreate(d1, EDIT_2, 143 , 12, 549, 59, guiFont)
UnicodeEditCreate(d1, EDIT_9, 140,340,551,59, guiFont)
CASE @IDCLOSEWINDOW
FreeUnicodeString(unic_str)
DeleteGuiFont(guiFont)
CLOSEDIALOG d1,@IDOK
CASE @IDCONTROL
SELECT @CONTROLID
CASE EDIT_2
/* respond to edit notifications here */
SELECT @NOTIFYCODE
CASE @ENCHANGE
UnicodeEditGetText(d1, EDIT_2, buff, BUFF_SIZE)
DoWork(buff)
ENDSELECT
ENDSELECT
ENDSELECT
RETURN
ENDSUB

'------------------------------------------
' INPUT TEXT CHANGE
'------------------------------------------

SUB DoWork(wstring base)
buffer = W2S(base)
SETCONTROLTEXT(d1, EDIT_5, PrintString(buffer))

SETCONTROLTEXT(d1, EDIT_7, PrintWString(base))

SETCONTROLTEXT(d1, EDIT_9, PrintBack(base))

'FreeUnicodeString(unic_str)
'InitUnicodeString(unic_str, buff)
'SETCONTROLTEXT(d1, EDIT_9, PrintUnicodeString(unic_str))
ENDSUB

'------------------------------------------
' UNICODE EDIT CONTROL
'------------------------------------------

SUB UnicodeEditCreate(window w, INT ctrlID, INT x, INT y, INT width, INT height, INT font)
INT dwStyles = WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_LEFT | ES_MULTILINE
INT hwndEDIT = CreateWindowExW(WS_EX_CLIENTEDGE, L"EDIT",NULL, dwStyles, x, y, width, height, w.hwnd, ctrlID, GetModuleHandle(0), NULL)
SendMessageW(hwndEDIT, WM_SETFONT, font, MAKELPARAM(TRUE,0) )
ENDSUB

SUB UnicodeEditGetText(window w, INT ctrlID, pointer buffer, INT maxCount)
INT hwndEDIT = GetDlgItem(w.hwnd, ctrlID)
GetWindowTextW(hwndEDIT, buffer, maxCount)
ENDSUB

'------------------------------------------
' GUI FONT
'------------------------------------------

SUB CreateGuiFont(), INT
NONCLIENTMETRICSW metrics
metrics.cbSize = LEN(metrics)
SystemParametersInfow(SPI_GETNONCLIENTMETRICS, LEN(metrics), &metrics, 0)

RETURN CreateFontIndirectW(&metrics.lfSmCaptionFont)
ENDSUB

SUB DeleteGuiFont(INT font)
IF font THEN DeleteObject(font)
ENDSUB

'------------------------------------------
' AUX
'------------------------------------------

SUB InitUnicodeString(pointer DestinationString, wstring SourceString)
UINT hInst=LoadLibrary("ntdll.dll")

IF hInst THEN
UINT proc = GetProcAddress(hInst,"RtlInitUnicodeString")

IF proc THEN
!<RtlInitUnicodeString>proc(DestinationString, SourceString)
ENDIF

FreeLibrary(hInst)
ENDIF
ENDSUB

SUB FreeUnicodeString(pointer DestinationString)
UINT hInst=LoadLibrary("ntdll.dll")

IF hInst THEN
UINT proc = GetProcAddress(hInst,"RtlFreeUnicodeString")

IF proc THEN
!<RtlFreeUnicodeString>proc(DestinationString)
ENDIF

FreeLibrary(hInst)
ENDIF
ENDSUB

SUB PrintString(string ansi), string
bytes = ""
pointer p = &ansi + 0
char a = 0

DO
a = *<char>p
int t = ASC(chr$(a))

IF t = 32 THEN
bytes += "      "
ELSE
IF t THEN
bytes += STR$(t) + " "
ELSE
bytes += STR$(t)
ENDIF
ENDIF

p+=1
UNTIL a = 0

RETURN bytes
ENDSUB

SUB PrintWString(wstring unic), string
bytes = ""
pointer p = &unic + 0
char a = 0
char b = 0

DO
a = *<char>p
int t = ASC(chr$(a))

b = *<char>(p+1)
int t2 = ASC(chr$(b))

IF t = 32 AND t2 = 0 THEN
bytes += "         "
ELSE
IF t > 0 or t2 > 0 THEN
bytes += STR$(t) + "," + STR$(t2) + ","
ELSE
bytes += STR$(t) + "," + STR$(t2)
ENDIF
ENDIF

p+=2
UNTIL a = 0 AND b = 0

RETURN bytes
ENDSUB

SUB PrintUnicodeString(UNICODE_STRING unic_str), string
bytes = ""
pointer p = &unic_str + 0
char a = 0

INT lenght = LEN(unic_str)

INT j

FOR j = 1 TO lenght
a = *<char>p
int t = ASC(chr$(a))

IF j < lenght THEN
bytes += STR$(t) + " -"
ELSE
bytes += STR$(t)
ENDIF

p+=1
NEXT j

RETURN bytes
ENDSUB

SUB PrintBack(wstring unic), string
bytes = ""
pointer p = &unic + 0
char a = 0
char b = 0

DO
a = *<char>p
int t = ASC(chr$(a))

b = *<char>(p+1)
int t2 = ASC(chr$(b))

      bytes += chr$(a)

p+=2
UNTIL a = 0 AND b = 0

RETURN bytes
ENDSUB


What I'm trying to do is to take the WSTRING and convert it back to unicode characters and place them in EDIT_9.

Help please!

Andy.

Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

Andy

It's okay, I've found it!

I was using SETCONTROLTEXT instead of SetWindowTextW:

SUB DoWork(wstring base)
   buffer = W2S(base)
   SetWindowTextW(GetDlgItem(d1.hwnd, EDIT_9),base)
ENDSUB

Andy
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.