Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

BeatOne

Delphi - Incompatible types?

This topic is 5719 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi peeps! I´m sort of a newbie to Delphi, But I have experience in C++ and Visual Basic. I have this error when compiling a "set and get username" program: [Error] Unit1.pas(48): Incompatible types: ''TCaption'' and ''PAnsiChar'' Line 48 is: SetComputerName(edit1.text); I have no idea what to do... Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
The problem is that you''re dealing with the Windows API, which is somewhat old-fashioned -- it expects strings to be passed as a pointer to the first character in the string, using nul as a terminator.

Just typecast the string to force it to the wanted type:

SetComputerName(PChar(Edit1.Text));

or

SetComputerName(PAnsiChar(Edit1.Text));

Remember this: when dealing with the Windows API, you can typecast any string directly to a PChar (or PAnsiChar) as above. The easiest way to know whether you have to do this is to look at the tooltip help that lists the function''s parameters.

The VCL itself uses strings, which are safer and more intuitive than pointer-to-first-char. If you have the choice between a VCL function and a Windows API function then pick the VCL one first unless you''re absolutely paranoid about every clock cycle (you shouldn''t be).

Share this post


Link to post
Share on other sites
Oh, and another couple of points about Delphi strings. First of all, they start at index 1, not 0 (which may catch you out to begin with). You can use the Length function to get how many characters are in a string. Finally, if any of the Windows API functions talk about passing a buffer of a specified size then you can manually set the size using SetLength to ensure enough space.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!