Ok, so I'm new to VB.NET and trying to write a program that prompts the user for a server name and then restarts the IIS on that machine.
Problem 1) namespace System.ServiceProcess is not being recognized.
Problem 2) need help with code, passing servername into sub.
Imports System
Imports System.ServiceProcess
Imports System.IO
Imports System.Threading
Class RestartIIS
Shared Sub Main()
Run()
End Sub
Public Sub Run()
Console.WriteLine("Please enter the Server Name: ")
Dim ServerName As String = Console.ReadLine()
Dim sc As ServiceController = New ServiceController("W3SVC")
sc.Stop()
Thread.Sleep(2000)
sc.Start()
Console.Write("Press Enter to Exit")
Console.ReadLine()
End Sub
End Class
You should add a reference to System.ServiceProcess assembly by right clicking the project and clicking Add Reference... and get command line arguments passed to the Main method like this:
Imports System
Imports System.ServiceProcess
Imports System.IO
Imports System.Threading
Class RestartIIS
Shared Sub Main(ByVal commandLineArgs() as String)
Run(commandLineArgs(0))
End Sub
Public Sub Run(ByVal machineName as String)
Console.WriteLine("Please enter the Server Name: ")
Dim ServerName As String = Console.ReadLine()
Dim sc As ServiceController = New ServiceController("W3SVC", machineName)
sc.Stop()
Thread.Sleep(2000)
sc.Start()
Console.Write("Press Enter to Exit")
Console.ReadLine()
End Sub
End Class
ServiceControl.Stop()
Do
ServiceControl.Refresh()
If ServiceControl.Status = ServiceControllerStatus.Stopped Then
ServiceControl.Start()
Exit Do
End If
Loop
Related
Here's my code:
Imports Microsoft.Owin.Hosting
Imports System
Module Program
Sub Main(args As String())
Const url As String = "http://localhost:9001"
Using WebApp.Start(url)
Console.WriteLine("Server started at:" & url)
Console.ReadLine()
End Using
End Sub
End Module
The exception occurs
Your syntax for the Using statement is wrong. It is supposed to be similar to a variable declaration:
Imports Microsoft.Owin.Hosting
Imports System
Module Program
Sub Main(args As String())
Const url As String = "http://localhost:9001"
Using wa As New WebApp
wa.Start(url)
Console.WriteLine("Server started at:" & url)
Console.ReadLine()
End Using
End Sub
End Module
More information is available here.
I installed the libraries xUnit with NuGet, and added references but I have errors because of the attributes [Theory], [InlineData("11/12/2011","2011-11-12")] and [Fact].
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Linq
Imports System.Text.RegularExpressions
Imports FluentAssertions
Imports Xunit
Imports System
Imports System.Diagnostics
Imports Xunit.Extensions
Public Class XUnitClassTest
[Theory]
[InlineData("11/12/2011","2011-11-12")]
Public Sub test(input As String, output As String)
Dim pattern As String = "\d+|[A-Za-zÀàÂâÄäÇçÉéÈèÊêËëÎîÏïÔôÖöÙùÛûÜü']+"
Dim matchList As MatchCollection = Regex.Matches(input, pattern)
Dim matchArray(matchList.Count - 1) As Match
matchList.CopyTo(matchArray, 0)
Dim manager As Processeur = New Processeur
manager.GetData(matchArray.Select(Function(a) a.ToString())).Should().Be(output)
End Sub
[Fact]
Public Sub FactMethodName()
Write(DateTime.Parse("1658").ToString())
End Sub
Public Shared Sub Write(format As String, ParamArray param As Object())
Console.WriteLine(format, param)
End Sub
End Class
You are using C# attribute syntax in VB.NET.
VB.NET syntax would be
<Fact>
Public Sub FactMethodName()
Write(DateTime.Parse("1658").ToString())
End Sub
and so on.
There is possibly a few things wrong with the below. Haven't had a chance to test/debug the code yet as can't run it. It stating that no main method has been found. But there is? i've even changed it to shared etc. It's probably something obvious?
It's flagging - 'Sub Main' was not found in 'ConsoleApplication1.Module1' error.
Also the main method wasn't always a separate class, I was just trying stuff. I'm importing a reference - system.processes. Was initially created as a vb.form but realised i didn't want the form part and recreated as a console app (which is very possibly where the problem lies as it's one of the first console apps i've done).
Code is basically planned to act on a service dying. Report and try and manage the restart (not finished, ideas welcome).
Imports System
Imports System.Management
Imports System.ServiceProcess
Imports System.Diagnostics
Imports System.Threading
Imports System.IO
Module Module1
Public Class Control
Public Sub Main() 'Public Sub Main(ByVal sArgs() As String)
Dim restart As New Rest
restart.startTime = DateTime.Now()
restart.cr20Services()
restart.Report()
End Sub
End Class
Public Class Rest
public startTime As String
Dim logPath As String = "C:\cr20\restart.txt"
'Dim fileExists As Boolean = File.Exists(strFile)
Dim arrcr20ServicesInitialStatus As New ArrayList
Dim failedServices As New ArrayList
Dim arrcr20Services As New ArrayList
Public Sub cr20Services()
'cr20 Services
arrcr20Services.Add("cr20 service")
arrcr20Services.Add("cr20 router")
For Each cr20Service In arrcr20Services
arrcr20ServicesInitialStatus.Add(cr20Service & " - " & cr20Status(cr20Service))
cr20Restore(cr20Service)
Next
End Sub
Private Function cr20Status(ByVal cr20Service As String)
Dim service As ServiceController = New ServiceController(cr20Service)
Return service.Status.ToString
End Function
Private Sub cr20Restore(ByVal cr20Service As String)
Dim service As ServiceController = New ServiceController(cr20Service)
'Dim p() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("calc")
If (service.Status.Equals(ServiceControllerStatus.Stopped)) Or (service.Status.Equals(ServiceControllerStatus.StopPending)) Then
failedServices.Add(service)
service.Stop()
Thread.Sleep(10000) 'give service 10 seconds to stop
service.Start()
End If
End Sub
I made a "custom" form as seen below. When I say:
Dim nSplash As New frmSplash(nBitmap)
It is telling me that there are "too many arguments for Public Sub New".
I do not see why it is mocking about it.
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Namespace AlphaWindow
Public Class frmSplash
Inherits Form
Public Sub New(ByRef uBitmap As Bitmap)
Me.Size = uBitmap.Size
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
APIHelp.ShowTopmost(Me)
Me.SelectBitmap(uBitmap)
End Sub
(...)
' Class to assist with Win32 API calls
Class APIHelp
Private Const SW_SHOWNOACTIVATE As Integer = 4
Private Const HWND_TOPMOST As Integer = -1
(...)
End Class
End Namespace
The problem is not the Namespace, but when a namespace is included, the form has to be called by "Namespace.Form" instead of just "Form".
I have been trying to build a game bot in vb.net. One of the main problems is getting access to the text the game prints on the screen so I have been trying to hook the games calls to the windows api drawtext and textout functions. I have been hunting for examples of how to set up a hook in vb.net for a long time without any luck. I have found it impossible to translate examples in old school vb, C++, and C#. For convenience's sake I would like to use the freely available deviare and/or easyhook libraries. Can anyone help?
I found working vb.net code based on the deviare hooking dlls buried in the deviare forums.
please remember to add all 6 deviare references found under the com tab of the add references page of visual studio after installing deviare.
Public Class Form1
'To print to a textbox in the gui you will have to call textbox.invoke
Private _mgr As Deviare.SpyMgr
Private WithEvents _hook As Deviare.Hook
Private _proc As DeviareTools.IProcess
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_mgr = New Deviare.SpyMgr()
_hook = _mgr.CreateHook("user32.dll!ShowWindow")
_hook.Attach(_mgr.Processes)
_hook.AddProcessFilter(0, "notepad", 1)
_hook.Hook()
Private Sub OnFunctionCalled(ByVal proc As DeviareTools.IProcess, ByVal callInfo As DeviareParams.ICallInfo, ByVal rCall As Deviare.IRemoteCall) Handles _hook.OnFunctionCalled
Debug.Print("Caught function call in " & proc.Name) 'view in imediate window
End Sub
end class
I am in the process of converting the Easyhook c# code into vb.net. At the moment I am getting the error code 5 message and the hooked application is being closed by windows eg to help protect your computer etc. If anyone could help with this then I would be grateful. What I have so far is...
Imports EasyHook
Imports System
Imports System.Diagnostics
Imports System.Runtime.Remoting
Imports System.Windows.Forms
Imports System.Security
Imports System.Security.Principal
Namespace FileMon
Friend Class Program
' Methods
Public Shared Sub Main(ByVal args As String())
Dim result As Integer = 0
If ((args.Length <> 1) OrElse Not Integer.TryParse(args(0), result)) Then
Console.WriteLine()
Console.WriteLine("Usage: FileMon %PID%")
Console.WriteLine()
Else
Try
Try
Console.WriteLine("Registering Application")
Console.WriteLine()
Config.Register("A FileMon like demo application.", "FileMon.exe", "FileMonInject.dll")
Catch exception1 As ApplicationException
Console.WriteLine("This is an administrative task! " & exception1.ToString)
Console.WriteLine()
Process.GetCurrentProcess.Kill()
End Try
Console.WriteLine("Creating IPC Server")
Console.WriteLine()
RemoteHooking.IpcCreateServer(Of FileMonInterface)(Program.ChannelName, WellKnownObjectMode.SingleCall)
RemoteHooking.Inject(result, "FileMonInject.dll", "FileMonInject.dll", New Object() {Program.ChannelName})
Console.WriteLine("Injected")
Console.WriteLine()
Console.ReadLine()
Catch exception As Exception
Console.WriteLine("There was an error while connecting to target:" & exception.ToString)
End Try
End If
End Sub
' Fields
Private Shared ChannelName As String
End Class
End Namespace
and
Imports System
Namespace FileMon
Public Class FileMonInterface
Inherits MarshalByRefObject
' Methods
Public Sub IsInstalled(ByVal InClientPID As Integer)
Console.WriteLine("FileMon has been installed in target " & InClientPID)
End Sub
Public Sub OnCreateFile(ByVal InClientPID As Integer, ByVal InFileNames As String())
Dim i As Integer
For i = 0 To InFileNames.Length - 1
Console.WriteLine(InFileNames(i))
Next i
End Sub
Public Sub Ping()
End Sub
Public Sub ReportException(ByVal InInfo As Exception)
Console.WriteLine(("The target process has reported an error:" & InInfo.ToString))
End Sub
End Class
End Namespace
easyhook libraries
Try to use the EasyHook library