Solved

Delphi 2010 Check if a PChar is Empty

Posted on 2010-09-02
4
1,325 Views
Last Modified: 2012-05-10
Hello,

In Delphi 2007 i have the following function to check if a PChar is Empty...you should remember that in Delphi 2007 PChar = PAnsiChar and not PWideChar as actually in Delphi 2010 of course hehe

function EmptyPChar(P: PChar): Boolean;
begin
  try
    Result := (P = nil) or (P^ = #0);
  except
    Result := True;
  end;
end;

//Added to support PAnsiChar...
function EmptyPAnsiChar(P: PAnsiChar): Boolean;
begin
  try
    Result := (P = nil) or (P^ = #0);
  except
    Result := True;
  end;
end;

Works fine... but in Delphi 2010 making some tests like

type
  TSomething = record
    bla: PChar;
    ble: PChar;
    bli: PAnsiChar;
  end;

var
  Something: TSomething;
begin
  Something.ble := 'AAA';

  if EmptyPChar(Something.bla) then
  ShowMessage('Empty PChar'); //should appears... but isnt!

  if EmptyPAnsiChar(Something.bli) then
  ShowMessage('Empty PAnsiChar');

Should show both messages, since i just set BLE and not BLA but just show "Empty PAnsiChar"
But Something.bla is empty!
If i remove the bli: PAnsiChar; from the record, works fine!

Soembody has another or the right way to get compatible with Delphi 2010?

Regards,
Carlos
0
Comment
Question by:cebasso
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Expert Comment

by:irishbuddha
ID: 33588059
This actually works as noted in Delphi 2009 with a copy/paste of your functions and test.
I'll boot 2010 after lunch as I'm a bit low on resources at the moment and check it out there, but it's possible there is a configuration setting that may be getting in the way here.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 33588100
Carlos,

Local variables can/will contain random data until a value is assigned to them. You should clear this record before attempting to examine the fields within.

  FillChar(Something, SizeOf(Something), 0);

Regards,
Russell
0
 
LVL 4

Author Closing Comment

by:cebasso
ID: 33588273
@rllibby yeahh really! i forgot about this :D

thank you!
0
 
LVL 4

Author Comment

by:cebasso
ID: 33588288
and sorry for this mistake/stupid question!

i'm very late in my project and now more and more since i need to get it working Delphi 2010
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month5 days, 13 hours left to enroll

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question