Putting a char into a Pchar

Putting a char into a Pchar

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.

WinAPI calls can be pretty confusing, huh? Let’s say you have a functioncalled WinAPICall that takes a PChar as an argument. Here are a couple ofways to make the call:First Method (This will only work for Delphi 2.0, which supports casting):

Second Method:
procedure CallWinApiCall(S : String);var  Val  : String;  pVal : PChar;begin  Val := S;  {Initialize memory for the PChar}  GetMem(pVal, Length(Val));  {Copy the contents of Val to PChar}  pVal := StrPCopy(pVal, Val);  WinAPICall(pVal);    {This next step is ABSOLUTELY necessary}  FreeMem(pVal, Length(Val));end;  
In any case, that should do it for you pretty nicely.

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist