WSHShell.Run versus WSHShell.Exec

rocatis
Member Posts: 163
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:
Method 1:
Method 2:
Method 3:
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?
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 IntegerMethod 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
Senior NAV Developer
Elbek & Vejrup
0
Comments
-
[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!0 -
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?
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.0 -
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.
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 & Vejrup0 -
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 ..0
Categories
- All Categories
- 73 General
- 73 Announcements
- 66.6K Microsoft Dynamics NAV
- 18.7K NAV Three Tier
- 38.4K NAV/Navision Classic Client
- 3.6K Navision Attain
- 2.4K Navision Financials
- 116 Navision DOS
- 851 Navision e-Commerce
- 1K NAV Tips & Tricks
- 772 NAV Dutch speaking only
- 617 NAV Courses, Exams & Certification
- 2K Microsoft Dynamics-Other
- 1.5K Dynamics AX
- 320 Dynamics CRM
- 111 Dynamics GP
- 10 Dynamics SL
- 1.5K Other
- 990 SQL General
- 383 SQL Performance
- 34 SQL Tips & Tricks
- 35 Design Patterns (General & Best Practices)
- 1 Architectural Patterns
- 10 Design Patterns
- 5 Implementation Patterns
- 53 3rd Party Products, Services & Events
- 1.6K General
- 1.1K General Chat
- 1.6K Website
- 83 Testing
- 1.2K Download section
- 23 How Tos section
- 252 Feedback
- 12 NAV TechDays 2013 Sessions
- 13 NAV TechDays 2012 Sessions