| |
| Typecasting a PChar to a longint. |
 |
Many Windows functions claim to want PChar parameters in the documentation, but they are defined as requiring LongInts. Is this a bug?
No, this is where "typecasting" is used. Typecasting allows you to fool the compiler into thinking that one type of variable is of another type for the ultimate in flexibility. The last parameter of the Windows API function SendMessage() is a good example. It is documented as requiring a long integer, but commonly requires a PChar for some messages (WM_WININICHANGE). Generally, the variable you are typecasting from must be the same size as the variable type you are casting it to. In the SendMessage example, you could typecast a PChar as a longint, since both occupy 4 bytes of memory:
Example:
var
s : array[0..64] of char;
begin
StrCopy(S, 'windows');
SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(@S));
end;
Pointers are the easiest to typecast, and are the most flexible since
you can pass anything to the called procedure, and the most dangerous
for the same reason. You can also use untyped variable parameters for
convenience, although var parameters are really pointers behind the
scenes.
Example:
type
PBigArray = ^TBigArray;
TBigArray = Array[0..65000] of char;
procedure ZeroOutPointerVariable(P : pointer; size : integer);
var
i : integer;
begin
for i := 0 to size - do
PBigArray(p)^[i] := #0;
end;
procedure ZeroOutUntypedVariable(var v, size : integer);
var
i : integer;
begin
for i := 0 to size - do
TBigArray(v)[i] := #0;
end;
procedure TForm .Button Click(Sender: TObject);
var
s : string[255];
i : integer;
l : longint;
begin
s := 'Hello';
i := 0;
l := 2000;
ZeroOutPointerVariable(@s, sizeof(s));
ZeroOutPointerVariable(@i, sizeof(i));
ZeroOutPointerVariable(@l, sizeof(l));
s := 'Hello';
i := 0;
l := 2000;
ZeroOutUntypedVariable(s, sizeof(s));
ZeroOutUntypedVariable(i, sizeof(i));
ZeroOutUntypedVariable(l, sizeof(l));
end;
|
|
| Hits/month |
2,500,000+ |
Downloads (Since May 2000) |
7,393,709 |
| Total Files |
6,023 |
| Forum msgs |
7,670 |
| Articles/FAQs |
70+/900+ |
Top Selling Software at Amazon
|