Need some help understanding this advanced code which uses threading, pointers, etc..
Hi guys..:wave:
I had recently(2 months before :D) 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:
Public Function LockSystemAndShow(ByVal myDForm As Form) As Boolean
myForm = myDForm
'Dim owner As Control = TryCast(Form1.Button1, Control)
Dim scr As Screen
'If owner Is Nothing Then
scr = Screen.PrimaryScreen
'Else
'scr = Screen.FromControl(owner)
'End If
Dim background As Bitmap = New Bitmap(scr.Bounds.Width, scr.Bounds.Height)
Using g As Graphics = Graphics.FromImage(background)
g.CopyFromScreen(0, 0, 0, 0, scr.Bounds.Size)
Using br As Brush = New SolidBrush(Color.FromArgb(192, Color.Black))
g.FillRectangle(br, scr.Bounds)
End Using
'If owner IsNot Nothing Then
' Dim form As Form = owner.FindForm()
' g.CopyFromScreen(form.Location, form.Location, form.Size)
' Using br As Brush = New SolidBrush(Color.FromArgb(128, Color.Black))
' g.FillRectangle(br, New Rectangle(Form.Location, Form.Size))
'End Using
'End If
End Using
Dim originalThread As IntPtr
Dim originalInput As IntPtr
Dim newDesktop As IntPtr
originalThread = GetThreadDesktop(Thread.CurrentThread.ManagedThreadId)
originalInput = OpenInputDesktop(0, False, DESKTOP_SWITCHDESKTOP)
newDesktop = CreateDesktop("Desktop" & Guid.NewGuid().ToString(), Nothing, Nothing, 0, GENERIC_ALL, Nothing)
SetThreadDesktop(newDesktop)
SwitchDesktop(newDesktop)
Dim newThread As Thread = New Thread(AddressOf NewThreadMethod)
newThread.CurrentCulture = Thread.CurrentThread.CurrentCulture
newThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture
newThread.Start(New abcLockSystemParameters(newDesktop, background))
newThread.Join()
SwitchDesktop(originalInput)
SetThreadDesktop(originalThread)
CloseDesktop(newDesktop)
CloseDesktop(originalInput)
Return True 'Result
End Function
Private Sub NewThreadMethod(ByVal params As Object)
Dim v As abcLockSystemParameters = DirectCast(params, abcLockSystemParameters)
SetThreadDesktop(v.NewDesktop)
Using f As Form = New BackgroundForm(v.Background)
f.Show()
myForm.ShowDialog() '~~~~ Show the login screen here
f.BackgroundImage = Nothing
Application.DoEvents()
Thread.Sleep(250)
End Using
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...:wave:
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?
Re: Need some help understanding this advanced code which uses threading, pointers, e
Hi.. :wave:
Thanks for the reply.
I'm having trouble understanding what it does in this part:
vb.net Code:
originalThread = GetThreadDesktop(Thread.CurrentThread.ManagedThreadId)
originalInput = OpenInputDesktop(0, False, DESKTOP_SWITCHDESKTOP)
newDesktop = CreateDesktop("Desktop" & Guid.NewGuid().ToString(), Nothing, Nothing, 0, GENERIC_ALL, Nothing)
SetThreadDesktop(newDesktop)
SwitchDesktop(newDesktop)
Dim newThread As Thread = New Thread(AddressOf NewThreadMethod)
newThread.CurrentCulture = Thread.CurrentThread.CurrentCulture
newThread.CurrentUICulture = Thread.CurrentThread.CurrentUICulture
newThread.Start(New abcLockSystemParameters(newDesktop, background))
newThread.Join()
SwitchDesktop(originalInput)
SetThreadDesktop(originalThread)
CloseDesktop(newDesktop)
CloseDesktop(originalInput)
:wave:
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.
Re: Need some help understanding this advanced code which uses threading, pointers, e
Quote:
Originally Posted by
Lord Orwell
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 :wave:
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.
:wave:
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.
Re: Need some help understanding this advanced code which uses threading, pointers, e
Quote:
Originally Posted by
Lord Orwell
it should be fine, but if your code ever crashes you'll have to reboot.
Ok.. Thanks :wave: