Reputation: 21
I want to do something like this:
ShellExecute(0, nil, PChar('E:\generic\mpv\mpv.exe'), PChar(''+TntOpenDialog1.FileName+''), nil, SW_HIDE);
but nothing happened.
When I try to do something with code like this:
TntLabel1.Caption:=PChar(TntOpenDialog1.FileName);
it just shows me the first letter of file which I selected before.
How can I solve this problem?
Upvotes: 0
Views: 1267
Reputation: 612794
You've got an encoding mismatch:
TntOpenDialog1.FileName
is UTF-16 encoded.PChar
is an alias for PAnsiChar
and so is ANSI.ShellExecute
is an alias for ShellExecuteA
, the ANSI variant.This means that you are passing UTF-16 data to a function that expects ANSI encoded text. Hence the observed behaviour.
Replace PChar
with PWideChar
and ShellExecute
with ShellExecuteW
to correct that mismatch.
In the first instance, to convince yourself that this is the problem, change your debugging code to this:
TntLabel1.Caption := PWideChar(TntOpenDialog1.FileName);
Use ShellExecuteExW
if you wish to be able to report errors properly. Use CreateProcessW
to create the process directly and avoid involving the shell.
If
'' + TntOpenDialog1.FileName + ''
is not a typo from when you wrote the question, then that's a problem too. Because ''
is just the empty string.
In other words, that expression is equal to
TntOpenDialog1.FileName
You would need to use:
'"' + TntOpenDialog1.FileName + '"'
in order to quote the argument and escape any space characters.
Upvotes: 2
Reputation: 11958
if you space in the file path expect, you can not pass a param like ''+myparam+''
var
myparam : AnsiString;
begin
myparam := 'test file nr 10.txt';
ShellExecute(0, nil, PChar('...'), PChar(''+myparam+''), nil, SW_HIDE);
ParamStr(1) will give you only test
!!
If you have spaces in the path use it like:
ShellExecute(0, nil, PChar('...'), PChar('"'+myparam+'"'), nil, SW_HIDE);
Try following. but take care with the cast from FFileName: WideString
to AnsiString, information may be lost. Use this only if there are no special characters in the file path occur.
...
var
AnsiStr : AnsiString;
...
begin
AnsiStr := TntOpenDialog1.FileName;
ShellExecute(0, nil, PChar('E:\generic\mpv\mpv.exe'), PChar('"'+AnsiStr+'"'), nil, SW_HIDE);
Upvotes: -1