Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Pchar result IN UDF


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Dec 8, 2016 2:10 PM Last Post By: Jeff Overcash (...
Eric ten Westen...

Posts: 8
Registered: 4/4/10
Pchar result IN UDF  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 7, 2016 11:05 AM
Hi

if i understand , to use Unicode i need to use PChars in UDF

What wrong here?

function GetFullname(const FirstName: PChar; const Lastname: PChar) : PChar;
var
fn : string;
ln : string;
res: string;
begin
fn := Firstname;
ln := Lastname;
res := Firstname + ' ' = Lastname;

Result := ib_util_malloc(1+Length(res));
Result := StrPCopy(Result, rs);
end;

Eric

end;
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Pchar result IN UDF  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 7, 2016 3:52 PM   in response to: Eric ten Westen... in response to: Eric ten Westen...
Eric ten Westenend wrote:
Hi

if i understand , to use Unicode i need to use PChars in UDF

What wrong here?

function GetFullname(const FirstName: PChar; const Lastname: PChar) : PChar;
var
fn : string;
ln : string;
res: string;
begin
fn := Firstname;
ln := Lastname;
res := Firstname + ' ' = Lastname;

Result := ib_util_malloc(1+Length(res));
Result := StrPCopy(Result, rs);
end;

Eric

end;

No, you should still declare them as PAnsiChar. PChar in Delphi is a UTF16
string. InterBase does not know to convert the string into UTF16 before calling
the UDF, it will send it basically as a RawByteString.

Encode the incoming array from a UTF8 to UTF16 (using TEncoding). Work with it,
then Encode the result back to UTF8. Usually name your function to include the
encoding expected if something other than Ansi.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Eric ten Westen...

Posts: 10
Registered: 6/1/15
Re: Pchar result IN UDF  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 7, 2016 11:14 PM   in response to: Jeff Overcash (... in response to: Jeff Overcash (...
Hi Jeff

So better not to use PChar at all?

You write:
Encode the incoming array from a UTF8 to UTF16 (using TEncoding). Work with it,
then Encode the result back to UTF8.

What advantage do i have doing this?

Eric


Jeff Overcash (TeamB) wrote:
Eric ten Westenend wrote:
Hi

if i understand , to use Unicode i need to use PChars in UDF

What wrong here?

function GetFullname(const FirstName: PChar; const Lastname: PChar) : PChar;
var
fn : string;
ln : string;
res: string;
begin
fn := Firstname;
ln := Lastname;
res := Firstname + ' ' = Lastname;

Result := ib_util_malloc(1+Length(res));
Result := StrPCopy(Result, rs);
end;

Eric

end;

No, you should still declare them as PAnsiChar. PChar in Delphi is a UTF16
string. InterBase does not know to convert the string into UTF16 before calling
the UDF, it will send it basically as a RawByteString.

Encode the incoming array from a UTF8 to UTF16 (using TEncoding). Work with it,
then Encode the result back to UTF8. Usually name your function to include the
encoding expected if something other than Ansi.

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)
Jeff Overcash (...

Posts: 1,529
Registered: 9/23/99
Re: Pchar result IN UDF  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Dec 8, 2016 2:10 PM   in response to: Eric ten Westen... in response to: Eric ten Westen...
Eric ten Westenend wrote:
Hi Jeff

So better not to use PChar at all?

Correct, PChar in current Unicode Delphi means a pointer to a UTF16 string.
This is NOT what the server will be passing. PAnsiString does assume a pointer
to an AnsiString, but is more easily treated as a byte array. You also might
look at PRawByteString so make sure no under the hood conversions are happening.

You write:
Encode the incoming array from a UTF8 to UTF16 (using TEncoding). Work with it,
then Encode the result back to UTF8.

What advantage do i have doing this?

You have to get it into a form you can work with. All InterBase is passing you
is a pointer to an array of data. Best thing is to first encode it into a
normal Delphi string if you want to use any string function or the RTL. Then
encode it back into what InterBase expects before returning it.

Eric

--
Jeff Overcash (TeamB)
(Please do not email me directly unless asked. Thank You)
Learning is finding out what you already know. Doing is demonstrating that you
know it. Teaching is reminding others that they know it as well as you. We are
all leaners, doers, teachers. (R Bach)

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02