Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Windows API WinExec troubles ...

Status
Not open for further replies.

FlatWhatson

Programmer
May 14, 2003
3
0
0
AU
Could somebody please tell me why this code works:

Code:
WinExec('ping [ipaddress]', SW_SHOW);

Where [ipaddress] is an IP address (obviously!).
But when i use a 'PAnsiChar' string-like variable (PAnsiChar is the type that it wants you to use when using the WinExec command) constructed with text from an edit box it doesn't work (ie. Delphi likes it but when running the code does nothing, just like if you had have said WinExec('fred', SW_Show); or something):

Code:
var PingCommand : string;
    DOSCommand : PAnsiChar;

procedure TToolbox_Form.Ping_ButtonClick(Sender: TObject);
begin
     PingCommand := 'ping ' + PingIP_Edit.Text;
     DOSCommand := addr(PingCommand);
     WinExec(DOSCommand, SW_Show);
end;

I know that somehow the string that I am creating is wrong somewhere, but I can't for the life of me see where!

Any help would be greatly appreciated. :)
 
How about:

Code:
procedure TToolbox_Form.Ping_ButtonClick(Sender: TObject);
begin
     DOSCommand := 'ping ' + PingIP_Edit.Text;
     WinExec(DOSCommand, SW_Show);
end;
(Untested ;-) )

Delphi wil do the 'conversion' for you.

HTH
TonHu
 
If I just use the PAnsiChar variable instead of the string variable Delphi just complains that 'String' and 'PAnsiChar' are incompatible types. The conversion from string to PAnsiChar needs to happen as far as I know, beleive me.
 
And a typecast like
Code:
...PAnsiChar(PingCommand)...
in the WinExec line?

HTH
TonHu
 
Well you should be using 'ShellExecute' or 'Createproccess'
'WinExec' is only there for backwards comptibility with W3.X
The parameters for these are PChar you can use strpcopy to do the conversions, have a look at the ExecuteFile function in 'Demos/doc/filmanex/fmxutils.pas'
Steve
 

I got this code from internet. Works well in my XP and Win98
locally and when I use Win98 accessing XP as it was a server. But when i have Win98 with a Linux server, it hangs and i got no answer. If you open taskmanager you see several WinOldAp (one for each call i made to external EXEs).
What could i use instead ?


function ShellEXE( sFileEXE, sParam, pDir: PChar; iShow: Word; bWait: Boolean): LongInt;
var
OK : Boolean;
Info: TShellExecuteInfo;
begin
FillChar(Info, SizeOf(Info), Chr(0));
Info.cbSize := SizeOf(Info);
Info.fMask := SEE_MASK_NOCLOSEPROCESS;
Info.lpVerb := 'open';
Info.lpFile := sFileEXE;
Info.lpParameters := sParam;
Info.lpDirectory := pDir;
Info.nShow := iShow;
OK := Boolean(ShellExecuteEx(@Info));
if OK then
begin
if bWait then
begin
while WaitForSingleObject(Info.hProcess,100) =
WAIT_TIMEOUT do
Application.ProcessMessages;
OK := GetExitCodeProcess(Info.hProcess,
DWord(Result));
end
else
Result := 0;
end;
if not OK
then Result := -1;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top