|
-
Mar 30th, 2011, 09:49 AM
#1
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:
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...
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,...
-
Mar 30th, 2011, 11:20 AM
#2
Fanatic Member
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
-
Mar 30th, 2011, 11:23 AM
#3
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:
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)
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,...
-
Mar 31st, 2011, 11:47 AM
#4
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.
-
Mar 31st, 2011, 10:19 PM
#5
Re: Need some help understanding this advanced code which uses threading, pointers, e
 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 
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,...
-
Apr 1st, 2011, 03:44 AM
#6
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.
-
Apr 1st, 2011, 04:32 AM
#7
Re: Need some help understanding this advanced code which uses threading, pointers, e
 Originally Posted by Lord Orwell
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|