Watch, Follow, &
Connect with Us

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


Welcome, Guest
Guest Settings
Help

Thread: Get a correct string from the return value of widestring


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


Permlink Replies: 2 - Last Post: Mar 17, 2015 12:36 PM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Ricky Lie

Posts: 14
Registered: 10/2/05
Get a correct string from the return value of widestring  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 16, 2015 11:13 PM
Hello,

I hope I'm in the correct section of this forum and need a help of the following problem

I am working with RAD Studio 2010 and importing a zkemkeeper.dll and give me an unit of zkemkeeper_TLB.pas and creating an object of czkem in ActiveX palleteo, one of the function in zkemkeeper_TLB.pas declare as:

function SSR_GetGeneralLogData(dwMachineNumber: Integer; out dwEnrollNumber: WideString;
out dwVerifyMode: Integer; out dwInOutMode: Integer;
out dwYear: Integer; out dwMonth: Integer; out dwDay: Integer;
out dwHour: Integer; out dwMinute: Integer;
out dwSecond: Integer; var dwWorkCode: Integer): WordBool; safecall;

Software Development Kit information of "SSR_GetGeneralLogData":
VARIANT_BOOL SSR_GetGeneralLogData( [in] LONG dwMachineNumber, [out] BSTR*
dwEnrollNumber, [out] LONG* dwVerifyMode, [out] LONG* dwInOutMode, [out] LONG*
dwYear, [out] LONG* dwMonth, [out] LONG* dwDay, [out] LONG* dwHour, [out] LONG*
dwMinute, [out] LONG* dwSecond, [out] LONG* dwWorkcode)

My application (a simple test calling the function):
===========================================================================
procedure TForm1.RetrieveFromDevice;
var
dwMachineNumber,dwVerifyMode,dwInOutMode,dwYear,dwMonth,dwDay,dwHour,dwMinute,dwSecond,dwWorkCode: Integer;

dwEnrollNumber: WideString;

EnrolNumber: string;

begin

CZKEM1.SSR_GetGeneralLogData (dwMachineNumber,dwEnrollNumber,dwVerifyMode,dwInOutMode,dwYear,dwMonth,dwDay,dwHour,dwMinute,dwSecond,dwWorkCode);

EnrollNumber := dwEnrollNumber;

ShowMessage(EnrollNumber);

end;
============================================================================

The result of ShowMessage procedure give me unreadable characters which is suppose to be a user number string of four characters.
Is there any convertion for the widestring type in this case, please help me.

Those return value in type of Integer are retrieved correctly.

I thank you for all comments and suggestions,
Ricky Lie

.
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: Get a correct string from the return value of widestring  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 17, 2015 11:23 AM   in response to: Ricky Lie in response to: Ricky Lie
Ricky Lie wrote:

Hello,

I hope I'm in the correct section of this forum and need a help of
the following problem

I am working with RAD Studio 2010 and importing a zkemkeeper.dll and
give me an unit of zkemkeeper_TLB.pas and creating an object of czkem
in ActiveX palleteo, one of the function in zkemkeeper_TLB.pas
declare as:

function SSR_GetGeneralLogData(dwMachineNumber: Integer; out
dwEnrollNumber: WideString; out
dwVerifyMode: Integer; out dwInOutMode: Integer;
out dwYear: Integer; out dwMonth: Integer; out dwDay: Integer;
out dwHour: Integer; out dwMinute: Integer;
out dwSecond: Integer; var dwWorkCode: Integer): WordBool; safecall;

Software Development Kit information of "SSR_GetGeneralLogData":
VARIANT_BOOL SSR_GetGeneralLogData( [in] LONG dwMachineNumber, [out]
BSTR* dwEnrollNumber, [out] LONG* dwVerifyMode, [out] LONG*
dwInOutMode, [out] LONG* dwYear, [out] LONG* dwMonth, [out] LONG*
dwDay, [out] LONG* dwHour, [out] LONG* dwMinute, [out] LONG*
dwSecond, [out] LONG* dwWorkcode)

My application (a simple test calling the function):
======================================================================
===== procedure TForm1.RetrieveFromDevice;
var

dwMachineNumber,dwVerifyMode,dwInOutMode,dwYear,dwMonth,dwDay,dwHour,d
wMinute,dwSecond,dwWorkCode: Integer;

dwEnrollNumber: WideString;

EnrolNumber: string;

begin

CZKEM1.SSR_GetGeneralLogData
(dwMachineNumber,dwEnrollNumber,dwVerifyMode,dwInOutMode,dwYear,dwMont
h,dwDay,dwHour,dwMinute,dwSecond,dwWorkCode);

EnrollNumber := dwEnrollNumber;

ShowMessage(EnrollNumber);

end;
======================================================================
======

The result of ShowMessage procedure give me unreadable characters
which is suppose to be a user number string of four characters. Is
there any convertion for the widestring type in this case, please
help me.

Since you are using D2010 String is a UTF16 string, and since
Widestring is also supposed to hold UTF16 code points the assignment
should not result in any conversion of the characters held in the
dwEnrollNumber variable (which is as misnamed as the parameter in the
function, by the way. The dw prefix is supposed to identify a "double
word" parameter, so a longword or cardinal, not a widestring).

If you inspect the characters in dwEnrolNumber one by one in the
debugger, perhaps typecast to Word, what do you see? The function may
misuse the widestring as storage for non-character data, you know...


--
Peter Below (TeamB)

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Get a correct string from the return value of widestring  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 17, 2015 12:36 PM   in response to: Ricky Lie in response to: Ricky Lie
Ricky wrote:

The result of ShowMessage procedure give me unreadable characters
which is suppose to be a user number string of four characters.

Without seeing the actual implementation of SSR_GetGeneralLogData(), or at
least the raw data of the BSTR it is returning (and the value you are expecting
it to return), I can only guess that the returned BSTR probably contains
binary/ansi data in it. Although a BSTR usually contains a UTF-16 encoded
character string, this is NOT required by ActiveX/COM! A BSTR may contain
binary/ansi data instead, if it is allocating by SysAllocStringByteLen()
instead of SysAllocString(Len)(). However, the Delphi RTL assumes a BSTR
(and thus WideString) always holds a UTF-16 encoded string. If it contains
binary/ansi data instead, you are going to get weird results.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02