WSHShell.Run versus WSHShell.Exec

rocatisrocatis Member Posts: 163
edited 2013-07-18 in NAV Tips & Tricks
Everybody dreads the "You are about to run the following executable for the first time" message you get when using the SHELL command. The workarounds listed below seem to be the most popular.

Each workaround consists of a function with the following variables:
Parameters:
_commandLine	Text		1024
_runModally	Boolean		

Return Value:
exitValue		Integer

Local variables:
wSHShell		Automation	'Windows Script Host Object Model'.WshShell	
wSHExec		Automation	'Windows Script Host Object Model'.WshExec	
wSHExecStatus	Integer
Method 1, using WSHShell.Exec:
CREATE(wSHShell);

wSHExec := wSHShell.Exec(_commandLine);

IF NOT _runModally THEN
  EXIT(0);

WHILE wSHExecStatus = 0 DO BEGIN
  SLEEP(500);
  wSHExecStatus := wSHExec.Status;
END;

EXIT(wSHExec.ExitCode);
Method 2, using WSHShell.Exec:
CREATE(wSHShell);

wSHExec := wSHShell.Exec(
  STRSUBSTNO(
    '%1 /C %2',
    ENVIRON('COMSPEC'),
    _commandLine));

IF NOT _runModally THEN
  EXIT(0);

WHILE wSHExecStatus = 0 DO BEGIN
  SLEEP(500);
  wSHExecStatus := wSHExec.Status;
END;

EXIT(WSHExec.ExitCode);
Method 3, using WSHShell.Run:
CREATE(wSHShell);

dummyInt := 1;

EXIT(wSHShell.run(_commandLine,dummyInt,_runModally));
However, none of them are ideal:

Method 1:
    Runs both modally and non-modally. Does not work with associated program types (i.e. passing c:\mytext.txt as _commandLine will NOT launch NotePad (or whatever application has been associated with the txt extension)). Supports launching of applications (i.e. passing c:\myprog.exe as _commandLine).

Method 2:
    Runs both modally and non-modally. Works with associated program types. Does not support launching of applications.

Method 3:
    Runs non-modally but runs modally in a very strange fashion: NAV will wait for a result from the function before continuing executing code, but at the same time NAV is NOT locked, meaning you can navigate the application exactly like you're used to. You could say that the modality is limited to the call itself, whereas NAV normally expands the modality to the whole application. Works with associated program types. Does not support launching of applications.

It's fair to say that Method 3 is inferior to Method 2 if we disregard the much simpler implementation. That leaves Methods 1 and 2 which are unfortunately diametrically opposite: you need them both in your application if you want to run programs both by association and directly. But the worst part is that you can't always be sure which of the functions you need to invoke which leads me to my question:

Has anybody succeeded in creating a function that works with both associated and direct application launching while at the same type suppressing the annoying "You are about to.." message?
Brian Rocatis
Senior NAV Developer
Elbek & Vejrup

Comments

  • krikikriki Member, Moderator Posts: 9,112
    [Topic moved from 'NAV/Navision' forum to 'NAV Tips & Tricks' forum]
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • fverkelfverkel Member Posts: 66
    Has anybody succeeded in creating a function that works with both associated and direct application launching while at the same type suppressing the annoying "You are about to.." message?
    Yes.
    The trick is this: use a Text Constant for the executable. Then it is seen as a trusted executable, that means no annoying question.
    See the online help text (F1) for SHELL.
    SHELL(ExplorerTC, 'C:\MyFile.csv'); // Text Constant ExplorerTC = C:\windows\explorer.exe
    
    This starts Excel, assuming file type csv is linked to Excel.

    I made a function like this:
    StartTrustedExecutable(pExecutableCode : Code[20];pFilenameTxt : Text[250])
    
    IF pFilenameTxt[1] <> '"' THEN
      pFilenameTxt := '"' + pFilenameTxt + '"'; // Add quotes when omitted
    
    CASE pExecutableCode OF
      'EXPLORER': SHELL(ExplorerTC, pFilenameTxt);
      'NOTEPAD' : SHELL(NotepadTC, pFilenameTxt);
      'MYPROG' : SHELL(MyProgTC, pFilenameTxt);
      ELSE
        ERROR('Wrong function call: StartTrustedExecutable %1 %2.', pExecutableCode, pFilenameTxt);
    END;
    
    with these local Text Constants:
    ExplorerTC = c:\windows\explorer.exe
    NotepadTC = c:\windows\notepad.exe
    MyProgTC = C:\myprog.exe

    Usage:
    StartTrustedExecutable('EXPLORER', 'C:\MyFile.csv'); // If you want associated file types
    StartTrustedExecutable('NOTEPAD', 'C:\MyFile.csv'); // If you want to start notepad
    StartTrustedExecutable('MYPROG', 'C:\MyFile.csv'); // If you want to start MyProg
    
    Keep It Simple and Stupid (KISS), but never oversimplify.
  • rocatisrocatis Member Posts: 163
    fverkel wrote:
    The trick is this: use a Text Constant for the executable. Then it is seen as a trusted executable, that means no annoying question.
    I'm aware that this is the case. My problem is that I'm trying to run an executable which the user has specified in a setup table. :wink:

    If you look at my examples, the functions are all called with the command line to execute and cannot be implemented using text constants.
    Brian Rocatis
    Senior NAV Developer
    Elbek & Vejrup
  • mdPartnerNLmdPartnerNL Member Posts: 802
    But if you use fverkels example will this work for you? He's giving examples and all so it won't be that hard to investigate the registry HKEY_CLASSES_ROOT\.csv and ..
Sign In or Register to comment.