Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ShellExecute Problem!!

Hi!
When i try to open a URL with a certain web browser using
ShellExecute like this:

ShellExecute(Handle,'open',PChar(BrowserExeFile),PChar(URL),nil,SW_SHOWNORMAL);

it fails! The web browser opens the home page instead of the URL!

When i try to open a document (.txt or .rtf of .doc) with Wordpad or Word this way:

ShellExecute(Handle,'open',PChar(WordPad),PChar(file),nil,SW_SHOWNORMAL);

it fails to find the specified file. This happens only when the path to the file includes a folder with a name including spaces. For example,
if file='C:MyFolderPoetry.doc' then there is no problem. But if
file='C:My Folder.doc' then Wordpad opens with this message: "C:My Cannot find this file".

Any idea why these strange thinks happen?

Note: Ι use Delphi6 and Fully qualified filenames.

Comments

  • zibadianzibadian Posts: 6,349Member
    : Hi!
    : When i try to open a URL with a certain web browser using
    : ShellExecute like this:
    :
    : ShellExecute(Handle,'open',PChar(BrowserExeFile),PChar(URL),nil,SW_SHOWNORMAL);
    :
    : it fails! The web browser opens the home page instead of the URL!
    :
    : When i try to open a document (.txt or .rtf of .doc) with Wordpad or Word this way:
    :
    : ShellExecute(Handle,'open',PChar(WordPad),PChar(file),nil,SW_SHOWNORMAL);
    :
    : it fails to find the specified file. This happens only when the path to the file includes a folder with a name including spaces. For example,
    : if file='C:MyFolderPoetry.doc' then there is no problem. But if
    : file='C:My Folder.doc' then Wordpad opens with this message: "C:My Cannot find this file".
    :
    : Any idea why these strange thinks happen?
    :
    : Note: Ι use Delphi6 and Fully qualified filenames.
    :
    The reason, why the program cannot open c:my folder.doc, is that that's an illegal filename.
    As for the URL, it is easier to use the URL as the executable. That way it will load it into the default browser. Sometimes browsers need additional paramters to link to an URL. See the documentation of the browser if that's the case.
  • KapetaniosKapetanios Posts: 12Member
    The reason, why the program cannot open c:my folder.doc, is that that's an illegal filename.
    : As for the URL, it is easier to use the URL as the executable. That way it will load it into the default browser. Sometimes browsers need additional paramters to link to an URL. See the documentation of the browser if that's the case.


    1) Oups!! Sorry! Type mistake! What i try and fails is file='C:My FolderPoetry.doc' and not file='C:My Folder.doc' and, so ,it is a legal name but it doesn't work. Notepad works fine with this parameter but Wordpad and Word seem to have problem with this filepath that contains a space.

    2) Hmm...i will check the parameters needed for the browser!


  • zibadianzibadian Posts: 6,349Member
    : The reason, why the program cannot open c:my folder.doc, is that that's an illegal filename.
    : : As for the URL, it is easier to use the URL as the executable. That way it will load it into the default browser. Sometimes browsers need additional paramters to link to an URL. See the documentation of the browser if that's the case.
    :
    :
    : 1) Oups!! Sorry! Type mistake! What i try and fails is file='C:My FolderPoetry.doc' and not file='C:My Folder.doc' and, so ,it is a legal name but it doesn't work. Notepad works fine with this parameter but Wordpad and Word seem to have problem with this filepath that contains a space.
    :
    : 2) Hmm...i will check the parameters needed for the browser!
    :
    :
    :
    Place the filename in quotes. That makes of a filepath with spaces a single commandline parameter: "C:My FolderPoetry.doc"
  • KapetaniosKapetanios Posts: 12Member
    : :
    : Place the filename in quotes. That makes of a filepath with spaces a single commandline parameter: "C:My FolderPoetry.doc"
    :


    Yeap! It worked!! :-))) Thank you!

    What hasn't work is opening the web browser with the url as a parameter.
    While "C:.....firefox.exe www.yahoo.com" work through the command line,
    the :
    ShellExecute(handle,'open',PChar(BrowserExePath),PChar(URL),nil,SW_SHOWNORMAL);
    doesn't work!

    I tried to make it throught WinExec, too, but the result was the same.
  • zibadianzibadian Posts: 6,349Member
    : : :
    : : Place the filename in quotes. That makes of a filepath with spaces a single commandline parameter: "C:My FolderPoetry.doc"
    : :
    :
    :
    : Yeap! It worked!! :-))) Thank you!
    :
    : What hasn't work is opening the web browser with the url as a parameter.
    : While "C:.....firefox.exe www.yahoo.com" work through the command line,
    : the :
    : ShellExecute(handle,'open',PChar(BrowserExePath),PChar(URL),nil,SW_SHOWNORMAL);
    : doesn't work!
    :
    : I tried to make it throught WinExec, too, but the result was the same.
    :
    Try using this:
    [code]
    ShellExecute(handle, nil, PChar(URL), nil , nil, SW_SHOWNORMAL);
    [/code]
    That should work.

  • KapetaniosKapetanios Posts: 12Member
    : :
    : Try using this:
    : [code]
    : ShellExecute(handle, nil, PChar(URL), nil , nil, SW_SHOWNORMAL);
    : [/code]
    : That should work.
    :
    :
    Of course!! I know!
    But it opens the default browser.
    What am i trying to do is open a non-default browser at a certain URL.
    I can open a certain URL, i can open a non-default browser but i cannot
    do both in one, because i cannot pass parameters to the browser. I know it must be a way. When i right-click an html file and choose open with... choosing a non-default browser...it works!!! When i do that through command line, it works!! I don't know. It should be a way that involves window APIs.
    Whatever, let's forget it for the moment. If i find something a will
    make a new post here.
  • zibadianzibadian Posts: 6,349Member
    : : :
    : : Try using this:
    : : [code]
    : : ShellExecute(handle, nil, PChar(URL), nil , nil, SW_SHOWNORMAL);
    : : [/code]
    : : That should work.
    : :
    : :
    : Of course!! I know!
    : But it opens the default browser.
    : What am i trying to do is open a non-default browser at a certain URL.
    : I can open a certain URL, i can open a non-default browser but i cannot
    : do both in one, because i cannot pass parameters to the browser. I know it must be a way. When i right-click an html file and choose open with... choosing a non-default browser...it works!!! When i do that through command line, it works!! I don't know. It should be a way that involves window APIs.
    : Whatever, let's forget it for the moment. If i find something a will
    : make a new post here.
    :
    This works:
    [code]
    ShellExecute(handle, nil, PChar(MyBrowser), PChar(URL), nil, SW_SHOWNORMAL);
    [/code]
    Notice that I didn't include the operation.
  • KapetaniosKapetanios Posts: 12Member
    : :
    : This works:
    : [code]
    : ShellExecute(handle, nil, PChar(MyBrowser), PChar(URL), nil, SW_SHOWNORMAL);
    : [/code]
    : Notice that I didn't include the operation.
    :

    Hhmmm! Yes, it works!! You made it!

    So, this means that "open" prevents me pass parameters to some applications, like the browser?

    Because it doesn't prevent me from passing parameters to notepad,wordpad or Word.

    So people, be aware of that and two more thing when you use ShellExecute:

    1) When you don't give the application path or the url through '.....' but through a string variable, a constant or Edit1.Text (or something similar) you should convert it through PChar function.

    2) If the filepath is given through a string that contains spaces you may have problem. Make sure that you put the string between "".

    3) Don't forget to declare "Uses ShellAPI;"

    Thanks for your help!
Sign In or Register to comment.