Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Delphi 2010 Check if a PChar is Empty

Posted on 2010-09-02
4
Medium Priority
?
1,340 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

704 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