Forum > Beginners

How to properly use ANSI and UNICODE functions

(1/1)

Sergios:
Suppose I am declare following functions:

--- Code: ---function FuncA (cText: Char): Boolean; external 'somelib.dll' name 'FuncA';
function FuncW (wText: WChar): Boolean; external 'somelib.dll' name 'FuncW';
--- End code ---
and

--- Code: ---function Func (tText: TCHAR): Boolean;
--- End code ---
Now, I want to use Func under ANSI or UNICODE declaration.
How can I do it?
Something like this:

--- Code: ---function Func (tText: TCHAR): Boolean;
begin
{$IFDEF UNICODE}
  Result:=FuncW(tText);
{$ELSE}
  Result:=FuncA(tText);
{$ENDIF}
end;
--- End code ---
Is there more elegant solution?

taazz:
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: ---  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;

--- End code ---

engkin:
Or:

--- Code: ---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.

--- End code ---

Edit:
Welcome to Lazarus/FPC.

Sergios:
taazz,
engkin
Thanks for the help and suggestions

Navigation

[0] Message Index

Go to full version