Recent

Author Topic: [SOLVED] Assessing if first character of WideChar Array is NULL  (Read 4318 times)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
[SOLVED] Assessing if first character of WideChar Array is NULL
« on: February 28, 2019, 09:09:47 pm »
I realise this is a stupid question but I want to make sure I am not doing something wrong.

I am using some API functions (which are DELPHI based) and one of these functions is used to retrieve information about items. If there is no information available to retrieve for a given item, the documentation states (for the API function in question) : "If no textual designation is retrieved, the first character in the buffer is set to NULL."

So the function call looks like this :

Code: Pascal  [Select][+][-]
  1.   The_API_FunctionName( LONG ItID, LPWSTR lpDescr, DWORD nBufferLengthAndFlags) : LONG
  2.  

And so one of my example calls is :

Code: Pascal  [Select][+][-]
  1. {$mode Delphi}{$H+}  
  2. ....
  3. const
  4.   BufLen=256;
  5. ...
  6. var
  7.   itemtypeinfo : integer;
  8.   lpDescr : array[0..Buflen-1] of WideChar;    
  9. ...
  10. begin
  11.   ...
  12.   itemtypeinfo := FunctionName(ItID, @lpDescr, SizeOf(lpDescr) + $40000000);
  13.   ...
  14. end;
  15.  

That works fine for items where a textual description is returned for lpDescr.

However, I am trying to catch the occurrences where nothing is returned in lpDescr. My understanding from the documentation is that is achieved by assessing the first value of the lpDescr array, or, more precisely, the buffer pointer, which is set to NULL if its empty. I THINK I can do that by executing :

Code: Pascal  [Select][+][-]
  1. if @lpDescr[0] = nil then // assess if the first value of lpDescr is NULL
  2.   begin
  3.      // Do something to take care of the emtpiness
  4.   end;  
  5.  

It compiles OK, but never executes, even when I know some of the items have no value to return so it must empty several times. So I am assuming I am assessing the first character of lpDescr incorrectly or I am assessing the pointer to it incorrectly?

Can anyone help?
« Last Edit: March 04, 2019, 11:31:26 pm by Gizmo »

jamie

  • Hero Member
  • *****
  • Posts: 7420
Re: Assessing if first character of WideChar Array is NULL
« Reply #1 on: February 28, 2019, 10:42:23 pm »
it will never be NIL pointer because you give the function a real array of WideChar to use..

You need to test the first entry in the array.

if ipDesc[0] = #0 Then etc....

or
 If word(ipDesc[0]) = 0 Then ….
The only true wisdom is knowing you know nothing

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 880
Re: Assessing if first character of WideChar Array is NULL
« Reply #2 on: March 01, 2019, 09:21:06 am »
Emmm. May be it's simply Length(lpDescr) = 0? I.e. you don't even need to perform some special handling for this situation. As it's null-terminated string anyway and null in first element just means "empty string". Same as const S:String = ''.
« Last Edit: March 01, 2019, 09:24:19 am by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Assessing if first character of WideChar Array is NULL
« Reply #3 on: March 01, 2019, 10:48:40 am »
Just for curiosity, what is the (LONG) value returned by that API function? Because if it's the actual length of the returned WideString, you may be able to just check it for zero without  more ado.

Ignore me: itemtypeinfo ... hadn't seen that :-[


ETA - But this ....
FunctionName( ... DWORD nBufferLengthAndFlags) : LONG

That's a disaster waiting to happen :)
« Last Edit: March 01, 2019, 10:54:55 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1568
    • Lebeau Software
Re: Assessing if first character of WideChar Array is NULL
« Reply #4 on: March 01, 2019, 08:30:21 pm »
ETA - But this ....
FunctionName( ... DWORD nBufferLengthAndFlags) : LONG

That's a disaster waiting to happen :)

Agreed.  Does it take a BYTE length or a CHARACTER length?  Using SizeOf() is appropriate for the first case, but is wrong for the second case when dealing with a WideChar array, use Length() instead.  Also, since the parameter mixes length and flags together, that also imposes a max limit to the size of the buffer that can be used so as not to overflow into the flags.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Assessing if first character of WideChar Array is NULL
« Reply #5 on: March 04, 2019, 12:10:22 am »
Thanks guys...yes, I had missed that. The API doc does say "contains the length of the buffer that the buffer points to in the lower word, measured in characters, and may be optionally combined with one of the below flags...". So I think it is correct to use "SizeOf(lpDescr) and $40000000"

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Assessing if first character of WideChar Array is NULL
« Reply #6 on: March 04, 2019, 12:32:10 am »
The API doc does say "contains the length of the buffer that the buffer points to in the lower word, measured in characters [..] So I think it is correct to use "SizeOf(lpDescr) and $40000000"

Not if the length is really in characters. SizeOf() returns the size in bytes. Use:
Code: [Select]
Length(lpDescr) and $40000000and make sure that "Length(lpDescr) <= $FFFF"
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 880
Re: Assessing if first character of WideChar Array is NULL
« Reply #7 on: March 04, 2019, 01:47:17 pm »
Not if the length is really in characters. SizeOf() returns the size in bytes. Use:
Code: [Select]
Length(lpDescr) and $40000000and make sure that "Length(lpDescr) <= $FFFF"
Actually
Code: [Select]
Length(lpDescr) or $40000000But + will work too.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Assessing if first character of WideChar Array is NULL
« Reply #8 on: March 04, 2019, 02:39:17 pm »
Actually
Code: [Select]
Length(lpDescr) or $40000000But + will work too.

Ouch! Good catch. I concentrated on the SizeOf  vs. Length and missed that :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Gizmo

  • Hero Member
  • *****
  • Posts: 831
Re: Assessing if first character of WideChar Array is NULL
« Reply #9 on: March 04, 2019, 11:31:13 pm »
Thanks...I'd already discovered that myself but didn't get round to coming back. Thanks for pointing it out nevertheless.

 

TinyPortal © 2005-2018