Recent

Author Topic: How to properly use ANSI and UNICODE functions  (Read 3452 times)

Sergios

  • New Member
  • *
  • Posts: 10
How to properly use ANSI and UNICODE functions
« on: September 16, 2014, 12:05:47 am »
Suppose I am declare following functions:
Code: [Select]
function FuncA (cText: Char): Boolean; external 'somelib.dll' name 'FuncA';
function FuncW (wText: WChar): Boolean; external 'somelib.dll' name 'FuncW';
and
Code: [Select]
function Func (tText: TCHAR): Boolean;Now, I want to use Func under ANSI or UNICODE declaration.
How can I do it?
Something like this:
Code: [Select]
function Func (tText: TCHAR): Boolean;
begin
{$IFDEF UNICODE}
  Result:=FuncW(tText);
{$ELSE}
  Result:=FuncA(tText);
{$ENDIF}
end;
Is there more elegant solution?
Windows 8.1 Pro, Lazarus 1.2.4, FPC 2.6.4

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to properly use ANSI and UNICODE functions
« Reply #1 on: September 16, 2014, 12:31:13 am »
I'm guessing that you ar comming from a C++ background there is no tchar in freepascal there are a number of ways t oaccomplish your goal the easiest one is to create 2 overloaded functions something along the lines of
Code: [Select]
  function Func(tText :widestring):boolean; overload;
  begin
    Result := FuncW(tText);
  end;
  function Func(tText :Ansistring):boolean; overload;
  begin
    Result := FuncA(tText);
  end;
procedure test;
begin
  if not Func(AnsiString('Testing') then func(wideString('TEsting please');
end;
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: How to properly use ANSI and UNICODE functions
« Reply #2 on: September 16, 2014, 03:49:37 am »
Or:
Code: [Select]
function Func(aText: AnsiString): Boolean; overload; external 'somelib.dll' name 'FuncA';
function Func(wText: WideString): Boolean; overload; external 'somelib.dll' name 'FuncW';
...
begin
  Func(AnsiString('ABC'));
  Func(WideString('ABC'));
end.

Edit:
Welcome to Lazarus/FPC.
« Last Edit: September 16, 2014, 03:53:07 am by engkin »

Sergios

  • New Member
  • *
  • Posts: 10
Re: How to properly use ANSI and UNICODE functions
« Reply #3 on: September 16, 2014, 05:51:01 am »
taazz,
engkin
Thanks for the help and suggestions
Windows 8.1 Pro, Lazarus 1.2.4, FPC 2.6.4

 

TinyPortal © 2005-2018