Author Topic: Use_AnsiToWide  (Read 314 times)

jcfuller

  • Sr. Member
  • ****
  • Posts: 268
    • View Profile
Use_AnsiToWide
« on: October 20, 2020, 07:50:21 AM »
Kevin,
  As A follow up to:
https://bcxbasiccoders.com/smf/index.php?topic=379.0

I am trying to be agnostic in using Patrice's zTrace
I tried using:
$HEADER
#ifdef UNICODE
  #define ZPRINT(x) zTrace(x)
#else
  #define ZPRINT(x) zTrace(ANSITOWIDE((char*)(x)))
#endif
$HEADER
But of course in ANSI the BCX Use_AnsiToWide flag is not set so the ANSITOWIDE function is not created.
Any hints on how to do this
I used  CONST in my source but wanted a less hands on approach.

Code: [Select]
'For Unicode uncomment
'$ONEXIT "ULEX.EXE $FILE$.CPP"
'CONST ZPRINT(x) = zTrace(x)

'For ansi uncomment
'CONST ZPRINT(x) = zTrace(ANSITOWIDE((char*)(x)))
[code]

James




MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 714
    • View Profile
Re: Use_AnsiToWide
« Reply #1 on: October 20, 2020, 08:22:08 AM »
SUB ImportA2W
DIM Dummy as BSTR
Dummy = ANSITOWIDE("")
END SUB

jcfuller

  • Sr. Member
  • ****
  • Posts: 268
    • View Profile
Re: Use_AnsiToWide
« Reply #2 on: October 20, 2020, 09:16:45 AM »
This worked:
Code: [Select]
$IFDEF UNICODE
   CONST ZPRINT(x) = zTrace(x)
$ELSE
   CONST ZPRINT(x) = zTrace(AnsiToWide(LPCSTR(x)))   
$ENDIF

James

jcfuller

  • Sr. Member
  • ****
  • Posts: 268
    • View Profile
Re: Use_AnsiToWide
« Reply #3 on: October 20, 2020, 09:39:44 AM »
Well that works but BCX catches the AnsiToWide Function and it is added to a UNICODE project also.
Not the end of the world and it causes no problems (Except for the soon to be fixed LPCTSTR -> LPCSTR), Just not needed.

James


iancasey

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Use_AnsiToWide
« Reply #4 on: December 23, 2020, 12:52:28 PM »
James
I just saw your post,  but this is how I do it, this also works with the code page and dwflags parameters.

I manually change WideToAnsi$ to WideToAnsiW  and AnsiToWide to AnsiToWideW  in my code to stop BCX, and then adding thes substitute functions below, the lexer needs to translate MultiByteToWideChar to MultiByteToWideCharW for these to work. Fortunately there are only a few functions that I need to do this for, these can be found in https://bcxbasiccoders.com/bcxusers/ian/programs/Uni_functions.zip
Ian

In the unicode lexer make sure to add these if they are missing.
MultiByteToWideCharW,MultiByteToWideCharW,MultiByteToWideChar
WideCharToMultiByte, , ,SKIP
' Erase functions
WideToAnsi, , ,ERASEF
AnsiToWide, , ,ERASEF
LoadFile, , ,ERASEF    '<- if adding the whole Uni_functions.bas file


Code: [Select]
FUNCTION MultiByteToWideCharW(CP AS UINT,Flags AS DWORD, cText$, length AS INTEGER, sRet$, szBuf AS INTEGER)
  sRet$ = cText$
  FUNCTION = LEN(sRet$)
END FUNCTION

$CCODE    funcsub
//<---UNICODE AWARE

char*   WideToAnsiW (BSTR, UINT=CP_ACP, DWORD=0);


char* WideToAnsiW (BSTR WideStr, UINT CodePage,DWORD dwFlags)
{
  char *BCX_RetStr={0};
  UINT uLen;
  uLen=WideCharToMultiByte(CodePage,dwFlags,WideStr,-1,0,0,0,0);
  BCX_RetStr=(char*)BCX_TmpStr(uLen);   //Added char*
  WideCharToMultiByte(CodePage,dwFlags,WideStr,-1,BCX_RetStr,uLen,0,0);
  return BCX_RetStr;
}


//LPOLESTR AnsiToWideW (char*,UINT=CP_ACP,DWORD=MB_PRECOMPOSED);
_TCHAR*        AnsiToWideW (char*,UINT=CP_ACP,DWORD=MB_PRECOMPOSED);

//LPOLESTR AnsiToWideW (char *AnsiStr, UINT CodePage,DWORD dwFlags)
 _TCHAR*         AnsiToWideW (char *AnsiStr, UINT CodePage,DWORD dwFlags)
{
  UINT uLen;
  BSTR WideStr;
  uLen=MultiByteToWideChar(CodePage,dwFlags,AnsiStr,-1,0,0);
  if(uLen<=1) return (BSTR) BCX_TmpStr(2);
  WideStr=(BSTR) BCX_TmpStr(2*uLen);
  MultiByteToWideChar(CodePage,dwFlags,AnsiStr,uLen,WideStr,uLen);
  return WideStr;
}
//>---UNICODE AWARE
$CCODE