Solved

Delphi 2010 Check if a PChar is Empty

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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 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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

752 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