Skip to main content
gamedev.net
🔒 Locked

Delphi - Incompatible types?

Started by BeatOne 2 replies 1.2k views
Original Post
BeatOne
BeatOne
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!
Alimonster
Alimonster
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).
Alimonster
Alimonster
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.
BeatOne
BeatOne
Thank you!
What more can I say?

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.