Solved

Delphi 2010 Check if a PChar is Empty

Posted on 2010-09-02
4
1,265 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
  • 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now