2013-07-15 2 views
1

그래서 나는이 오류가 나는 여기에 System.invalidOperationexception 전체 오류가 얻을 말하는 얻을 : 포트에 대한Visual Basic의 System.invalidOperationexception

[Managed to Native Transition] 
Port Scan.exe!WindowsApplication1.My.MyProject.MyForms.Form1.get() 
Port Scan.exe!WindowsApplication1.My.MyApplication.OnCreateMainForm() 
    Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicatio Base.OnRun() 
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplication Base.DoApplicationModel() 
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplication Base.Run(string[] commandLine) 
[Native to Managed Transition] 
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationCo ntext activationContext, string[] activationCustomData) 
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.H ostProc.RunUsersAssemblyDebugInZone() 
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContex t executionContext, System.Threading.ContextCallback callback, object state, bool  preserveSyncCtx) 
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) 
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext  executionContext, System.Threading.ContextCallback callback, object state) 
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() 
[Native to Managed Transition] 

여기에 내가 컴파일하려고 코드가 그것을 스캐너

Public Class Form1 
Dim host As String 

Dim counter As Integer 
Dim portmin As Integer = TextBox3.Text 
Dim portmax As Integer = TextBox2.Text 
Private Sub Form1_Load(ByVal sender As System.Object, _ 
      ByVal e As System.EventArgs) Handles MyBase.Load 
    Button1.Enabled = False 
    'set counter explained before to 0 
    counter = 0 
End Sub 
Private Sub Timer1_Tick(ByVal sender As System.Object, _ 
     ByVal e As System.EventArgs) Handles Timer1.Tick 
    'Set the host and port and counter 
    counter = counter + 1 'counter is for the timer 
    host = TextBox1.Text 



    For port As Integer = portmin To portmax 

     If (port = portmax) Then 
      Exit For 
     End If 



     ' Next part creates a socket to try and connect 
     ' on with the given user information. 

     Dim hostadd As System.Net.IPAddress = _ 
      System.Net.Dns.GetHostEntry(host).AddressList(0) 
     Dim EPhost As New System.Net.IPEndPoint(hostadd, port) 
     Dim s As New System.Net.Sockets.Socket(_ 
     System.Net.Sockets.AddressFamily.InterNetwork, _ 
    System.Net.Sockets.SocketType.Stream, _ 
     System.Net.Sockets.ProtocolType.Tcp) 
     Try 
      s.Connect(EPhost) 
     Catch 
     End Try 
     If Not s.Connected Then 
      ListBox1.Items.Add("Port " + port.ToString + " is not open") 
     Else 
      ListBox1.Items.Add("Port " + port.ToString + " is open") 
      ListBox2.Items.Add(port.ToString) 

     End If 
     Label3.Text = "Open Ports: " + ListBox2.Items.Count.ToString 
    Next 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, _ 
      ByVal e As System.EventArgs) Handles Button1.Click 
    'stop button 
    Timer1.Stop() 
    Timer1.Enabled = False 
    Button1.Enabled = True 
    Button2.Enabled = False 
End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles Button2.Click 
    ListBox1.Items.Add("Scanning: " + TextBox1.Text) 
    ListBox1.Items.Add("-------------------") 
    Button2.Enabled = True 
    Button1.Enabled = False 
    Timer1.Enabled = True 
    Timer1.Start() 
End Sub 

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 

End Sub 

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged 

End Sub 

Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged 

End Sub 
End Class 

나는이 오류에 다소 당황하여 exe를 빌드 할 수 없으므로 정말 도움을 주셔서 감사합니다. 오류가 최소 및 최대 포트 번호를 허용하는 for 루프를 수행 할 때만 발생하는 것 같습니다.

+0

'portmin' ('TextBox3.Text')과'portmax' ('TextBox2.Text')의 값은 무엇입니까? –

답변

4

문제는이 코드에 : 그 문 실행시

Dim portmin As Integer = TextBox3.Text 
Dim portmax As Integer = TextBox2.Text 

는, 형태가 아직 완전히 구성되지 않습니다. TextBox2TextBox3은 아직 생성되지 않았습니다. 정수 값에 Nothing 참조를 지정하려고합니다. 허용되지 않습니다. 할당을 폼의 Load 이벤트로 이동하십시오. 또한 시간을내어 확인하고 해당 상자의 내용이 정수로 변환되는지 확인하십시오.

+0

"Load 이벤트로 옮기십시오"라는 것이 좋은 충고입니다. 사용자는 여전히 값을 입력 할 시간이 없었습니다. 그리고 훨씬 더 실패 모드 인 Load 이벤트 핸들러의 예외는 삼키는 경향이 있습니다. 시작 버튼을 사용하는 것이 가장 좋습니다. –

+0

초고속 답변을 주셔서 감사합니다. – Gabe

+0

@Gabe - 또한 프로젝트 속성으로 이동하여 Option Strict를 On으로 설정하거나 Option Strict를 코드 상단에 둡니다. –