Results 1 to 7 of 7

Thread: Need some help understanding this advanced code which uses threading, pointers, etc..

  1. #1

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Question Need some help understanding this advanced code which uses threading, pointers, etc..

    Hi guys..

    I had recently(2 months before ) found some code for locking the screen. Here's the link to that code submission in our CodeBank : http://www.vbforums.com/showthread.php?t=639579

    I wish to get a clear idea of what it actually does. As it is a bit advanced code for me, I need your help.

    Here's the main part of the code:
    vb.net Code:
    1. Public Function LockSystemAndShow(ByVal myDForm As Form) As Boolean
    2.         myForm = myDForm
    3.  
    4.         'Dim owner As Control = TryCast(Form1.Button1, Control)
    5.         Dim scr As Screen
    6.         'If owner Is Nothing Then
    7.         scr = Screen.PrimaryScreen
    8.         'Else
    9.         'scr = Screen.FromControl(owner)
    10.         'End If
    11.         Dim background As Bitmap = New Bitmap(scr.Bounds.Width, scr.Bounds.Height)
    12.         Using g As Graphics = Graphics.FromImage(background)
    13.  
    14.             g.CopyFromScreen(0, 0, 0, 0, scr.Bounds.Size)
    15.             Using br As Brush = New SolidBrush(Color.FromArgb(192, Color.Black))
    16.                 g.FillRectangle(br, scr.Bounds)
    17.             End Using
    18.  
    19.             'If owner IsNot Nothing Then
    20.             ' Dim form As Form = owner.FindForm()
    21.             ' g.CopyFromScreen(form.Location, form.Location, form.Size)
    22.             ' Using br As Brush = New SolidBrush(Color.FromArgb(128, Color.Black))
    23.             ' g.FillRectangle(br, New Rectangle(Form.Location, Form.Size))
    24.             'End Using
    25.             'End If
    26.  
    27.         End Using
    28.  
    29.         Dim originalThread As IntPtr
    30.         Dim originalInput As IntPtr
    31.         Dim newDesktop As IntPtr
    32.  
    33.         originalThread = GetThreadDesktop(Thread.CurrentThread.ManagedThreadId)
    34.         originalInput = OpenInputDesktop(0, False, DESKTOP_SWITCHDESKTOP)
    35.  
    36.         newDesktop = CreateDesktop("Desktop" & Guid.NewGuid().ToString(), Nothing, Nothing, 0, GENERIC_ALL, Nothing)
    37.         SetThreadDesktop(newDesktop)
    38.         SwitchDesktop(newDesktop)
    39.  
    40.         Dim newThread As Thread = New Thread(AddressOf NewThreadMethod)
    41.         newThread.CurrentCulture = Thread.CurrentThread.CurrentCulture
    42.         newThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture
    43.         newThread.Start(New abcLockSystemParameters(newDesktop, background))
    44.         newThread.Join()
    45.  
    46.         SwitchDesktop(originalInput)
    47.         SetThreadDesktop(originalThread)
    48.  
    49.         CloseDesktop(newDesktop)
    50.         CloseDesktop(originalInput)
    51.  
    52.         Return True 'Result
    53.  
    54.     End Function
    55.  
    56.     Private Sub NewThreadMethod(ByVal params As Object)
    57.         Dim v As abcLockSystemParameters = DirectCast(params, abcLockSystemParameters)
    58.         SetThreadDesktop(v.NewDesktop)
    59.         Using f As Form = New BackgroundForm(v.Background)
    60.             f.Show()
    61.             myForm.ShowDialog()  '~~~~ Show the login screen here
    62.             f.BackgroundImage = Nothing
    63.             Application.DoEvents()
    64.             Thread.Sleep(250)
    65.         End Using
    66.     End Sub
    Looks like pretty easy one, isn't it ? But it's a tough one for me.

    Can you please help me to understand this code ?

    Thanks in advance...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  2. #2
    Fanatic Member
    Join Date
    Aug 2009
    Posts
    540

    Re: Need some help understanding this advanced code which uses threading, pointers, e

    maybe some more pointed questions will help you get answers. What exactly about the above code do you not understand or needs more explanation?
    Where I'm from we only have one bit of advice for new comers: "If you hear banjos, turn and run".


    VS 2008 .NetFW 2.0

  3. #3

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Need some help understanding this advanced code which uses threading, pointers, e

    Hi..

    Thanks for the reply.

    I'm having trouble understanding what it does in this part:
    vb.net Code:
    1. originalThread = GetThreadDesktop(Thread.CurrentThread.ManagedThreadId)
    2.         originalInput = OpenInputDesktop(0, False, DESKTOP_SWITCHDESKTOP)
    3.  
    4.         newDesktop = CreateDesktop("Desktop" & Guid.NewGuid().ToString(), Nothing, Nothing, 0, GENERIC_ALL, Nothing)
    5.         SetThreadDesktop(newDesktop)
    6.         SwitchDesktop(newDesktop)
    7.  
    8.         Dim newThread As Thread = New Thread(AddressOf NewThreadMethod)
    9.         newThread.CurrentCulture = Thread.CurrentThread.CurrentCulture
    10.         newThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture
    11.         newThread.Start(New abcLockSystemParameters(newDesktop, background))
    12.         newThread.Join()
    13.  
    14.         SwitchDesktop(originalInput)
    15.         SetThreadDesktop(originalThread)
    16.  
    17.         CloseDesktop(newDesktop)
    18.         CloseDesktop(originalInput)

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Need some help understanding this advanced code which uses threading, pointers, e

    although you don't get easy access to it, windows actually supports multiple desktops similar to osx and linux. The only thing it's used for by the OS is to create UAC prompts. They take a screen shot of the main desktop and create a new one with that shaded in the background and then show you the new desktop. You can use the same generalities to do the same thing and that is what this code does. It locks you out of the main desktop by showing you a different one.

    however, i don't think that's the entire code. I don't see closedesktop declared anywhere, for example.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  5. #5

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Need some help understanding this advanced code which uses threading, pointers, e

    Quote Originally Posted by Lord Orwell View Post
    although you don't get easy access to it, windows actually supports multiple desktops similar to osx and linux. The only thing it's used for by the OS is to create UAC prompts. They take a screen shot of the main desktop and create a new one with that shaded in the background and then show you the new desktop. You can use the same generalities to do the same thing and that is what this code does. It locks you out of the main desktop by showing you a different one.

    however, i don't think that's the entire code. I don't see closedesktop declared anywhere, for example.
    Thanks
    That means it's perfect to use as a lock screen, right ?

    I haven't posted the entire code in the previous post. You can find the attachment in the other thread.


    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  6. #6
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Need some help understanding this advanced code which uses threading, pointers, e

    it should be fine, but if your code ever crashes you'll have to reboot.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  7. #7

    Thread Starter
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Need some help understanding this advanced code which uses threading, pointers, e

    Quote Originally Posted by Lord Orwell View Post
    it should be fine, but if your code ever crashes you'll have to reboot.
    Ok.. Thanks

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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