KiXforms Forum Index KiXforms
The Forum for the KiXforms Community
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 Quick Links 
Site News
Downloads
Documentation
Donations
Script Archive
Tracking Systems

Ping: Pings a host and redirects output back to form.

 
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion
View previous topic :: View next topic  
Author Message
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Mon Oct 03, 2005 2:22 am    Post subject: Ping: Pings a host and redirects output back to form. Reply with quote

Requires the latest version of KiXforms.NET

Code:

Break On

;; s. tassie

$System = CreateObject("Kixforms.System")

$Form = $System.Form()
$Form.AutoScaleBaseSize = $System.Size(5, 13);
$Form.DockPadding.All = 5
$Form.Text = "Ping"

$ReplyTextbox = $Form.Controls.Add($System.TextBox())
$ReplyTextbox.MultiLine = "True"
$ReplyTextbox.Dock = $System.DockStyle_Fill
$ReplyTextbox.BackColor = $System.Color.Black
$ReplyTextbox.ForeColor = $System.Color.PaleGreen
$ReplyTextbox.ScrollBars = $System.ScrollBars_Both

$TopPanel = $Form.Controls.Add($System.Panel())
$TopPanel.Height = 30
$TopPanel.DockPadding.All = 5
$TopPanel.Dock = $System.DockStyle_Top

$HostnameTextbox = $TopPanel.Controls.Add($System.TextBox())
$HostnameTextbox.Text = "127.0.0.1"
$HostnameTextbox.Dock = $System.DockStyle_Fill

$PingButton = $TopPanel.Controls.Add($System.Button())
$PingButton.FlatStyle = $System.FlatStyle_System;
$PingButton.Text = "Ping Host";
$PingButton.Dock = $System.DockStyle_Right
$PingButton.Click = "PingButtonClick()"

$BottomPanel = $Form.Controls.Add($System.Panel())
$BottomPanel.Height = 30
$BottomPanel.DockPadding.All = 5
$BottomPanel.Dock = $System.DockStyle_Bottom

$CloseButton = $BottomPanel.Controls.Add($System.Button())
$CloseButton.DialogResult = $System.DialogResult_Cancel;
$CloseButton.FlatStyle = $System.FlatStyle_System;
$CloseButton.Text = "Close";
$CloseButton.Click = "CloseButtonClick()"
$CloseButton.Dock = $System.DockStyle_Right

$Form.AcceptButton = $PingButton;
$Form.CancelButton = $CloseButton;
$Form.ClientSize = $System.Size(416, 278);

$Form.Center
$Form.Show

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

Exit 0

Function PingButtonClick()

 Dim $ProcessStartInfo
 Dim $line

 $ProcessStartInfo = $System.ProcessStartInfo()
 $ProcessStartInfo.FileName = "ping"
 $ProcessStartInfo.Arguments = $HostnameTextbox.Text
 $ProcessStartInfo.CreateNoWindow = "True"
 $ProcessStartInfo.UseShellExecute = "False";
 $ProcessStartInfo.WindowStyle = $System.ProcessWindowStyle_Hidden;
 $ProcessStartInfo.RedirectStandardOutput = "True";

 Dim $Process

 $Process = $System.Process();
 $Process.StartInfo = $ProcessStartInfo

 If $Process.Start()

  Do

   $line = $Process.StandardOutput.ReadLine

   If $line

    $ReplyTextbox.AppendText($line + @CRLF)

   Endif

  Until vartype($line) = 0

 Else

  $ReplyTextbox.Text = @SERROR
 
 Endif

 $ReplyTextbox.AppendText(@CRLF)

EndFunction

Function CloseButtonClick()

 $Form.Close

EndFunction
Back to top
View user's profile Send private message
Radimus
KiXforms Enthusiast
KiXforms Enthusiast


Joined: 05 Mar 2003
Posts: 157
Location: Tampa, Florida

PostPosted: Mon Oct 03, 2005 1:13 pm    Post subject: Reply with quote

something like this (completely untested, but it looks good I suppose)

Code:
Function KFCMD($filename, optional $args, optional $objHandle)
   Dim $ProcessStartInfo, $Process ,$line, $nul

   $ProcessStartInfo = $System.ProcessStartInfo()
   $ProcessStartInfo.FileName = $filename
   $ProcessStartInfo.Arguments = $args
   $ProcessStartInfo.CreateNoWindow = "True"
   $ProcessStartInfo.UseShellExecute = "False"
   $ProcessStartInfo.WindowStyle = $System.ProcessWindowStyle_Hidden
   $ProcessStartInfo.RedirectStandardOutput = "True"

   $Process = $System.Process()
   $Process.StartInfo = $ProcessStartInfo

   If $Process.Start()
      Do
         $line = $Process.StandardOutput.ReadLine
         If $line and $objHandle
            $nul=execute("$$"+$objHandle+".AppendText($line + @CRLF)")
         Endif
      Until vartype($line) = 0
   Else
      $nul=execute("$$"+$objHandle+".AppendText(@serror)")
   Endif

    if $objHandle
      $nul=execute("$$"+$objHandle+".AppendText(@CRLF)")
   Endif
EndFunction
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Tue Oct 04, 2005 8:50 am    Post subject: Reply with quote

Umm... might be me, but I can't get this to work...
No matter what i put in, I always get "green light" Confused

Kix ver: 4.50
KiXforms.NET ver: 3.1.15.0
Back to top
View user's profile Send private message MSN Messenger
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Tue Oct 04, 2005 10:43 am    Post subject: Reply with quote

green light ?
Back to top
View user's profile Send private message
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Tue Oct 04, 2005 11:21 am    Post subject: Reply with quote

Shawn wrote:
green light ?


Hehe... yes. Smile
I get two green "bars" (--> || <--), and I suppose thats what I should get if I get a ping response.

Problem is that if I write "thisisnohost", I still get the two bars and no error.
If I write 123.123.123.123, I still get no error, and so on.

I might be wrong, but $line never returns anything?
Back to top
View user's profile Send private message MSN Messenger
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Tue Oct 04, 2005 11:46 am    Post subject: Reply with quote

Are you able to get any command with redirection working. What happens if you you try this script, which uses Rad's KFCMD udf to list the contents of the C:\ drive ...

Code:

Break On

;; s. tassie
 
$System = CreateObject("Kixforms.System")

$Form = $System.Form()
$Form.AutoScaleBaseSize = $System.Size(5, 13);
$Form.DockPadding.All = 5
$Form.Text = "Ping"

$ReplyTextbox = $Form.Controls.Add($System.TextBox())
$ReplyTextbox.MultiLine = "True"
$ReplyTextbox.Dock = $System.DockStyle_Fill
$ReplyTextbox.BackColor = $System.Color.Black
$ReplyTextbox.ForeColor = $System.Color.PaleGreen
$ReplyTextbox.ScrollBars = $System.ScrollBars_Both

$TopPanel = $Form.Controls.Add($System.Panel())
$TopPanel.Height = 30
$TopPanel.DockPadding.All = 5
$TopPanel.Dock = $System.DockStyle_Top

$HostTextbox = $TopPanel.Controls.Add($System.TextBox())
$HostTextbox.Text = "127.0.0.1"
$HostTextbox.Dock = $System.DockStyle_Fill

$PingButton = $TopPanel.Controls.Add($System.Button())
$PingButton.FlatStyle = $System.FlatStyle_System;
$PingButton.Text = "Ping Host";
$PingButton.Dock = $System.DockStyle_Right
$PingButton.Click = "PingButtonClick()"

$BottomPanel = $Form.Controls.Add($System.Panel())
$BottomPanel.Height = 30
$BottomPanel.DockPadding.All = 5
$BottomPanel.Dock = $System.DockStyle_Bottom

$CloseButton = $BottomPanel.Controls.Add($System.Button())
$CloseButton.DialogResult = $System.DialogResult_Cancel;
$CloseButton.FlatStyle = $System.FlatStyle_System;
$CloseButton.Text = "Close";
$CloseButton.Click = "CloseButtonClick()"
$CloseButton.Dock = $System.DockStyle_Right

$Form.AcceptButton = $PingButton;
$Form.CancelButton = $CloseButton;
$Form.ClientSize = $System.Size(416, 278);

$Form.Center
$Form.Show

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

Exit 0

Function PingButtonClick()

 $= KFCMD("%comspec%","/c dir c:\",$ReplyTextBox)

EndFunction

Function KFCMD($filename, optional $args, optional $objHandle)

   Dim $ProcessStartInfo, $Process ,$line, $nul

   $ProcessStartInfo = $System.ProcessStartInfo()
   $ProcessStartInfo.FileName = $filename
   $ProcessStartInfo.Arguments = $args
   $ProcessStartInfo.CreateNoWindow = "True"
   $ProcessStartInfo.UseShellExecute = 0
   $ProcessStartInfo.WindowStyle = $System.ProcessWindowStyle_Hidden
   $ProcessStartInfo.RedirectStandardOutput = "True"

   $Process = $System.Process()
   $Process.StartInfo = $ProcessStartInfo

   If $Process.Start()
      Do
         $line = $Process.StandardOutput.ReadLine
         If $line and $objHandle
            $objHandle.AppendText($line + @CRLF)
         Endif
      Until vartype($line) = 0
   Else
      $objHandle.AppendText(@serror)
   Endif

   If $objHandle
    $objHandle.AppendText(@CRLF)
   Endif

EndFunction

Function CloseButtonClick()

 $Form.Close

EndFunction

Back to top
View user's profile Send private message
Viggen
KiXforms Regular
KiXforms Regular


Joined: 28 Jun 2005
Posts: 40

PostPosted: Tue Oct 04, 2005 12:52 pm    Post subject: Reply with quote

That showed the contents of c:\ Very Happy
(in a dosbox that flashes by very quickly)
Back to top
View user's profile Send private message MSN Messenger
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Tue Oct 04, 2005 12:56 pm    Post subject: Reply with quote

Thanks for trying that out. Still not sure what the issue is with ping though. I do know that there is something "funky" about shelling and redirecting with ping in general. I can't back that up with any details though.
Back to top
View user's profile Send private message
Radimus
KiXforms Enthusiast
KiXforms Enthusiast


Joined: 05 Mar 2003
Posts: 157
Location: Tampa, Florida

PostPosted: Tue Oct 04, 2005 12:58 pm    Post subject: Reply with quote

it works fine for me... output in the 'ping form' from above

Thanks for making the UDF work
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Shawn
KiXforms Developer
KiXforms Developer


Joined: 22 Feb 2003
Posts: 1983
Location: Canada

PostPosted: Tue Oct 04, 2005 1:12 pm    Post subject: Reply with quote

Did your first version not work ? I just changed it 'cause I'm not a big Execute statement fan (other than executing DoEvents of course) ;0)
Back to top
View user's profile Send private message
Radimus
KiXforms Enthusiast
KiXforms Enthusiast


Joined: 05 Mar 2003
Posts: 157
Location: Tampa, Florida

PostPosted: Tue Oct 04, 2005 1:14 pm    Post subject: Reply with quote

I just typed it out... I never ran it myself. It looked right though.

I've never gotten the hang of passing handles through a UDF, Your version is much more efficient
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Lonkero
KiXforms Devotee
KiXforms Devotee


Joined: 13 Mar 2003
Posts: 1022
Location: Espoo, Finland

PostPosted: Tue Oct 04, 2005 3:46 pm    Post subject: Reply with quote

shawn, your ping never worked for me either Smile

anyways, ping needs a async style handling.
thus, you need to wait for the output from it.

something like:
Code:

...
If $Process.Start()
  do
   sleep 0.1
   $line = $Process.StandardOutput.ReadLine
  until $line
  while $line
   $ReplyTextbox.AppendText($line + @CRLF)
   $line = $Process.StandardOutput.ReadLine
  loop
else
...

_________________
Hammer
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    KiXforms Forum Index -> KiXforms.NET Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group