Results 1 to 20 of 20

Thread: Restricting Websites

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Texas
    Posts
    140

    Question Restricting Websites

    I am making a website restriction app and I want to be able to test whether Netscape or IE are going to a website and compare the site with those in a list. If it matches in the list I want to redirect the browser to a restriction page. How can I accomplish this?

  2. #2
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    This is a fairly crude method and it only works for IE (you could easily adapt it to work for Netscape too), but it does work:

    In a Module:
    Code:
    Option Explicit
    
    Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const WM_GETTEXT = &HD
    Private Const WM_SETTEXT = &HC
    Private Const WM_KEYDOWN = &H100
    Private Const WM_KEYUP = &H101
    
    Private aHwnds() As Long
    Private lHwnds As Long
    
    Public Sub CheckURLs()
        Dim lIndex As Long
        Dim lHwnd As Long
        Dim sURL As String
        
        ' The URL to monitor for, this can be adapted to reference an array or flat file list of multiple URLs
        Const BADURL = "www.microsoft.com"
        
        ' The URL to redirect the "BAD" URLs too, i.e. a Notice saying "This is restricted" or whatever.
        Const REDIRECT_URL = "C:\Denied.htm"
        
        ' Reset the Browser count
        lHwnds = 0
        
        ' Enumerate all Top Level windows, looking for IE Browsers, returning a list in the aHwnds() array.
        Call EnumWindows(AddressOf EnumWindowsProc, 0)
        
        ' If Browsers were found, check each one.
        If lHwnds Then
            For lIndex = 0 To lHwnds - 1
                ' Next, Find the Navigation (Address) Box in the Browser
                lHwnd = FindIEEditbox(aHwnds(lIndex))
                If lHwnd Then
                    ' If it was found, extract the current URL
                    sURL = Space(255)
                    sURL = Left(sURL, SendMessage(lHwnd, WM_GETTEXT, 255, ByVal sURL))
                    ' If the URL matches one that's been restricted, redirect the page.
                    If Left(Replace(LCase(sURL), "http://", ""), Len(BADURL)) = LCase(BADURL) Then
                        'Restrict Access to this URL
                        Call SendMessage(lHwnd, WM_SETTEXT, Len(REDIRECT_URL), ByVal REDIRECT_URL)
                        Call SendMessage(lHwnd, WM_KEYDOWN, vbKeyReturn, ByVal 0&)
                        Call SendMessage(lHwnd, WM_KEYUP, vbKeyReturn, ByVal 0&)
                    End If
                End If
            Next
        End If
    End Sub
    
    Private Function EnumWindowsProc(ByVal hWnd As Long, ByVal lParam As Long) As Long
        Dim sClass As String
        
        ' Check the Class of each Window Enumerated looking for IE Browser Windows (IEFrame)
        sClass = Space(255)
        sClass = Left(sClass, GetClassName(hWnd, ByVal sClass, 255))
        If LCase(sClass) = "ieframe" Then
          ' When one is found add it to an array
          ReDim Preserve aHwnds(lHwnds)
          aHwnds(lHwnds) = hWnd
          lHwnds = lHwnds + 1
        End If
        EnumWindowsProc = hWnd
    End Function
    
    Function FindIEEditbox(ByVal hWnd As Long) As Long
        Dim lHwnd As Long
        
        ' Drill down the specified browsers controls until we find the Editbox (Address Entry)
        lHwnd = FindWindowEx(hWnd, 0, "WorkerA", vbNullString)
        If lHwnd Then
            lHwnd = FindWindowEx(lHwnd, 0, "ReBarWindow32", vbNullString)
            If lHwnd Then
                lHwnd = FindWindowEx(lHwnd, 0, "ComboBoxEx32", vbNullString)
                If lHwnd Then
                    lHwnd = FindWindowEx(lHwnd, 0, "ComboBox", vbNullString)
                    If lHwnd Then
                        lHwnd = FindWindowEx(lHwnd, 0, "Edit", vbNullString)
                    End If
                End If
            End If
        End If
        FindIEEditbox = lHwnd
    End Function
    The just stick a call to CheckURLs() in a Timer event or however you want to call it.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Texas
    Posts
    140

    Unhappy Crude?

    When you say crude, what do you mean - will it not work %100 of the time? I will try this method by the way and thank you very much for the help. Also, how can it be adapted to Netscape or Opera or something? What if the user types in just news.com or www.news.com or http://www.news.com, will it still work?

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    I only say it's a bit crude, because I'm sure there are better ways to do it, but it should work 99% of the time (will never commit to that extra 1% - lol)
    You can adapt it to work with Netscape or other browsers by changing what Classes you need to check for instead of "IEFrame" and the controls leading to the Addressbar Edit box.

    If the user types in news.com the browser will resolve this automatically to www.news.com at which point it will trigger, alternatively you can change the checking code to strip out the leading "www" and compare only the Domain.

    I am already stripping out the "http://" section if it exists so this isn't an issue.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Texas
    Posts
    140

    Wink So...

    Is there any event for when a website is gone too? I don't have a clue what event to use to check the address of whether it's bad or not. I could use timer to constantly check under a timer event, but that would eat resources, any ideas?

  6. #6
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    I know, why don't I write the app, then you can sell it...

    I could see no noticable Resource usage by this process when using a Timer, it's pretty uninvasive.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Texas
    Posts
    140

    Talking Cool

    Having a timer constantly checking whether or not a website is good doesn't eat resources? I jsut thought it would be more conservative to check only when a website adress is enter and sent to the computer (it's just a little bulky to check even when IE isn't even open. I would be glad to sell it, lol. That's actually my intended goal, this is part one of 3 for this program.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Location
    Texas
    Posts
    140

    Smile One last thing man...

    I have this code:

    Private Sub tmrTimer_Timer()
    CheckURLs ("www.news.com")
    End Sub

    That works with the slightly modified code (only modified at the top):
    Public Sub CheckURLs(strWebsite)
    Dim lIndex As Long
    Dim lHwnd As Long
    Dim sURL As String
    Dim strBadUrl As String
    Dim strRedirectUrl As String

    strBadUrl = strWebsite

    ' The URL to redirect the "BAD" URLs too, i.e. a Notice saying "This is restricted" or whatever.
    strRedirectUrl = App.Path & "\" & "Restricted.html"

    ' Reset the Browser count
    lHwnds = 0

    ' Enumerate all Top Level windows, looking for IE Browsers, returning a list in the aHwnds() array.
    Call EnumWindows(AddressOf EnumWindowsProc, 0)

    ' If Browsers were found, check each one.
    If lHwnds Then
    For lIndex = 0 To lHwnds - 1
    ' Next, Find the Navigation (Address) Box in the Browser
    lHwnd = FindIEEditbox(aHwnds(lIndex))
    If lHwnd Then
    ' If it was found, extract the current URL
    sURL = Space(255)
    sURL = Left(sURL, SendMessage(lHwnd, WM_GETTEXT, 255, ByVal sURL))
    ' If the URL matches one that's been restricted, redirect the page.
    If Left(Replace(LCase(sURL), "http://", ""), Len(strBadUrl)) = LCase(strBadUrl) Then
    'Restrict Access to this URL
    Call SendMessage(lHwnd, WM_SETTEXT, Len(strRedirectUrl), ByVal strRedirectUrl)
    Call SendMessage(lHwnd, WM_KEYDOWN, vbKeyReturn, ByVal 0&)
    Call SendMessage(lHwnd, WM_KEYUP, vbKeyReturn, ByVal 0&)
    End If
    End If
    Next
    End If
    End Sub


    As part of your module. The problem is, how can I detect whatever site is entered instead of just news.com, I want to see what URL they type in and grab it and enter it into CheckURLs, can you help?

  9. #9
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    sURL contains the full URL entered.

  10. #10
    Lively Member
    Join Date
    Aug 2007
    Posts
    91

    Re: Restricting Websites

    Quote Originally Posted by Aaron Young
    sURL contains the full URL entered.
    sir,
    i gone through your code and its just fendastic.
    but i have one question to ask you sir.
    can you tell me how did you know that which control you have to drill down through to find your edit box??

    i mean to say that how did you know that you need to do
    lHwnd = FindWindowEx(hWnd, 0, "WorkerA", vbNullString) this first

    Then this
    lHwnd = FindWindowEx(lHwnd, 0, "ReBarWindow32", vbNullString)
    Then this
    lHwnd = FindWindowEx(lHwnd, 0, "ComboBoxEx32", vbNullString)
    Then this
    lHwnd = FindWindowEx(lHwnd, 0, "ComboBox", vbNullString)
    Then this
    lHwnd = FindWindowEx(lHwnd, 0, "Edit", vbNullString)
    this all call are for IE but how can i find these sequence for Mozilla or other web browser?

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Restricting Websites

    Visual Studio 6 comes with a tool called Spy++ which is very helpful in doing such things


    Has someone helped you? Then you can Rate their helpful post.

  12. #12
    Lively Member
    Join Date
    Aug 2007
    Posts
    91

    Re: Restricting Websites

    hi
    thank so much.
    i tried above code with IE and it work for me.
    now i want to do same thing for mozilla.i did use spy++ to find class tree so that and i send message to appropriate edit box (url textbox)and that tree is as follow.





    now in this tree everything is Mozilla class .nothign in it tell me about which is edit box for url.
    i use RanorexSpy to see what is class for url textbox. but according to RanorexSpy url textbox is not a control ,it is element in it.here is screenshot of that






    please do tell me what to do to get text withing url editbox.

  13. #13
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Restricting Websites

    It looks to me that what you are looking for is the first (and only) Child window of the root class. If you use FindWindowEx specifying the handle of the root class and nothing for the other parameters it will return the handle of the first child which is the one you want.

  14. #14
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Restricting Websites

    The class is MozillaUIWindowClass.
    Ok but you have to handle it's url textbox, which is ???

  15. #15
    Lively Member
    Join Date
    Aug 2007
    Posts
    91

    Re: Restricting Websites

    Quote Originally Posted by sapator
    The class is MozillaUIWindowClass.
    Ok but you have to handle it's url textbox, which is ???
    i didnt understan what you wanted to say.can you tell me in detail???????

  16. #16
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Restricting Websites

    Using the HOSTS file might not be the best idea, but atleast this trick works with any browser or internet enabled application.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  17. #17
    Fanatic Member
    Join Date
    Oct 2004
    Posts
    751

    Re: Restricting Websites

    I believe you are going to need a little more than just elbow grease to pull this off. Research up on firewalls and how they monitor and intercept data. Because if you are just going to check against the URL in a specified browser, whats stopping me from making my own browser in VB6 and using that to surf?
    Or even using lynx
    My Projects: [ Instant Messagener Client/Server ] [ VBPictochat ]

    My Sites:
    [ Datanethost ]
    [ Helpdesk ]

    Remember if my post was helpful then Rate This Post.

  18. #18
    Lively Member
    Join Date
    Aug 2007
    Posts
    91

    Re: Restricting Websites

    Quote Originally Posted by iPrank
    Using the HOSTS file might not be the best idea, but atleast this trick works with any browser or internet enabled application.
    thank you so much such for your guidance and this is very nice solution.but my intention behind this is how to peek in to other application.means how to fetch data from the other application(like mozilla or other windows application).and i know that it is possible coz every thing in windows is just a class and it is possible with windows api.but i need little guidance from you guy. any book name or article which can help me out.
    so any kind of help in that direction?????
    Last edited by bhavin12300; Apr 27th, 2008 at 03:20 AM.

  19. #19
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Restricting Websites

    Hi.
    You can try to use this API and then handle the id of the application to get the class names.

    Private Shared Sub GetClassName(ByVal hWnd As System.IntPtr, _
    ByVal lpClassName As System.Text.StringBuilder, ByVal nMaxCount As Integer)



    Originally Posted by sapator
    The class is MozillaUIWindowClass.
    Ok but you have to handle it's url textbox, which is ???
    i didnt understan what you wanted to say.can you tell me in detail???????
    I thought that the url textbox of mozilla has a name so you can use it in sendmessage like internet explorer but apparently it hasn't.

  20. #20
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,729

    Re: Restricting Websites

    Quote Originally Posted by bhavin12300
    but my intention behind this is how to peek in to other application.
    Firewalls do that, but I have no idea how to achive this. If you find any open source firewall, that might help. But I think C++ would me better choice for this.

    how to fetch data from the other application(like mozilla or other windows application).and i know that it is possible coz every thing in windows is just a class and it is possible with windows api.
    A app may not have any windows at all or it might be a service or a spyware.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


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