PDA

View Full Version : Delphi: IsTextUnicode() API calling


Kaiousama
28th February 2004, 15:57
I'm experiencing problems in IsTextUnicode windows API calling from Delphi7.
My goal is to check if the passed string is Unicode or not, secondary goal is to recognise UTF type.

I writted this simple procedure:

procedure CheckUnicode;
Const
IS_TEXT_UNICODE_ASCII16 = $1;
IS_TEXT_UNICODE_REVERSE_ASCII16 = $10;
IS_TEXT_UNICODE_STATISTICS = $2;
IS_TEXT_UNICODE_REVERSE_STATISTICS = $20;
IS_TEXT_UNICODE_CONTROLS = $4;
IS_TEXT_UNICODE_REVERSE_CONTROLS = $40;
IS_TEXT_UNICODE_SIGNATURE = $8;
IS_TEXT_UNICODE_REVERSE_SIGNATURE = $80;
IS_TEXT_UNICODE_ILLEGAL_CHARS = $100;
IS_TEXT_UNICODE_ODD_LENGTH = $200;
IS_TEXT_UNICODE_DBCS_LEADBYTE = $400;
IS_TEXT_UNICODE_NULL_BYTES = $1000;
IS_TEXT_UNICODE_UNICODE_MASK = $F;
IS_TEXT_UNICODE_REVERSE_MASK = $F0;
IS_TEXT_UNICODE_NOT_UNICODE_MASK = $F00;
IS_TEXT_UNICODE_NOT_ASCII_MASK = $F000;

var
MyString : String;
pMyString : ^String;
MyOpt : Integer;
pMyOpt : ^Integer;

begin
MyString:= 'Test';
pMyString := @MyString;
MyOpt := IS_TEXT_UNICODE_UNICODE_MASK;
pMyOpt := @MyOpt;

if IsTextUnicode(pMyString,length(MyString),pMyOpt) then ShowMessage('Unicode');
end;


but Delphi refuse to compile giving that error in the last line (the API calling):
"Incompatible types: 'frmMain.Integer' and 'Windows.Integer'"

From Windows SDK help the function IsTextUnicode is declared in this way:

DWORD IsTextUnicode(CONST LPVOID lpBuffer, int cb, LPINT lpi);

so, if i've well understood, the parameter "LPINT lpi" is a pointer to an integer value.... and that's what i passed with the pMyOpt parameter.

I'm totally confused #_#. Please help me to solve this problem.
Thanks

[Toff]
28th February 2004, 19:32
Something like this should works :


begin
MyString:= 'Test';
MyOpt := IS_TEXT_UNICODE_UNICODE_MASK;
if IsTextUnicode(PChar(MyString),Length(MyString),@MyOpt) then
ShowMessage('Unicode');
end;


Also beware of Length() here, with string it will be ok, but with WideString it will return you the number of bytes divided by 2.
IsTextUnicode wants a length in bytes.