Glenn BarnasAdministrator
(KiX Supporter)
2007-10-23 09:53 PM
WSH issue w/ KF Kode

I mentioned this in the scripting forum a while back...

When I use WSHPipe, WSHPing, or any WSH-based function from within a KixForms process, a command window opens momentarily and then disappears. Is there any way to suppress this? It's downright annoying!

Thanks,

Glenn


Benny69
(MM club member)
2007-10-23 10:06 PM
Re: WSH issue w/ KF Kode

are you useing kix32.exe or wkix32.exe? well i should have to ask you that, my guess is that there is somthing in the code that is poping the command box, can you post your code?

Glenn BarnasAdministrator
(KiX Supporter)
2007-10-23 10:53 PM
Re: WSH issue w/ KF Kode

WKix32 - I've associated .kxf with WKix32. Same deal with 4.53 & 4.6.

I doubt it's code, as it happens all the time.

Here's the WSHPipe udf (with headers removed), with a call to Ping Localhost. For me, a console window opens when I run "WKix32 .\test.kxf". I added the sleep commands to simulate the prep that goes on in my other commands. This example illustrates what happens before I instantiate KF, during initialization. Once KF has opened a form, future calls to WSH-related processes pop up additional windows - so it is not KF related.

Glenn
 Code:
Break On

Sleep 3
$a = WSHPipe('ping localhost', 1)
Sleep 3

;Function: 
;   WshPipe() 
; 
;Author: 
;   Christopher Shilt (christopher.shilt@relizon.com) 
Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
	Dim $oExec, $Output
	$oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
	If Not VarType($oExec)=9 $WshPipe="WScript.Shell Exec Unsupported" Exit 10 EndIf
	$Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
	If Not $NoEcho $Output Endif
	$WshPipe=Split(Join(Split($Output,CHR(13)),''),CHR(10))
	Exit($oExec.ExitCode)
EndFunction




Benny69
(MM club member)
2007-10-24 01:58 AM
Re: WSH issue w/ KF Kode

I think you are right its not the code.

I have slightly modified the code to show the steps in the code:
 Code:
Break On

Sleep 3
$a = WSHPipe('ping localhost', 1)
Sleep 3

;Function: 
;   WshPipe() 
; 
;Author: 
;   Christopher Shilt (christopher.shilt@relizon.com) 
Function WshPipe($ShellCMD, OPTIONAL $NoEcho)
	Dim $oExec, $Output
	
  $oExec = CreateObject("WScript.Shell").Exec($ShellCMD)
  ? 1
  Get $
  
  If Not VarType($oExec) = 9
    $WshPipe="WScript.Shell Exec Unsupported"
    Exit 10
  EndIf
  
  $Output = $oExec.StdOut.ReadAll + $oExec.StdErr.ReadAll
  ? 2
  Get $
  
  If Not $NoEcho
    $Output
  EndIf
	
  $WshPipe=Split(Join(Split($Output,Chr(13)),''),Chr(10))
  Exit($oExec.ExitCode)
  
EndFunction


I think what is being seen is the output (maybe output is not the correct word) of ping.exe its self.


Glenn BarnasAdministrator
(KiX Supporter)
2007-10-24 04:46 AM
Re: WSH issue w/ KF Kode

Gee, thanks - now I have two windows opening! \:o

It's clear that one is from the Ping command, since the title shows "C:\Windows\System32\Ping.exe". The other is the expected window with the output from your "1" "2" statements.

The length of time the Ping window was open was surprising until I added "-n 1" - do that and you'll see just how quickly it flashes by in my app.

So, pal - remember that the goal is to eliminate the WSH window, not add more!

Glenn


NTDOCAdministrator
(KiX Master)
2007-10-24 09:15 AM
Re: WSH issue w/ KF Kode

Been a while since I've run it in production but I didn't have any window open that a user could see (back around 4.2x days as I recall.

Try an all new hand coded script (not a kgen version) and see if it still happens for you on a minimal script.


Richard H.Administrator
(KiX Supporter)
2007-10-24 10:11 AM
Re: WSH issue w/ KF Kode

I think you may be out of luck.

From MS technet article http://www.microsoft.com/technet/scriptcenter/guide/sas_wsh_pkoy.mspx?mfr=true (my emphasis)
 Quote:
You might want to run the application in a specified window type, such as a minimized window. Exec offers no control over window style


I can think of a couple of options if it really is a problem for you.
  1. Use WSHShell.RUN instead which has control over the window style. You will need to redirect output to a file and read it in so it's not a great solution.
  2. While you are waiting for the command to complete grab the window and minimise / hide it. The console will flash on the screen.
  3. Start the script from kix32.exe rather than wkix32.exe. If you do this the script will create a console that you can manage, and the WSH Exec will use the same console so you won't have additional ones popping up whenever you run a command. You can SetConsole("HIDE") when your script starts, so it should only be a minor irritation.


JochenAdministrator
(KiX Supporter)
2007-10-24 12:36 PM
Re: WSH issue w/ KF Kode

On the other side you could also use the shell function of kf classic.
It may work but is quite undocumented \:\)

TypeLibrary Viewer on kixforms.dll yields:

Function Shell ([ByVal Cmd]) As Variant

Searching for a sample....


JochenAdministrator
(KiX Supporter)
2007-10-24 12:45 PM
Re: WSH issue w/ KF Kode

here is a demo script posted by Shawn on kf.org:


Break On

$System = CreateObject("Kixtart.System")

$Form = $System.Form()
$Form.Size = 600,400
$Form.Text = "Kixpad"
$Form.FontName = "Tahoma"
$Form.FontSize = 8.25

$CommandLabel = $Form.Controls.Add("Label")
$CommandLabel.Left = 5
$CommandLabel.Top = 5
$CommandLabel.Width = 65
$CommandLabel.Text = "Command:"
$CommandLabel.TextAlign = "MiddleLeft"

$CommandTextBox = $Form.Controls.Add("TextBox")
$CommandTextBox.Top = $CommandLabel.Top
$CommandTextBox.Left = $CommandLabel.Right
$CommandTextBox.Right = $Form.ClientWidth - 90
$CommandTextBox.Anchor = 13
$CommandTextBox.Text = 'cmd /c dir %%systemroot%%'

$ExecuteButton = $Form.Controls.Add("Button")
$ExecuteButton.Text = "Execute"
$ExecuteButton.Top = $CommandLabel.Top
$ExecuteButton.Left = $CommandTextBox.Right + 10
$ExecuteButton.Anchor = 9
$ExecuteButton.OnClick = "ExecuteButton_Click()"

$ListingTextBox = $Form.Controls.Add("TextBox")
$ListingTextBox.MultiLine = "True"
$ListingTextBox.ScrollBars = "Both"
$ListingTextBox.Left = 5
$ListingTextBox.Top = $CommandLabel.Bottom + 5
$ListingTextBox.Right = $Form.ClientWidth - 5
$ListingTextBox.Bottom = $Form.ClientHEight - 35
$ListingTextBox.Anchor = 15
$ListingTextBox.FontName = "Courier New"
$ListingTextBox.FontSize = 10
$ListingTextBox.BackColor = "Black"
$ListingTextBox.ForeColor = "White"
$ListingTextBox.MaxLength = 0

$StyleLabel = $Form.Controls.Add("Label")
$StyleLabel.Left = 5
$StyleLabel.Top = $ListingTextBox.Bottom + 5
$StyleLabel.Width = 80
$StyleLabel.Text = "Window Style"
$StyleLabel.TextAlign = "MiddleLeft"

$StyleComboBox = $Form.Controls.Add("ComboBox")
$StyleComboBox.Top = $StyleLabel.Top
$StyleComboBox.Left = $StyleLabel.Right
$StyleComboBox.Width = 150
$StyleComboBox.DropDownStyle = 2

$WindowStyles = "Hidden", "Normal (focus)", "Minimized (focus)", "Maximized", "Normal", "Minimized"

For Each $Style In $WindowStyles
 $= $StyleComboBox.AddItem($Style)
Next

$StyleComboBox.ListIndex = 4

$CloseButton = $Form.Controls.Add("Button")
$CloseButton.Top = $ListingTextBox.Bottom + 5
$CloseButton.Text = "Close"
$CloseButton.Left = $ExecuteButton.Left
$CloseButton.Anchor = 10
$CloseButton.OnClick = "CloseButton_Click()"

$Form.AcceptButton = $ExecuteButton
$Form.CancelButton = $CloseButton

$Form.Center
$Form.Show

$CommandTextBox.Focus

While $Form.Visible
 $= Execute($Form.DoEvents)
Loop

Exit 0

Function CloseButton_Click()
 $Form.Hide
EndFunction

Function ExecuteButton_Click()

 $WindowStyle = $StyleComboBox.ListIndex

 $ListingTextBox.Text = $System.Shell($CommandTextBox.Text,$WindowStyle,3)

EndFunction


You can set the WindowStyle to hidden, thus no console popup happens, and instead of assigning the console output to a TextBox text property you can assign it to a variable for later analysis \:\)


Benny69
(MM club member)
2007-10-24 01:39 PM
Re: WSH issue w/ KF Kode

Code Tags, Code Tags! Man, slacker ;\)

Glenn BarnasAdministrator
(KiX Supporter)
2007-10-24 01:42 PM
Re: WSH issue w/ KF Kode

The above example was hand-coded and it still pops up the box, and the UDF was straigt off KORG. Can't get more basic than that. You can always tell a KGen'd script because line 1 looks like ";;KixGenerated <date>"

Glenn


JochenAdministrator
(KiX Supporter)
2007-10-24 01:47 PM
Re: WSH issue w/ KF Kode

No,

you just have to select 'hidden' in the dropdown (ComboBox) below the huge black TextBox and the WindowStyle value of the Shell function will get set to 0.

Instead of displaying the result to a textbox like in the demo :

 Quote:

$ListingTextBox.Text = $System.Shell($CommandTextBox.Text,$WindowStyle,3)


you can as well do this :

 Code:

$result = $System.Shell($CommandTextBox.Text,$WindowStyle,3)
if instr($result,"whatever")
    "it's a whatever !!!"
endif



See?


JochenAdministrator
(KiX Supporter)
2007-10-24 01:48 PM
Re: WSH issue w/ KF Kode

 Originally Posted By: Benny69
Code Tags, Code Tags! Man, slacker ;\)


Nay nay, I don't want to see any more of these code scroll pains no more


Glenn BarnasAdministrator
(KiX Supporter)
2007-10-24 01:49 PM
Re: WSH issue w/ KF Kode

Thanks, Richard..

1. I was trying to move away from the temp-file issue, which is what I was using with standard "shell" commands.
2. The short duration of the commands - "ping -n 1 host" and "nslookup host" flash so quickly that this isn't practical, unless your DNS is down (which is a bigger issue anyway)
3. I do all my debugging in Kix32 - set a debug flag and monitor the code progress. Never saw the popup then. One issue, though, is that use of MessageBox tends to bring the console above the KF main window, since MessageBox is associated with Kix and not KF process, I suppose.

Too bad WSH can't run as a minimized window, or even on the hidden desktop.

Glenn


Glenn BarnasAdministrator
(KiX Supporter)
2007-10-24 01:53 PM
Re: WSH issue w/ KF Kode

Undocumented? $#%@$#%$#!! Shawn!!!! Wait till I get my hands on that sheep-___ @$# #$%$# (&*$$ (*%$ Grrrr!

I'm gonna have to give this a try. Thanks, Jochen! (code tags not withstanding;) ) This looks like what I need.

Glenn


JochenAdministrator
(KiX Supporter)
2007-10-24 02:56 PM
Re: WSH issue w/ KF Kode

Well, I have to take the undocumented statement back ...

It is a Method of the System object, so I didn't find it immediately :

 Quote:

Kixforms Class Library
Shell Method


Description
Loads and runs a program.

Syntax
object.Shell(command, window-style, redirection)

Parameters
command

Command can be any 16-bit or 32-bit application. To run command interpreter commands, specify the correct command interpreter as part of the command.

window-style (optional)

One of the following values:

Value Setting Description
0 Hidden Window is hidden and focus is passed to the hidden window.
1 Normal (focused) Window has focus and is restored to its original size and position.
2 Minimized (focused) Window is displayed as an icon with focus. This is the default.
3 Maximized Window is maximized with focus.
4 Normal Window is restored to its most recent size and position. The currently active window remains active.
6 Minimized Window is displayed as an icon. The currently active window remains active.


redirection (optional)

One of the following values: Value Setting Description
0 None No redirection. This is the default.
1 Stdout Redirect standard output.
2 Stderr Redirect standard error.
3 Both Redirect both standard output and standard error.


Return Value
If redirection is specified, returns the redirected output as a string; otherwise, returns the integer exit code of the command.

Remarks
Script execution is stopped until the program exits.

Example
$Listing = $System.Shell("cmd /c dir c:\myfolder", 0, 3)

For Each $Line In Split($Listing, @CRLF)
?"Line=" $Line
Next
See Also
Applies To: System



It seems to have problems taking %ComSpec% as input though ...


Glenn BarnasAdministrator
(KiX Supporter)
2007-10-24 04:00 PM
Re: WSH issue w/ KF Kode

OK - maybe I'm caffiene depleted, but using
 Code:
Break On
Dim $System, $result
$System = CreateObject('Kixtart.System')$result = $System.Shell('cmd.exe /c ping.exe -n 1 fileserver1',0,3)
if instr($result,"TTL=")
    "Responds!" ?
endif

still pops up a window <sssssiip> (Ahhh!)
.
.
.
Oh, wait - that's from the "responds" line!

This is sweet!!

Shawn, I take back what I said, and will buy you the cutest little sheep...

Jochen - thanks for the heads up on this, I'll be adding some KFPing and KFNsLookup UDFs today...

Glenn \:D


JochenAdministrator
(KiX Supporter)
2007-10-24 04:23 PM
Re: WSH issue w/ KF Kode

Yeah, no problem, just make sure to add the dependencies list ;\)

JochenAdministrator
(KiX Supporter)
2008-06-04 11:37 PM
Re: WSH issue w/ KF Kode

Oh hey, btw.

If you just need to check for a reply of a remote host, my ping() thing udf from a couple years ago WON'T produce any temporary file unless you need to get the resolved host name from an IP address back from it.

I dunno if this was ever set right, and I know this is an old post, but it was just referenced in another post


Glenn BarnasAdministrator
(KiX Supporter)
2008-06-05 12:18 AM
Re: WSH issue w/ KF Kode

Yeah, but Gargoyle wasn't pinging, he was running a command with WSHPipe and it was popping up that annoying CMD screen flash... \:o

Glenn


ShawnAdministrator
(KiX Supporter)
2008-06-05 04:32 AM
Re: WSH issue w/ KF Kode

Where da sheep ?!

Mart
(KiX Supporter)
2008-06-05 01:41 PM
Re: WSH issue w/ KF Kode

 Originally Posted By: Shawn
Where da sheep ?!


Kinda off topic but here's one.