Take the 2-minute tour ×

I am getting an error in my delphi code when using shellexecute to call an external progrem

procedure TReceiverMainForm.btnSearchClick(Sender: TObject);
//  args := ' /d='  + TIdURI.URLEncode(Trim(sSearch)) + ' /t=ReceiverMainForm /s=30 /m=1'
args := ' /q='  + httpencode(Trim(txtSearch.Text)) + ' /t=ReceiverMainForm /s='+Trim(txtS.Text)+' /m='+Trim(txtM.Text);
  ShellExecute(Handle, 'open', 'YTD.exe', pWideChar(args), nil, SW_SHOWNORMAL);

how do i reslve this error?

share|improve this question
Perhaps, you should try ShellExecuteW instead. –  Andriy M Sep 22 '12 at 23:12
@Andriy That would fix the compile error, but the code would fail at runtime. Would need args to be WideString also. Since args is built out of 8 bit data, there's nothing to be gained from using the wide API. –  David Heffernan Sep 23 '12 at 8:07
@DavidHeffernan: Ah, yes, you are right. Don't know why I didn't pay attention to args's type. Even if it worked, there would be no point in using the Unicode version instead of the ANSI one, as the original string wasn't a Unicode one. –  Andriy M Sep 23 '12 at 18:58
add comment

1 Answer

up vote 7 down vote accepted

The ShellExecute function is defined like so

function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,
  Directory: PChar; ShowCmd: Integer): HINST; stdcall;

So the issue is which you are casting the args variable to a PWideChar instead of a PChar.

Try this code

ShellExecute(Handle, 'open', 'YTD.exe', PChar(args), nil, SW_SHOWNORMAL);

Remember that Delphi 7 pre-dates the Unicode Delphi development. And so PChar is an alias for the 8 bit character type, PAnsiChar.

share|improve this answer
add comment

Your Answer


