Author: Lou Adler
I'm using the Winexec command and trying to use a string as the argument, but
Winexec takes only a Pchar as its argument. I can't figure out how to put a regular
string into a Pchar, or how to make a Pchar 'point' to a character string.
Answer:
WinAPI calls can be pretty confusing, huh? Let's say you have a function called
WinAPICall that takes a PChar as an argument. Here are a couple of ways to make the
call:
First Method (This will only work for Delphi 2.0 and above, which supports casting):
WinAPICall(PChar(MyStringVal));
Second Method:
1 procedure CallWinApiCall(S: string);
2 var3 Val: string;
4 pVal: PChar;
5 begin6 Val := S;
7 {Initialize memory for the PChar}8 GetMem(pVal, Length(Val));
9 10 {Copy the contents of Val to PChar}11 pVal := StrPCopy(pVal, Val);
12 WinAPICall(pVal);
13 14 {This next step is ABSOLUTELY necessary}15 FreeMem(pVal, Length(Val));
16 end;
In any case, that should do it for you pretty nicely.