Listing a file in a directory:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("c:\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Sub
Pinging a host:
Option Explicit On
Imports System
Imports System.Net
Imports System.Net.Sockets
'* Class clsPing
'*
'* Author : Paulo S. Silva Jr.
'* Date : September 2002
'* Objective : Ping a host and return basic informations
'*
'* Class Properties
'*
'* +------------+-------------+-------------------------------------------------+
'* | Name | Type | Description |
'* +------------+-------------+-------------------------------------------------+
'* | DataSize | Integer | Size of the data to be send to the host |
'* | Identifier | Integer | Identifier of ping packet |
'* | Sequence | Integer | Sequence of the packet |
'* | LocalHost | IPHostEntry | Info for Local Computer |
'* | Host | Object | Host Entry for the remote computer |
'* +------------+-------------+-------------------------------------------------+
'*
'* Class Methods
'*
'* +-------------------+----------------------------------------------------+
'* | Name | Description |
'* +-------------------+----------------------------------------------------+
'* | PingHost | Pings the specified host |
'* +-------------------+----------------------------------------------------+
'*
'* Parts of the code based on information from Visual Studio Magazine
'* more info :
http://www.fawcette.com/vsm/2002_03/magazi.../qa/default.asp'*
Public Class clsPing
Public Structure stcError
Dim Number As Integer
Dim Description As String
End Structure
Public Const PING_SUCCESS As Long = 0
Public Const PING_ERROR As Long = (-1)
Public Const PING_ERROR_BASE As Long = &H8000
Public Const PING_ERROR_HOST_NOT_FOUND As Long = PING_ERROR_BASE + 1
Public Const PING_ERROR_SOCKET_DIDNT_SEND As Long = PING_ERROR_BASE + 2
Public Const PING_ERROR_HOST_NOT_RESPONDING As Long = PING_ERROR_BASE + 3
Public Const PING_ERROR_TIME_OUT As Long = PING_ERROR_BASE + 4
Private Const ICMP_ECHO As Integer = 8
Private Const SOCKET_ERROR As Integer = -1
Private udtError As stcError
Private Const intPortICMP As Integer = 7
Private Const intBufferHeaderSize As Integer = 8
Private Const intPackageHeaderSize As Integer = 28
Private intDataSize As Byte
Private ipheLocalHost As System.Net.IPHostEntry
Private ipheHost As System.Net.IPHostEntry
Public Property DataSize() As Byte
Get
Return intDataSize
End Get
Set(ByVal Value As Byte)
intDataSize = Value
End Set
End Property
Public ReadOnly Property Identifier() As Byte
Get
Return 0
End Get
End Property
Public ReadOnly Property Sequence() As Byte
Get
Return 0
End Get
End Property
Public ReadOnly Property LocalHost() As System.Net.IPHostEntry
Get
Return ipheLocalHost
End Get
End Property
Public Property Host() As Object
Get
Return ipheHost
End Get
Set(ByVal Value As Object)
If (Value.GetType.ToString.ToLower = "system.string") Then
'*
'* Find the Host's IP address
'*
Try
ipheHost = System.Net.Dns.GetHostByName(Value)
Catch
udtError.Number = PING_ERROR_HOST_NOT_FOUND
udtError.Description = "Host " & ipheHost.HostName & " not found."
ipheHost = Nothing
End Try
ElseIf (Value.GetType.ToString.ToLower = "system.net.iphostentry") Then
ipheHost = (Value)
Else
ipheHost = Nothing
End If
End Set
End Property
'*
'* Class Constructor
'*
Public Sub New()
'*
'* Initializes the parameters
'*
intDataSize = 32
udtError = New stcError()
'*
'* Get local IP and transform in EndPoint
'*
ipheLocalHost = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
'*
'* Defines Host
'*
ipheHost = Nothing
End Sub
'*
'* Function : PingHost
'* Author : Paulo dos Santos Silva Jr
'* Date : 05/09/2002
'* Objective : Pings a specified host
'*
'* Parameters : Host as String
'*
'* Returns : Response time in milliseconds
'* (-1) if error
'*
Public Function Ping() As Long
Dim intCount As Integer
Dim aReplyBuffer(255) As Byte
Dim intNBytes As Integer = 0
Dim intEnd As Integer
Dim intStart As Integer
Dim epFrom As System.Net.EndPoint
Dim epServer As System.Net.EndPoint
Dim ipepServer As System.Net.IPEndPoint
'*
'* Transforms the IP address in EndPoint
'*
ipepServer = New System.Net.IPEndPoint(ipheHost.AddressList(0), 0)
epServer = CType(ipepServer, System.Net.EndPoint)
epFrom = New System.Net.IPEndPoint(ipheLocalHost.AddressList(0), 0)
'*
'* Builds the packet to send
'*
DataSize = Convert.ToByte(DataSize + intBufferHeaderSize)
'*
'* The packet must by an even number, so if the DataSize is and odd number adds one
'*
If (DataSize Mod 2 = 1) Then
DataSize += Convert.ToByte(1)
End If
Dim aRequestBuffer(DataSize - 1) As Byte
'*
'* --- ICMP Echo Header Format ---
'* (first 8 bytes of the data buffer)
'*
'* Buffer (0) ICMP Type Field
'* Buffer (1) ICMP Code Field
'* (must be 0 for Echo and Echo Reply)
'* Buffer (2) checksum hi
'* (must be 0 before checksum calc)
'* Buffer (3) checksum lo
'* (must be 0 before checksum calc)
'* Buffer (4) ID hi
'* Buffer (5) ID lo
'* Buffer (6) sequence hi
'* Buffer (7) sequence lo
'* Buffer (

..(n) Ping Data
'*
'*
'* Set Type Field
'*
aRequestBuffer(0) = Convert.ToByte(ICMPType.Echo)
'*
'* Set ID field
'*
BitConverter.GetBytes(Identifier).CopyTo(aRequestBuffer, 4)
'*
'* Set Sequence
'*
BitConverter.GetBytes(Sequence).CopyTo(aRequestBuffer, 6)
'*
'* Load some data into buffer
'*
Dim i As Integer
For i = 8 To DataSize - 1
aRequestBuffer(i) = Convert.ToByte(i Mod

Next i
'*
'* Calculate Checksum
'*
Call CreateChecksum(aRequestBuffer, DataSize, aRequestBuffer(2), aRequestBuffer(3))
'*
'* Try send the packet
'*
Try
'*
'* Create the socket
'*
Dim sckSocket As New System.Net.Sockets.Socket( _
Net.Sockets.AddressFamily.InterNetwork, _
Net.Sockets.SocketType.Raw, _
Net.Sockets.ProtocolType.Icmp)
sckSocket.Blocking = False
'*
'* Records the Start Time
'*
intStart = System.Environment.TickCount
sckSocket.SendTo(aRequestBuffer, 0, DataSize, SocketFlags.None, ipepServer)
intNBytes = sckSocket.ReceiveFrom(aReplyBuffer, SocketFlags.None, epServer)
intEnd = System.Environment.TickCount
If (intNBytes > 0) Then
'*
'* Informs on GetLastError the state of the server
'*
udtError.Number = (aReplyBuffer(19) * &H100) + aReplyBuffer(20)
Select Case aReplyBuffer(20)
Case 0 : udtError.Description = "Success"
Case 1 : udtError.Description = "Buffer too Small"
Case 2 : udtError.Description = "Destination Unreahable"
Case 3 : udtError.Description = "Dest Host Not Reachable"
Case 4 : udtError.Description = "Dest Protocol Not Reachable"
Case 5 : udtError.Description = "Dest Port Not Reachable"
Case 6 : udtError.Description = "No Resources Available"
Case 7 : udtError.Description = "Bad Option"
Case 8 : udtError.Description = "Hardware Error"
Case 9 : udtError.Description = "Packet too Big"
Case 10 : udtError.Description = "Reqested Timed Out"
Case 11 : udtError.Description = "Bad Request"
Case 12 : udtError.Description = "Bad Route"
Case 13 : udtError.Description = "TTL Exprd In Transit"
Case 14 : udtError.Description = "TTL Exprd Reassemb"
Case 15 : udtError.Description = "Parameter Problem"
Case 16 : udtError.Description = "Source Quench"
Case 17 : udtError.Description = "Option too Big"
Case 18 : udtError.Description = "Bad Destination"
Case 19 : udtError.Description = "Address Deleted"
Case 20 : udtError.Description = "Spec MTU Change"
Case 21 : udtError.Description = "MTU Change"
Case 22 : udtError.Description = "Unload"
Case Else : udtError.Description = "General Failure"
End Select
End If
sckSocket.Close
Return (intEnd - intStart)
Catch oExcept As Exception
'
End Try
End Function
Public Function GetLastError() As stcError
Return udtError
End Function
' ICMP requires a checksum that is the one's
' complement of the one's complement sum of
' all the 16-bit values in the data in the
' buffer.
' Use this procedure to load the Checksum
' field of the buffer.
' The Checksum Field (hi and lo bytes) must be
' zero before calling this procedure.
Private Sub CreateChecksum(ByRef data() As Byte, ByVal Size As Integer, ByRef HiByte As Byte, ByRef LoByte As Byte)
Dim i As Integer
Dim chk As Integer = 0
For i = 0 To Size - 1 Step 2
chk += Convert.ToInt32(data(i) * &H100 + data(i + 1))
Next
chk = Convert.ToInt32((chk And &HFFFF&) + Fix(chk / &H10000&))
chk += Convert.ToInt32(Fix(chk / &H10000&))
chk = Not (chk)
HiByte = Convert.ToByte((chk And &HFF00) / &H100)
LoByte = Convert.ToByte(chk And &HFF)
End Sub
End Class
or one function:
Public Function Ping(ByVal HostToPing As System.Net.IPEndPoint, ByVal TimeOutMs As Integer) As Long
Dim Server As System.Net.IPEndPoint = HostToPing
Dim StartTime As Integer
Dim EndTime As Integer
Dim WaitTime As Integer = 0
Dim DatInt As Byte = 40
Dim RecBuff(255) As Byte
Dim ReqBuf(DatInt - 1) As Byte
ReqBuf(0) = New Byte().Parse(

BitConverter.GetBytes(0).CopyTo(ReqBuf, 4)
BitConverter.GetBytes(0).CopyTo(ReqBuf, 6)
For i As Integer = 8 To 39
ReqBuf(i) = New Byte().Parse(i)
Next i
Dim sckSocket As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, _
Net.Sockets.SocketType.Raw, Net.Sockets.ProtocolType.Icmp)
sckSocket.Blocking = False
'Make the connection and time how long it takes to reply
StartTime = System.Environment.TickCount
sckSocket.SendTo(ReqBuf, 0, DatInt, System.Net.Sockets.SocketFlags.None, Server)
While True
If sckSocket.Poll(1, Net.Sockets.SelectMode.SelectRead) Then
sckSocket.ReceiveFrom(RecBuff, System.Net.Sockets.SocketFlags.None, Server)
EndTime = System.Environment.TickCount
sckSocket.Close()
Return EndTime - StartTime
Else
WaitTime = System.Environment.TickCount
If (WaitTime - StartTime) > TimeOutMs Then
Return -1
End If
End If
End While
End Function
Usage:
Dim x As Long = Ping(New System.Net.IPEndPoint(System.Net.IPAddress.Parse("192.168.0.1"), Nothing),1000)
retrieving username of current windows user:
Declare Function GetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, _
ByRef nSize As Integer) As Integer
Public Function GetUserName() As String
Dim iReturn As Integer
Dim userName As String
userName = New String(CChar(" "), 50)
iReturn = GetUserName(userName, 50)
GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
End Function
Spoofing a web referrer during a web request:
Function FetchURL(SomeURL as String, Referer As String) as String
Dim WebResp as HTTPWebResponse
Dim HTTPGetRequest as HttpWebRequest
Dim sr As StreamReader
dim myString as String
HTTPGetRequest = DirectCast(WebRequest.Create(SomeURL),HttpWebRequest)
HTTPGetRequest.KeepAlive = false
HTTPGetRequest.Referer = Referer
WebResp = HTTPGetRequest.GetResponse()
sr = new StreamReader(WebResp.GetResponseStream(), Encoding.ASCII)
myString = sr.ReadToEnd()
Return myString
End Function
You can then call this using the following:
Dim PageString As String
PageString = FetchURL("
http://www.google.com/","
http://www.microsoft.com")
Sending an email with attachment:
Dim oMsg As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()
oMsg.From = "
noone@nobody.com"
oMsg.To = "
someone@somewhere.com"
oMsg.Subject = "Email with Attachment Demo"
oMsg.Body = "This is the main body of the email"
Dim oAttch As MailAttachment = New MailAttachment("C:\myattachment.zip")
oMsg.Attachments.Add(oAttch)
SmtpMail.Send(oMsg)
Using diff kinds of APIs:
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents btnUserName As System.Windows.Forms.Button
Friend WithEvents btnComputerName As System.Windows.Forms.Button
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents btnGetMouseX As System.Windows.Forms.Button
Friend WithEvents lblMouseX As System.Windows.Forms.Label
Friend WithEvents btnGetMouseY As System.Windows.Forms.Button
Friend WithEvents lblMouseY As System.Windows.Forms.Label
Friend WithEvents btnDriveType As System.Windows.Forms.Button
Friend WithEvents txtDrive As System.Windows.Forms.TextBox
Friend WithEvents btnSwapMouse As System.Windows.Forms.Button
Friend WithEvents btnResetMouse As System.Windows.Forms.Button
Friend WithEvents chkNormalMouseOnExit As System.Windows.Forms.CheckBox
Friend WithEvents trkBeepFreq As System.Windows.Forms.TrackBar
Friend WithEvents trkBeepLength As System.Windows.Forms.TrackBar
Friend WithEvents lblBeepFreq As System.Windows.Forms.Label
Friend WithEvents lblBeepLength As System.Windows.Forms.Label
Friend WithEvents btnBeep As System.Windows.Forms.Button
Friend WithEvents lblHz As System.Windows.Forms.Label
Friend WithEvents lblMs As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.btnUserName = New System.Windows.Forms.Button()
Me.btnComputerName = New System.Windows.Forms.Button()
Me.btnExit = New System.Windows.Forms.Button()
Me.btnGetMouseX = New System.Windows.Forms.Button()
Me.lblMouseX = New System.Windows.Forms.Label()
Me.btnGetMouseY = New System.Windows.Forms.Button()
Me.lblMouseY = New System.Windows.Forms.Label()
Me.btnDriveType = New System.Windows.Forms.Button()
Me.txtDrive = New System.Windows.Forms.TextBox()
Me.btnSwapMouse = New System.Windows.Forms.Button()
Me.btnResetMouse = New System.Windows.Forms.Button()
Me.chkNormalMouseOnExit = New System.Windows.Forms.CheckBox()
Me.trkBeepFreq = New System.Windows.Forms.TrackBar()
Me.trkBeepLength = New System.Windows.Forms.TrackBar()
Me.lblBeepFreq = New System.Windows.Forms.Label()
Me.lblBeepLength = New System.Windows.Forms.Label()
Me.btnBeep = New System.Windows.Forms.Button()
Me.lblHz = New System.Windows.Forms.Label()
Me.lblMs = New System.Windows.Forms.Label()
CType(Me.trkBeepFreq, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.trkBeepLength, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'btnUserName
'
Me.btnUserName.Location = New System.Drawing.Point(16, 16)
Me.btnUserName.Name = "btnUserName"
Me.btnUserName.Size = New System.Drawing.Size(112, 24)
Me.btnUserName.TabIndex = 0
Me.btnUserName.Text = "Show UserName"
'
'btnComputerName
'
Me.btnComputerName.Location = New System.Drawing.Point(144, 16)
Me.btnComputerName.Name = "btnComputerName"
Me.btnComputerName.Size = New System.Drawing.Size(136, 24)
Me.btnComputerName.TabIndex = 1
Me.btnComputerName.Text = "Show ComputerName"
'
'btnExit
'
Me.btnExit.Location = New System.Drawing.Point(208, 320)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(96, 24)
Me.btnExit.TabIndex = 2
Me.btnExit.Text = "E&xit"
'
'btnGetMouseX
'
Me.btnGetMouseX.Location = New System.Drawing.Point(32, 56)
Me.btnGetMouseX.Name = "btnGetMouseX"
Me.btnGetMouseX.Size = New System.Drawing.Size(112, 24)
Me.btnGetMouseX.TabIndex = 3
Me.btnGetMouseX.Text = "Get cursor X"
'
'lblMouseX
'
Me.lblMouseX.Location = New System.Drawing.Point(32, 88)
Me.lblMouseX.Name = "lblMouseX"
Me.lblMouseX.Size = New System.Drawing.Size(112, 16)
Me.lblMouseX.TabIndex = 4
Me.lblMouseX.Text = "X: Press Get"
'
'btnGetMouseY
'
Me.btnGetMouseY.Location = New System.Drawing.Point(160, 56)
Me.btnGetMouseY.Name = "btnGetMouseY"
Me.btnGetMouseY.Size = New System.Drawing.Size(104, 24)
Me.btnGetMouseY.TabIndex = 5
Me.btnGetMouseY.Text = "Get cursor Y"
'
'lblMouseY
'
Me.lblMouseY.Location = New System.Drawing.Point(160, 88)
Me.lblMouseY.Name = "lblMouseY"
Me.lblMouseY.Size = New System.Drawing.Size(104, 16)
Me.lblMouseY.TabIndex = 6
Me.lblMouseY.Text = "Y: Press Get"
'
'btnDriveType
'
Me.btnDriveType.Location = New System.Drawing.Point(88, 120)
Me.btnDriveType.Name = "btnDriveType"
Me.btnDriveType.Size = New System.Drawing.Size(104, 24)
Me.btnDriveType.TabIndex = 7
Me.btnDriveType.Text = "Get Drive Type"
'
'txtDrive
'
Me.txtDrive.Location = New System.Drawing.Point(200, 120)
Me.txtDrive.MaxLength = 1
Me.txtDrive.Name = "txtDrive"
Me.txtDrive.Size = New System.Drawing.Size(24, 21)
Me.txtDrive.TabIndex = 8
Me.txtDrive.Text = "C"
'
'btnSwapMouse
'
Me.btnSwapMouse.Location = New System.Drawing.Point(8, 280)
Me.btnSwapMouse.Name = "btnSwapMouse"
Me.btnSwapMouse.Size = New System.Drawing.Size(120, 24)
Me.btnSwapMouse.TabIndex = 9
Me.btnSwapMouse.Text = "Swap Mouse Buttons"
'
'btnResetMouse
'
Me.btnResetMouse.Location = New System.Drawing.Point(136, 280)
Me.btnResetMouse.Name = "btnResetMouse"
Me.btnResetMouse.Size = New System.Drawing.Size(168, 24)
Me.btnResetMouse.TabIndex = 10
Me.btnResetMouse.Text = "Return Mouse Button Settings"
'
'chkNormalMouseOnExit
'
Me.chkNormalMouseOnExit.CheckAlign = System.Drawing.ContentAlignment.BottomRight
Me.chkNormalMouseOnExit.Location = New System.Drawing.Point(32, 320)
Me.chkNormalMouseOnExit.Name = "chkNormalMouseOnExit"
Me.chkNormalMouseOnExit.Size = New System.Drawing.Size(168, 16)
Me.chkNormalMouseOnExit.TabIndex = 11
Me.chkNormalMouseOnExit.Text = "Normalize my mouse on exit"
'
'trkBeepFreq
'
Me.trkBeepFreq.Location = New System.Drawing.Point(56, 144)
Me.trkBeepFreq.Maximum = 20
Me.trkBeepFreq.Name = "trkBeepFreq"
Me.trkBeepFreq.Orientation = System.Windows.Forms.Orientation.Vertical
Me.trkBeepFreq.Size = New System.Drawing.Size(45, 104)
Me.trkBeepFreq.TabIndex = 12
Me.trkBeepFreq.TickStyle = System.Windows.Forms.TickStyle.Both
'
'trkBeepLength
'
Me.trkBeepLength.Location = New System.Drawing.Point(208, 144)
Me.trkBeepLength.Maximum = 20
Me.trkBeepLength.Name = "trkBeepLength"
Me.trkBeepLength.Orientation = System.Windows.Forms.Orientation.Vertical
Me.trkBeepLength.Size = New System.Drawing.Size(45, 104)
Me.trkBeepLength.TabIndex = 13
Me.trkBeepLength.TickStyle = System.Windows.Forms.TickStyle.Both
'
'lblBeepFreq
'
Me.lblBeepFreq.Location = New System.Drawing.Point(32, 248)
Me.lblBeepFreq.Name = "lblBeepFreq"
Me.lblBeepFreq.Size = New System.Drawing.Size(120, 16)
Me.lblBeepFreq.TabIndex = 14
Me.lblBeepFreq.Text = "Beep Frequency: 0"
'
'lblBeepLength
'
Me.lblBeepLength.Location = New System.Drawing.Point(192, 248)
Me.lblBeepLength.Name = "lblBeepLength"
Me.lblBeepLength.Size = New System.Drawing.Size(104, 16)
Me.lblBeepLength.TabIndex = 15
Me.lblBeepLength.Text = "Beep Length: 0"
'
'btnBeep
'
Me.btnBeep.Location = New System.Drawing.Point(104, 176)
Me.btnBeep.Name = "btnBeep"
Me.btnBeep.Size = New System.Drawing.Size(96, 40)
Me.btnBeep.TabIndex = 16
Me.btnBeep.Text = "Beep with these settings"
'
'lblHz
'
Me.lblHz.Location = New System.Drawing.Point(24, 264)
Me.lblHz.Name = "lblHz"
Me.lblHz.Size = New System.Drawing.Size(120, 16)
Me.lblHz.TabIndex = 17
Me.lblHz.Text = "Cycles per second (Hz)"
'
'lblMs
'
Me.lblMs.Location = New System.Drawing.Point(192, 264)
Me.lblMs.Name = "lblMs"
Me.lblMs.Size = New System.Drawing.Size(96, 16)
Me.lblMs.TabIndex = 18
Me.lblMs.Text = "Milliseconds (MS)"
'
'frmAPI
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
Me.ClientSize = New System.Drawing.Size(314, 352)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblMs, Me.lblHz, Me.btnBeep, Me.lblBeepLength, Me.lblBeepFreq, Me.trkBeepLength, Me.trkBeepFreq, Me.chkNormalMouseOnExit, Me.btnResetMouse, Me.btnSwapMouse, Me.txtDrive, Me.btnDriveType, Me.lblMouseY, Me.btnGetMouseY, Me.lblMouseX, Me.btnGetMouseX, Me.btnExit, Me.btnComputerName, Me.btnUserName})
Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.MaximizeBox = False
Me.Name = "frmAPI"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "APIs for CS54"
CType(Me.trkBeepFreq, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.trkBeepLength, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
executing a process and fetching the output:
Shared Function GetProcessText(ByVal process As String, ByVal param As String, ByVal workingDir As String) As String
Dim p As Process = New Process
' this is the name of the process we want to execute
p.StartInfo.FileName = process
If Not (workingDir = "") Then
p.StartInfo.WorkingDirectory = workingDir
End If
.StartInfo.Arguments = param
' need to set this to false to redirect output
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
' start the process
p.Start
' read all the output
' here we could just read line by line and display it
' in an output window
Dim output As String = p.StandardOutput.ReadToEnd
' wait for the process to terminate
p.WaitForExit
Return output
End Function
Syntax for clicking once whereever the mouse is:
HotKeySet("^!a", "clicker")
Sleep(30000) ;thirty second sleep
Func clicker()
$pos = MouseGetPos()
MouseClick("left", $pos[0], $pos[1], 1)
EndFunc
Simulating mouse clicks (note that this can be used for games such as ragnarok):
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Integer, ByVal dX As Integer, ByVal dY As Integer, ByVal dwData As Integer, ByVal dwExtraInfo As Integer)
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer)
Public Shared Sub MouseClick(ByVal Button As MouseButtons, Optional ByVal TimeBetweenDownAndUp As Integer = 250)
' Flags that are used for mouse_event
Const MOUSEEVENTF_LEFTDOWN As Integer = &H2S ' left button down
Const MOUSEEVENTF_LEFTUP As Integer = &H4S ' left button up
Const MOUSEEVENTF_MIDDLEDOWN As Integer = &H20S ' middle button down
Const MOUSEEVENTF_MIDDLEUP As Integer = &H40S ' middle button up
Const MOUSEEVENTF_MOVE As Integer = &H1S ' mouse move
Const MOUSEEVENTF_RIGHTDOWN As Integer = &H8S ' right button down
Const MOUSEEVENTF_RIGHTUP As Integer = &H10S ' right button up
' Const MOUSEEVENTF_WHEEL As Integer = &H800S ' wheel button rolled
' Const MOUSEEVENTF_ABSOLUTE As Integer = &H8000 ' absolute move
Select Case Button
Case MouseButtons.None, MouseButtons.XButton1, MouseButtons.XButton2
Throw New ArgumentException("Button must be Left, Middle or Right.")
End Select
' Click the mouse, with a delay to simulate human timing
Select Case Button
Case MouseButtons.Left : Call mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Case MouseButtons.Middle : Call mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
Case MouseButtons.Right : Call mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
End Select
If TimeBetweenDownAndUp > 0 Then
Application.DoEvents() ' Allow down position to paint
Call Sleep(TimeBetweenDownAndUp)
End If
Select Case Button
Case MouseButtons.Left : Call mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Case MouseButtons.Middle : Call mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
Case MouseButtons.Right : Call mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
End Select
End Sub
Sample code of using Sendkeys.Send method from System.windows.form class to open calculator:
Public Module SendKeyTest
Private Shell As IwshRuntimeLibrary.WshShell
Public Sub Main()
Shell = New IwshRuntimeLibrary.WshShell()
Shell.Run("calc")
Threading.Thread.Sleep(100)
Shell.AppActivate("Calculator")
SendKeys("101")
SendKeys("*")
SendKeys("2~")
SendKeys("^c") ''copy to clipboard
Console.Write("Answer: ")
Console.WriteLine(Clipboard.GetDataObject().GetData(DataFormats.Text))
Console.ReadLine()
End Sub
Private Sub SendKeys(ByVal key As String)
Shell.SendKeys(key)
Threading.Thread.Sleep(500)
End Sub
End Module
Yes some of these codes can be modified and used to supplement, or create bots in ragnarok or any other app.
AutoIT (for simulating keystrokes)