Results 1 to 14 of 14

Thread: [RESOLVED] vb6 remote shutdown app

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Resolved [RESOLVED] vb6 remote shutdown app

    Hi,

    I was looking trough google play and found an app with wich i could remoteley perform shutdown commands on my computer. I had to run a server on the computer wich was provided by the author.

    I wanted to create my own server. The app on my phone sends commands to the server on port 3000 ( default, can be changed ) and i listen on port 3000 with the server.

    Well the problem is that the data comes in good but i cant do anything with it.
    I tryed to put the incoming data in a string and then check it with an if statement but it just wont work. I now have set it up so that the data comes into a listbox and a timer checks the first item for a command. This also wont work. Im getting pretty frustrated about it and i hope somebody can help me.

    My code:

    Code:
    Private Sub c_ConnectionRequest(ByVal requestID As Long)
    'accept connection
    If c.State <> sckClosed Then c.Close
    c.Accept requestID
    End Sub
    
    Private Sub c_DataArrival(ByVal bytesTotal As Long)
    'get data
    Dim henk As String
    c.GetData henk
    List1.AddItem henk
    'the data is added properly to the listbox
    'just need to read it out properly.
    
    End Sub
    
    Private Sub Form_Load()
    c.Listen
    End Sub
    
    Private Sub Form_OLESetData(Data As DataObject, DataFormat As Integer)
    'dont mind this sub, it is to store the if statements temporarely.
    
    If List1.List = "2:300" Then 'shutdown 5 minutes
    Label1.Caption = "Shutdown 5 minutes"
    End If
    
    If List1.List = "2:900" Then 'shutdown 15 minutes
    Label1.Caption = "Shutdown 15 minutes"
    End If
    
    If List1.List = "2:1800" Then 'shutdown 30 minutes
    Label1.Caption = "Shutdown 30 minutes"
    End If
    
    If List1.List = "2:3600" Then 'shutdown 1 hour
    Label1.Caption = "Shutdown 1 hour"
    End If
    
    If List1.List = "1:0" Then 'reboot
    Label1.Caption = "Reboot"
    End If
    
    If List1.List = "3:0" Then 'lock
    Label1.Caption = "Lock"
    End If
    
    If List1.List = "5:0" Then 'standby
    Label1.Caption = "Standby"
    End If
     
    If List1.List = "6:0" Then 'logoff
    Label1.Caption = "LogOff"
    End If
    End Sub
    
    Private Sub Timer1_Timer()
    'timer to check if there is a connection with the client
    If Not c.State = 7 Then
    Form1.Caption = "No connection!"
    Else
    Form1.Caption = "Connection established"
    End If
    End Sub
    
    Private Sub Timer2_Timer()
    'this is the timer that checks the listbox
    If List1.ListCount = 0 Then
    Exit Sub
    End If
    
    If List1.List(1) = "2:0" Then 'shutdown
    Label1.Caption = "Shutdown"
    MsgBox "shutdown"
    List1.RemoveItem 0
    Else
    List1.RemoveItem 0
    End If
    
    End Sub
    Thanks in advance,

    Jason8100

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: vb6 remote shutdown app

    Can't tell much about it, don't see any code to actually attempt a shutdown or anything other than display a message box or label. Also wondering why you are referencing List(1) in the timer instead of List(0). Looks like this could very well cause a runtime error.

    Aside from that you have not even hinted as to what you mean by "won't work" so it is hard to say what your problem may be.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Re: vb6 remote shutdown app

    Quote Originally Posted by DataMiser View Post
    Can't tell much about it, don't see any code to actually attempt a shutdown or anything other than display a message box or label. Also wondering why you are referencing List(1) in the timer instead of List(0). Looks like this could very well cause a runtime error.

    Aside from that you have not even hinted as to what you mean by "won't work" so it is hard to say what your problem may be.
    Well the reason I don´t have any code for shutdown and such is because I first want to get the communication working before even adding that code.

    The problem is that when i try to read the data from the client it just gives me a blank string.

  4. #4
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: vb6 remote shutdown app

    In the comments of your code you say the data is being added to the list
    Code:
    Private Sub c_ConnectionRequest(ByVal requestID As Long)
    'accept connection
    If c.State <> sckClosed Then c.Close
    c.Accept requestID
    End Sub
    
    Private Sub c_DataArrival(ByVal bytesTotal As Long)
    'get data
    Dim henk As String
    c.GetData henk
    List1.AddItem henk
    'the data is added properly to the listbox
    'just need to read it out properly.
    
    End Sub
    But above you say when you read from the client you just get a blank string. Both of these can not be true. So if you would like some help take some time to explain what is happening and what is not working as expected. Currently there is no way for us to tell what you are getting.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Re: vb6 remote shutdown app

    Quote Originally Posted by DataMiser View Post
    In the comments of your code you say the data is being added to the list
    Code:
    Private Sub c_ConnectionRequest(ByVal requestID As Long)
    'accept connection
    If c.State <> sckClosed Then c.Close
    c.Accept requestID
    End Sub
    
    Private Sub c_DataArrival(ByVal bytesTotal As Long)
    'get data
    Dim henk As String
    c.GetData henk
    List1.AddItem henk
    'the data is added properly to the listbox
    'just need to read it out properly.
    
    End Sub
    But above you say when you read from the client you just get a blank string. Both of these can not be true. So if you would like some help take some time to explain what is happening and what is not working as expected. Currently there is no way for us to tell what you are getting.
    Well, the data is added properly to the listbox, so what i want to achieve is that i can read the first item in the listbox and then compare it if it contains one of the commands. My code apperently doesnt work so maybe u can help me.

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: vb6 remote shutdown app

    The first item in the listbox is List(0) not 1

    Here you are testing Item(1) if there are more than 0 items in the list then removing item 0
    Code:
    If List1.List(1) = "2:0" Then 'shutdown
    Label1.Caption = "Shutdown"
    MsgBox "shutdown"
    List1.RemoveItem 0
    This should generate a runtime error when listcount=1 as there would be no item 1 only item 0

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Re: vb6 remote shutdown app

    There was no runtime error because i had an if statement checking for an empty list and exiting the sub when so. I fixed it anyway and it still doesn&#180;t see the "2:0" in the listbox when checking for it. Although i can see it being added to the listbox when i send the command. Its like vb6 is pretending its not there.

    Maybe i am doing something else wrong.

    How would you write a code for a timer to check a listbox for a string and remove the item if it doesnt match the string?

    Thanks in advance,

    Jason8100

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: vb6 remote shutdown app

    Sure enough no runtime error. I would expect the call to list1.list(1) to cause and out of bounds error when there is only one item in the list but it did not return an error when I tested it. Still when you test List1.List(1) you are actually testing the second item in the list. They start at 0.

    Here is a simple sample that will add 4 items to a list and remove them using a timer at the rate of 1/2 second then display a message box when the first item is called "2:0"

    Code:
    Option Explicit
    
    Private Sub Form_Load()
    List1.AddItem "Item1"
    List1.AddItem "Item2"
    List1.AddItem "Item3"
    List1.AddItem "2:0"
    Timer1.Interval = 500
    
    Timer1.Enabled = True
    End Sub
    
    
    Private Sub Timer1_Timer()
    If List1.ListCount > 0 Then
        If List1.List(0) = "2:0" Then
            MsgBox "Got " & List1.List(0)
            List1.RemoveItem 0
        Else
            List1.RemoveItem 0
        End If
    End If
    
    End Sub
    If you have corrected your code to test the value at list(0) and you see the 2:0 displayed but not recognized by the code then best guess is that there is one or more spaces or other invisible character in the mix.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Re: vb6 remote shutdown app

    I tested your code, in a new project, it worked like a charm, in my project it still doesnt 'see' the "2:0"... Am i the only one that thinks this is very strange?

    I added: option explicit to the code, im not sure what this is but it didnt help.

    Anyways, thanks for your time and help. I hope u can still help me.

    Jason8100

  10. #10
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    Re: vb6 remote shutdown app

    Remember that TCP is not a message-based protocol.

    A SendData just queues data to be sent in dribs and drabs and a GetData returns whatever is currently available:
    Code:
      SendData    Sent         Receive Buffers    GetData
      ----------- ------------ ------------------ ----------
    T ABC
    i MORE        ABCMORE
    m                          A
    e                          ABC
                               ABCMO              ABCMO
    |                          RE
    | AND-MORE    AND-MOE
    V                          ERAND-MO
                  RE
                               ERAND-MORE         ERAND-MORE
    This is why the sender needs to apply a framing protocol that the recevier uses to accumulate data and break out messages for processing.

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: vb6 remote shutdown app

    If you are getting the 2:0 displayed on a single line of the list box then you should be able to match it. If it is there and there is no match then most likely there are leading or trailing spaces or possibly a carriage return and/or linefeed. Hard to say without knowing what is being sent but your code is looking for an exact match so any extra characters would cause it to fail.

    Option Explicit simply forces you to declare all variables. This can be set as a default in the VB IDE and should always be used in every project. It makes you develop the habit of defining the variables and alerts you if you have missspelled one.

    Also as stated above it is possible to get data in ways other than you may expect. I always add a character to seperate values and/or records as well as end of string marker so that I can acurately tell when I have a complete transaction.

    Try this
    Code:
    If List1.List(0) = "2:0" Then 'shutdown
       Label1.Caption = "Shutdown"
       MsgBox "shutdown"
       List1.RemoveItem 0
    Else
       Debug.Print Chr$(34) & List1.List(0) & chr$(34)
       List1.RemoveItem 0
    End If
    This should print all non maching enteries in the immeadiate window in quotes so you can see if there are any spaces or other character which would otherwise not be noticable. You should see exactly what you see if the list but with a " on either side.
    Last edited by DataMiser; May 19th, 2012 at 09:04 PM.

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Re: vb6 remote shutdown app

    I tested your code and this was the debug output
    Code:
    ""
    ""
    "7
    "
    "7
    "
    "7
    "
    "7
    "
    "7
    ""7
    "
    "7"7
    "
    "7
    "
    
    "
    
    "2:0
    "
    "7
    "
    "7
    "
    "7
    "
    As you can see there is an extra line coming with the data. How do i put this in a if statement? vbcrlf and vbnewline both wont work.

    Jason8100
    Last edited by jason8100; May 20th, 2012 at 08:37 AM.

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: vb6 remote shutdown app

    Depends if you are getting a carriage return and linefeed then you could use

    Code:
    If List1.List(0) = "2:0" & VBCRLF Then 'shutdown
    If it is just a linefeed then
    Code:
    If List1.List(0)="2:0" & VBLF then
    Or if it is a carriage return
    Code:
    If List1.List(0)="2:0" & VBCR then
    Normally I would parse the incoming data before adding to the list checking to make sure I have gotten the CR or whatever the line terminator is then I would remove those from the data, check to make sure what is left is not an empty string then add the string to the list without the line terminators. It is a bit more envolved but is the proper way to handle data arrival. One of the senarios above should work in your case but you may want to consider adding proper parsing especially if there is going to be much data coming in.
    Last edited by DataMiser; May 20th, 2012 at 08:28 AM.

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Dec 2009
    Posts
    27

    Re: vb6 remote shutdown app

    Vblf did it for me, thank you very much for your time, help and patience!!

    Jason8100

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width