<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bahramov's Personal Blog &#187; Visual Basic</title>
	<atom:link href="http://www.bahramov.com/category/visual-basic/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bahramov.com</link>
	<description>Computers, databases, networks and virtualization</description>
	<lastBuildDate>Fri, 24 Jun 2011 12:05:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Script to fix error with start up of AU and BITS (EventID 7023 and 7024)</title>
		<link>http://www.bahramov.com/2009/06/30/script-to-fix-the-error-with-start-up-of-automatic-updates-and-bits-services-eventid-7023-and-eventid-7024/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/06/30/script-to-fix-the-error-with-start-up-of-automatic-updates-and-bits-services-eventid-7023-and-eventid-7024/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 11:39:13 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[VBScript]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=330</guid>
		<description><![CDATA[This script detects and tries to fix the error related to start up of Automatic Updates and BITS services on network computers. Such error is related to security identifier (EventID 7023 and EventID 7024). Â This problem is usually observed on network computers after applying changes to group policies. In such cases the script will reset [...]]]></description>
			<content:encoded><![CDATA[<p>This script detects and tries to fix the error related to start up of Automatic Updates and BITS services on network computers. Such error is related to security identifier (EventID 7023 and EventID 7024). Â This problem is usually observed on network computers after applying changes to group policies. In such cases the script will reset a security identifier as per Microsoft KB 555336Â (<a href="http://support.microsoft.com/kb/555336">http://support.microsoft.com/kb/555336</a>) and start Automatic Updates and BITS.Â Can be usefull for networks where Windows Server Update Services 3.0 (WSUS) is used.</p>
<p>Script is taken from: <a href="http://forum.vingrad.ru/forum/topic-154708/view-all.html">http://forum.vingrad.ru/forum/topic-154708/view-all.html</a></p>
<p><span id="more-330"></span>
<pre class="brush: vb.net">&#039; This script detects and tries to fix the error related to start up of Automatic Updates and BITS services
&#039; on network computers. Such error is related to security identifier (EventID 7023 and EventID 7024).
&#039; This problem is usually observed on network computers after applying changes to group policies.
&#039; In such cases the script will reset a security identifier as per Microsoft KB 555336
&#039; http://support.microsoft.com/kb/555336 and start Automatic Updates and BITS.
&#039; Can be usefull for networks where Windows Server Update Services 3.0 (WSUS) is used.
&#039; Script source: http://forum.vingrad.ru/forum/topic-154708/view-all.html

&#039; Initialize variables and WMI-moniker
strComputer = &quot;.&quot;
Set objWMIService = GetObject(&quot;winmgmts:&quot; _
Â Â Â  &amp;amp; &quot;{impersonationLevel=impersonate}!\\&quot; &amp;amp; strComputer &amp;amp; &quot;\root\cimv2&quot;)
Â Â Â 
&#039; Select Automatic Updates and BITS services from the list of services
Set colListOfServices = objWMIService.ExecQuery _
Â Â Â  (&quot;Select * from Win32_Service Where Name = &#039;BITS&#039; or Name = &#039;wuauserv&#039;&quot;)
Â Â Â 
&#039; For each of selected services:
For Each objService in colListOfServices
Â Â Â  isStarted = 0
Â Â Â  &#039; Check its status. If stopped then
Â Â Â  IfÂ  objService.State = &quot;Stopped&quot; Then
Â Â Â Â  &#039; Try to start it.
Â Â Â Â  WScript.Echo (&quot;Service &quot; &amp;amp; objService.Name &amp;amp; &quot; Stopped&quot;)
Â Â Â Â  isStarted = objService.StartService()
Â 
Â Â Â Â  &#039; If when starting we get the same problem try to fix it
Â Â Â Â  &#039; (fix is provided by the Repair function)
Â Â Â Â  If objService.ExitCode &lt;&gt; 0 Then
Â Â Â Â Â Â Â Â  WScript.Echo(&quot;WARNING!!! Error occured, when try to started...&quot;)
Â Â Â Â Â Â Â Â  WScript.Echo (&quot;Try to repair...&quot; &amp;amp; objService.Name)
Â Â Â Â Â Â Â Â Â Â Â  intCode = Repair(strComputer)
Â Â Â Â Â Â Â Â Â Â Â  If intCode = 0 Then
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  WScript.Echo(&quot;Done...&quot;)
Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â  objService.StartService
Â Â Â Â Â Â Â Â Â Â Â  Else WScript.Echo(&quot;Failed To Repair&quot;)
Â Â Â Â Â Â Â Â Â Â Â  End If
Â Â Â Â  Else If objService.ExitCode = 0 Then WScript.Echo (&quot;Successfully stated...&quot;)
Â Â Â Â  End If
Â Â Â  End If
Next
Function Repair(strComputer)
Â Â Â  strCommandLine = &quot;sc sdset &quot; &amp;amp; objService.Name &amp;amp; &quot; &quot; &amp;amp; Chr(34) &amp;amp; &quot;D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;AU)(A;;CCLCSWRPWPDTLOCRRC;;;PU)&quot; &amp;amp; Chr(34)
Â Â Â  Set objWMIService = GetObject(&quot;winmgmts:\\&quot; &amp;amp; strComputer &amp;amp; &quot;\root\CIMV2&quot;)
Â Â Â  &#039; Obtain the definition of the class.
Â Â Â  Set objShare = objWMIService.Get(&quot;Win32_Process&quot;)
Â Â Â  &#039; Obtain an InParameters object specific
Â Â Â  &#039; to the method.
Â Â Â  Set objInParam = objShare.Methods_(&quot;Create&quot;). _
Â Â Â Â  inParameters.SpawnInstance_()
Â Â Â  &#039; Add the input parameters.
Â Â Â  objInParam.Properties_.Item(&quot;CommandLine&quot;) = strCommandLine
Â Â Â  &#039; Execute the method and obtain the return status.
Â Â Â  &#039; The OutParameters object in objOutParams
Â Â Â  &#039; is created by the provider.
Â Â Â  Set objOutParams = objWMIService.ExecMethod(&quot;Win32_Process&quot;, &quot;Create&quot;, objInParam)
Â Â Â  &#039; List OutParams
Â Â Â  WScript.Echo &quot;Out Parameters: &quot;
Â Â Â  WScript.echo &quot;ProcessId: &quot; &amp;amp; objOutParams.ProcessId
Â Â Â  Repair = objOutParams.ReturnValue
End Function</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/06/30/script-to-fix-the-error-with-start-up-of-automatic-updates-and-bits-services-eventid-7023-and-eventid-7024/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script for remote reboot/shutdown, power off and more.</title>
		<link>http://www.bahramov.com/2009/06/30/script-for-remote-rebootshutdown-power-off-and-more/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/06/30/script-for-remote-rebootshutdown-power-off-and-more/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 11:13:26 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[WMI]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=326</guid>
		<description><![CDATA[Script for remote reboot/shutdown, power off, etc.Â You must have proper rights on a remote computerÂ  for this script to work.

&#039; Script for remote reboot/shutdown, power off, etc.
&#039; You must have proper rights on a remote computer
&#039; for this script to work

Dim StationName
Dim OpSysSet
Dim Flags
Dim Reserved
StationName = InputBox(&#34;Insert the Name of the Station to be Shutdown.&#34;)
Set [...]]]></description>
			<content:encoded><![CDATA[<p>Script for remote reboot/shutdown, power off, etc.Â You must have proper rights on a remote computerÂ  for this script to work.</p>
<p><span id="more-326"></span>
<pre class="brush: vb.net">&#039; Script for remote reboot/shutdown, power off, etc.
&#039; You must have proper rights on a remote computer
&#039; for this script to work

Dim StationName
Dim OpSysSet
Dim Flags
Dim Reserved
StationName = InputBox(&quot;Insert the Name of the Station to be Shutdown.&quot;)
Set OpSysSet = GetObject(&quot;winmgmts:{impersonationLevel=impersonate,(RemoteShutdown)}//&quot; &amp; StationName).ExecQuery(&quot;select * from Win32_OperatingSystem where Primary=true&quot;)
&#039; Flags Action
&#039;Â  0Â Â Â  Logoff
&#039;Â  1Â Â Â  Shutdown
&#039;Â  2Â Â Â  Reboot
&#039;Â  4Â Â Â  Force
&#039;Â  8Â Â Â  Power Off
&#039; 16Â Â Â  Force If Hung
Flags = 8
Reserved = 0
For Each OpSys In OpSysSet
Â Â  OpSys.Win32Shutdown Flags,Reserved
Next</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/06/30/script-for-remote-rebootshutdown-power-off-and-more/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script to manage Event Log files</title>
		<link>http://www.bahramov.com/2009/06/30/script-to-manage-event-log-files/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/06/30/script-to-manage-event-log-files/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 10:28:33 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[VB]]></category>
		<category><![CDATA[VBScript]]></category>
		<category><![CDATA[WMI]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=314</guid>
		<description><![CDATA[This script will check the size of the Event Log files and if any of these files will have the size greater than defined value it will back it up and then clean it.


strComputer = &#34;.&#34;
Set objWMIService = GetObject(&#34;winmgmts:&#34; _
Â Â Â  &#38; &#34;{impersonationLevel=impersonate, (Backup, Security)}!\\&#34; _
Â Â Â Â Â Â Â  &#38; strComputer &#38; &#34;\root\cimv2&#34;)
Set colLogFiles = objWMIService.ExecQuery _
Â Â Â  (&#34;Select * [...]]]></description>
			<content:encoded><![CDATA[<p>This script will check the size of the Event Log files and if any of these files will have the size greater than defined value it will back it up and then clean it.</p>
<p><span id="more-314"></span></p>
<p><span></span>
<pre class="brush: vb.net">strComputer = &quot;.&quot;
Set objWMIService = GetObject(&quot;winmgmts:&quot; _
Â Â Â  &amp; &quot;{impersonationLevel=impersonate, (Backup, Security)}!\\&quot; _
Â Â Â Â Â Â Â  &amp; strComputer &amp; &quot;\root\cimv2&quot;)
Set colLogFiles = objWMIService.ExecQuery _
Â Â Â  (&quot;Select * from Win32_NTEventLogFile&quot;)
For Each objLogfile in colLogFiles
Â Â Â  If objLogFile.FileSize &gt; 100000 Then
Â Â Â Â Â Â  strBackupLog = objLogFile.BackupEventLog _
Â Â Â Â Â Â Â Â Â Â  (&quot;c:\scripts\&quot; &amp; objLogFile.LogFileName &amp; &quot;.evt&quot;)
Â Â Â Â Â Â  objLogFile.ClearEventLog()
Â Â Â  End If
Next</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/06/30/script-to-manage-event-log-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>VB Script to query domain computers for currently logged in users</title>
		<link>http://www.bahramov.com/2009/06/30/vb-script-to-query-domain-computers-for-currently-logged-in-users/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/06/30/vb-script-to-query-domain-computers-for-currently-logged-in-users/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 09:34:42 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[Active Directory]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/2009/06/30/vb-script-to-query-domain-computers-for-currently-logged-in-users/</guid>
		<description><![CDATA[Source: http://forum.vingrad.ru/forum/topic-154708/view-all.html
The script will query domain computers for logged on users on each computer


&#039; The script will query domain computers for logged on users
&#039; on each computer
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objRoot = GetObject(&#34;LDAP://RootDSE&#34;)
strDomainName = objRoot.Get(&#34;DefaultNamingContext&#34;)
Set objRoot = Nothing

strComputer = &#34;&#34;
Dim fso
Dim file

Set fso = CreateObject(&#34;Scripting.FileSystemObject&#34;)
Set file = fso.OpenTextFile (&#34;logged_user_list.txt&#34;, 2, True)
Set objShell [...]]]></description>
			<content:encoded><![CDATA[<div class="csharpcode">Source: <a title="http://forum.vingrad.ru/forum/topic-154708/view-all.html" href="http://forum.vingrad.ru/forum/topic-154708/view-all.html">http://forum.vingrad.ru/forum/topic-154708/view-all.html</a></div>
<div>The script will query domain computers for logged on users on each computer</div>
<div><span id="more-309"></span></div>
<div>
<pre class="brush: vb.net">&#039; The script will query domain computers for logged on users
&#039; on each computer
On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2

Set objRoot = GetObject(&quot;LDAP://RootDSE&quot;)
strDomainName = objRoot.Get(&quot;DefaultNamingContext&quot;)
Set objRoot = Nothing

strComputer = &quot;&quot;
Dim fso
Dim file

Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set file = fso.OpenTextFile (&quot;logged_user_list.txt&quot;, 2, True)
Set objShell = CreateObject(&quot;WScript.Shell&quot;)

Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)
Set objCommand = CreateObject(&quot;ADODB.Command&quot;)
objConnection.Provider = &quot;ADsDSOObject&quot;
objConnection.Open &quot;Active Directory Provider&quot;

Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = &quot;Select Name, Location from &#039;LDAP://&quot; &amp; strDomainName &amp; &quot;&#039;&quot; _
&amp; &quot;Where objectClass =&#039;computer&#039;&quot;
objCommand.Properties(&quot;Page Size&quot;) = 1000
objCommand.Properties(&quot;Searchscope&quot;) = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute

&#039;**********************************************************************************

objRecordSet.MoveFirst

Wscript.Echo &quot;Processing information. This might take several minutes.&quot;

Do Until objRecordSet.EOF
Â Â Â  strComputer = objRecordSet.Fields(&quot;Name&quot;).Value

&#039; Check computer online status by using PING
&#039; and analysing output

Â Â Â  Set objScriptExec = objShell.Exec(&quot;%comspec% /c ping.exe -n 1 &quot; &amp; strComputer)
Â Â Â  strPingResults = LCase(objScriptExec.StdOut.ReadAll)

&#039;Â Â Â  If computer responds, connect to WMI

Â Â Â  If InStr(strPingResults, &quot;ttl=&quot;) Then
Â Â Â Â  Set objWMIService = GetObject(&quot;winmgmts:&quot; _
Â Â Â  &amp; &quot;{impersonationLevel=impersonate}!\\&quot; &amp; strComputer &amp; &quot;\root\cimv2&quot;)
Â Â Â Â Â Â 
Â Â Â Â Â Â Â  Set colComputer = objWMIService.ExecQuery _
Â Â Â  (&quot;Select * from Win32_ComputerSystem&quot;)
Â Â 
&#039; List logged on users to file and screen

Â Â Â Â Â Â Â  For Each objComputer in colComputer
Â Â Â Â Â Â Â Â Â Â Â Â  WScript.Echo &quot;Logged-on &quot; &amp;strComputer &amp; &quot; user: &quot; &amp; objComputer.UserName
Â Â Â Â Â Â Â Â Â Â Â Â  file.WriteLine(&quot;Logged-on &quot; &amp;strComputer &amp; &quot; user: &quot; &amp; objComputer.UserName)
Â Â Â Â Â Â Â  Next
Â Â Â Â Â Â Â  objRecordSet.MoveNext
Â Â Â Â Â Â 
&#039; If computer does not respond then show message and continue to next

Â Â Â  Else
Â Â Â Â  WScript.Echo(strComputer &amp; &quot;: Not responding...&quot;)
Â Â Â Â Â Â Â  objRecordSet.MoveNext
Â Â Â  End If
Loop
</pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/06/30/vb-script-to-query-domain-computers-for-currently-logged-in-users/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open / Close CD ROM in Visual Basic</title>
		<link>http://www.bahramov.com/2009/03/23/open-close-cd-rom-in-visual-basic/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/03/23/open-close-cd-rom-in-visual-basic/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 22:06:32 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=149</guid>
		<description><![CDATA[This code demonstrates how to create CD ROM open and close calls. Create a Windows Forms Application and add two buttons. I will leave default names in this example Form1 for the form and Button1 and Button2 for buttons. Iâ€™ve changed the text property only.

Click F7 to go to the code view and add the [...]]]></description>
			<content:encoded><![CDATA[<p>This code demonstrates how to create CD ROM open and close calls. Create a Windows Forms Application and add two buttons. I will leave default names in this example Form1 for the form and Button1 and Button2 for buttons. Iâ€™ve changed the text property only.</p>
<p><a href="http://www.bahramov.com/wp-content/uploads/2009/03/form1.jpg#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" title="form1"><img style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" title="form1" src="http://www.bahramov.com/wp-content/uploads/2009/03/form1-thumb.jpg" border="0" alt="form1" width="244" height="118" /></a></p>
<p><span id="more-149"></span>Click F7 to go to the code view and add the folowing code:</p>
<pre class="brush: vb.net">
&#039; Form design
&#039; - - - - - -
&#039; Button1 â€“ Button control
&#039; Button2 â€“ Button control
&#039; - - - - - -
&#039; Form code

Public Class Form1
Private Declare Function mciSendString Lib &quot;winmm.dll&quot; Alias _
&quot;mciSendStringA&quot; (ByVal lpstrCommand As String, ByVal _
lpstrReturnString As Long, ByVal uReturnLength As _
Long, ByVal hwndCallback As Long) As Long

Dim vDrive As String
Dim vAlias As String

Private Sub Button1_Click() Handles Button1.Click
&#039; Open
vDrive = &quot;f:/&quot;
vAlias = &quot;vv&quot; &amp; vDrive
Call mciSendString(&quot;Open &quot; &amp; vDrive &amp; &quot;: Alias &quot; &amp; vAlias &amp; _
&quot; Type CDAudio&quot;, 0, 0, 0)
Call mciSendString(&quot;Set &quot; &amp; vAlias &amp; &quot; Door Open&quot;, 0, 0, 0)
End Sub

Private Sub Button2_Click() Handles Button2.Click
&#039; Close
vDrive = &quot;f:/&quot;
vAlias = &quot;vv&quot; &amp; vDrive
Call mciSendString(&quot;Open &quot; &amp; vDrive &amp; &quot;: Alias &quot; &amp; vAlias &amp; _
&quot; Type CDAudio&quot;, 0, 0, 0)
Call mciSendString(&quot;Set &quot; &amp; vAlias &amp; &quot; Door Closed&quot;, 0, 0, 0)
End Sub
End Class</pre>
<p>Click F5 to Run the application.</p>
<p><a href="http://www.bahramov.com/wp-content/uploads/2009/03/windowsapplication1.zip#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" target="_self">Download sample</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/03/23/open-close-cd-rom-in-visual-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Log Off, Restart and Shutdown PC in Visual Basic</title>
		<link>http://www.bahramov.com/2009/03/23/how-to-log-off-restart-and-shutdown-pc-in-visual-basic/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/03/23/how-to-log-off-restart-and-shutdown-pc-in-visual-basic/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 08:29:10 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[Coding]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=117</guid>
		<description><![CDATA[Hereâ€™s a small example of how to log off, restart and shutdown computer in Visual Basic. Open Visual Studio and create a new Windows Forms Application:

In the Solution Explorer right-click on ExitWindows â€“&#62; Add â€“&#62; Moduleâ€¦ Leave the default name Module1.vb as is and click Add.
Paste the following code in Module1:
Public Declare Function ExitWindows Lib [...]]]></description>
			<content:encoded><![CDATA[<p>Hereâ€™s a small example of how to log off, restart and shutdown computer in Visual Basic. Open Visual Studio and create a new Windows Forms Application:</p>
<p><a href="http://www.bahramov.com/wp-content/uploads/2009/03/01.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" title="01"><img style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" title="01" src="http://www.bahramov.com/wp-content/uploads/2009/03/01-thumb.png" border="0" alt="01" width="244" height="179" /></a></p>
<p>In the Solution Explorer right-click on ExitWindows â€“&gt; Add â€“&gt; Moduleâ€¦ Leave the default name Module1.vb as is and click Add.</p>
<p>Paste the following code in Module1:</p>
<pre class="code"><span style="color: blue">Public Declare Function </span>ExitWindows <span style="color: blue">Lib </span><span style="color: #a31515">"user32" </span>(<span style="color: blue">ByVal </span>uFlags <span style="color: blue">As Long</span>, _
<span style="color: blue">ByVal </span>dwReserved <span style="color: blue">As Long</span>) <span style="color: blue">As Long

Public Const </span>EWX_LOGOFF = 0
<span style="color: blue">Public Const </span>EWX_SHUTDOWN = 1
<span style="color: blue">Public Const </span>EWX_REBOOT = 2
<span style="color: blue">Public Const </span>EWX_FORCE = 4</pre>
<p><span id="more-117"></span></p>
<p>Now, go to Form1.vb and add three buttons from the toolbox. Set the following properties for buttons:</p>
<table border="0" cellspacing="0" cellpadding="2" width="400">
<tbody>
<tr>
<td width="95" valign="top">Button1:</td>
<td width="305" valign="top"><strong>Name</strong>:Â  btnLogOff</p>
<p><strong>Text</strong>:Â Â Â  Log Off</td>
</tr>
<tr>
<td width="95" valign="top">Button2:</td>
<td width="305" valign="top"><strong>Name</strong>:Â  btnReboot</p>
<p><strong>Text</strong>:Â Â Â  Reboot</td>
</tr>
<tr>
<td width="95" valign="top">Button3:</td>
<td width="305" valign="top"><strong>Name</strong>:Â  btnShutdown</p>
<p><strong>Text</strong>:Â Â Â  Shutdown</td>
</tr>
</tbody>
</table>
<p>Click F7 and paste the following code:</p>
<pre class="code"><span style="color: blue">Public Class </span>Form1
    <span style="color: blue">Dim </span>s <span style="color: blue">As Long

    Private Sub </span>btnLogOff_Click(<span style="color: blue">ByVal </span>sender <span style="color: blue">As </span>System.Object, _
    <span style="color: blue">ByVal </span>e <span style="color: blue">As </span>System.EventArgs) <span style="color: blue">Handles </span>btnLogOff.Click
        <span style="color: green">' Logoff current user
        </span>s = ExitWindows(EWX_LOGOFF, 0&amp;)
    <span style="color: blue">End Sub

    Private Sub </span>btnReboot_Click(<span style="color: blue">ByVal </span>sender <span style="color: blue">As </span>System.Object, _
    <span style="color: blue">ByVal </span>e <span style="color: blue">As </span>System.EventArgs) <span style="color: blue">Handles </span>btnReboot.Click
        <span style="color: green">' Reboot computer
        </span>s = ExitWindows(EWX_REBOOT, 0&amp;)
    <span style="color: blue">End Sub

    Private Sub </span>btnShutdown_Click(<span style="color: blue">ByVal </span>sender <span style="color: blue">As </span>System.Object, _
    <span style="color: blue">ByVal </span>e <span style="color: blue">As </span>System.EventArgs) <span style="color: blue">Handles </span>btnShutdown.Click
        <span style="color: green">' Shutdown computer
        </span>s = ExitWindows(EWX_SHUTDOWN, 0&amp;)
    <span style="color: blue">End Sub
End Class
</span></pre>
<p>Thatâ€™s it <img src="http://messenger.msn.com/MMM2006-04-19_17.00/Resource/emoticons/regular_smile.gif" alt="Smile" />. Download the solution from the link below.</p>
<div id="scid:8eb9d37f-1541-4f29-b6f4-1eea890d4876:0768d2a2-b5bd-4c7e-a094-e0da0520931b" class="wlWriterEditableSmartContent" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<div><a href="http://www.bahramov.com/wp-content/uploads/2009/03/exitwindows2.zip#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed" target="_self">ExitWindows.zip</a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/03/23/how-to-log-off-restart-and-shutdown-pc-in-visual-basic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a custom DLL in Visual Basic .NET</title>
		<link>http://www.bahramov.com/2009/03/18/vbnet-tutorial-creating-a-custom-dll-dynamic-link-library-in-visual-basic-net/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.bahramov.com/2009/03/18/vbnet-tutorial-creating-a-custom-dll-dynamic-link-library-in-visual-basic-net/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 15:36:00 +0000</pubDate>
		<dc:creator>Zaur Bahramov</dc:creator>
				<category><![CDATA[Visual Basic]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[VB]]></category>

		<guid isPermaLink="false">http://www.bahramov.com/?p=27</guid>
		<description><![CDATA[One of the best programming practices is to use DLL (Dynamic Link Library) files to re-use your code and controls across your applications. In this tutorial I will show you how to create a sample DLL file in Visual Basic .NET Professional edition and attach it to a new application.
1)Â Â Â  Create a new project, select [...]]]></description>
			<content:encoded><![CDATA[<p>One of the best programming practices is to use DLL (Dynamic Link Library) files to re-use your code and controls across your applications. In this tutorial I will show you how to create a sample DLL file in Visual Basic .NET Professional edition and attach it to a new application.</p>
<p>1)Â Â Â  Create a new project, select Windows Forms Control Library and name it MiniCalc</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/01.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/01.png" alt="" width="477" height="348" /></span></a></p>
<p><span id="more-27"></span>2)Â Â Â  You will see that the new UserControl1.vb is added to your project. Right-click on the UserControl1.vb and select Rename from the menu. Name it MiniCalc.vb</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/02.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/02.png" alt="" width="261" height="276" /></span></a></p>
<p>3)Â Â Â  Go to the Toolbox and add 1 groupbox, 2 textboxes, 4 buttons and 1 label. Align them on the control as on the following image:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/03.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/03.png" alt="" width="197" height="177" /></span></a></p>
<p>Set the following properties:</p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr bgcolor="#cccccc">
<td colspan="2" valign="top" bordercolor="#000000"><strong>TextBox1</strong></td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Name</td>
<td width="216" valign="top" bordercolor="#000000">txtValue1</td>
</tr>
<tr bgcolor="#cccccc">
<td colspan="2" valign="top" bordercolor="#000000"><strong>TextBox2</strong></td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Name</td>
<td width="216" valign="top" bordercolor="#000000">txtValue2</td>
</tr>
<tr bgcolor="#cccccc">
<td colspan="2" valign="top" bordercolor="#000000"><strong>Button1</strong></td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Name</td>
<td width="216" valign="top" bordercolor="#000000">btnPlus</td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Text</td>
<td width="216" valign="top" bordercolor="#000000">+</td>
</tr>
<tr bgcolor="#cccccc">
<td colspan="2" valign="top" bordercolor="#000000"><strong>Button2</strong></td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Name</td>
<td width="216" valign="top" bordercolor="#000000">btnMinus</td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Text</td>
<td width="216" valign="top" bordercolor="#000000">-</td>
</tr>
<tr bgcolor="#cccccc">
<td colspan="2" valign="top" bordercolor="#000000"><strong>Button3</strong></td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Name</td>
<td width="216" valign="top" bordercolor="#000000">btnMultiply</td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Text</td>
<td width="216" valign="top" bordercolor="#000000">x</td>
</tr>
<tr bgcolor="#cccccc">
<td colspan="2" valign="top" bordercolor="#000000"><strong>Button4</strong></td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Name</td>
<td width="216" valign="top" bordercolor="#000000">btnSubtract</td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Text</td>
<td width="216" valign="top" bordercolor="#000000">/</td>
</tr>
<tr bgcolor="#cccccc">
<td colspan="2" valign="top" bordercolor="#000000"><strong>Label1</strong></td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Name</td>
<td width="216" valign="top" bordercolor="#000000">lblResult</td>
</tr>
<tr bgcolor="#cccccc">
<td colspan="2" valign="top" bordercolor="#000000"><strong>GroupBox1</strong></td>
</tr>
<tr>
<td width="132" valign="top" bordercolor="#000000">Text</td>
<td width="216" valign="top" bordercolor="#000000">MiniCalc</td>
</tr>
</tbody>
</table>
<p>4)Â Â Â  Right-click on the form and select View Code. You will see the following lines of code in the editor:</p>
<pre class="brush: vb.net">
Public Class miniCalc

End Class</pre>
<p>5)Â Â Â  Edit the code to make it as follows:</p>
<pre class="brush: vb.net">
Public Class miniCalc
Private Sub btnPlus_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnPlus.Click
lblResult.Text = CInt(txtValue1.Text) + CInt(txtValue2.Text)
End Sub

Private Sub btnMinus_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnMinus.Click
lblResult.Text = CInt(txtValue1.Text) - CInt(txtValue2.Text)
End Sub

Private Sub btnMultiply_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnMultiply.Click
lblResult.Text = CInt(txtValue1.Text) * CInt(txtValue2.Text)
End Sub

Private Sub btnDivide_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDivide.Click
lblResult.Text = CInt(txtValue1.Text) / CInt(txtValue2.Text)
End Sub

Private Sub GroupBox1_Enter(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles GroupBox1.Enter
lblResult.Text = &quot;&quot;
End Sub
End Class
</pre>
<p>6)Â Â Â  In the solution explorer right-click on MiniCalc and select Properties:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/04.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/04.png" alt="" /></span></a></p>
<p>7)Â Â Â  Make sure that the following parameters are set:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/05.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/05.png" alt="" /></span></a></p>
<p>8)Â Â Â  Now go to Buildâ†’ Build MiniCalc</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/06.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/06.png" alt="" /></span></a></p>
<p>9)Â Â Â  Now, go to the Bin folder in your project folder. In my case, I saved my MiniCalc project under C:\Projects\:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/07.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/07.png" alt="" /></span></a></p>
<p>10)Â Â Â  Copy the MiniCalc.dll to any other directory where you want to store your final DLLs. Itâ€™s always a good idea to keep your DLL project in case if you will need to modify it in the future. In my case I will copy MiniCalc.dll under C:\My DLLs\ folder.</p>
<p>11)Â Â Â  Close MiniCalc project.</p>
<p>12)Â Â Â  Create a new Windows Forms Application project and name it â€œMini Calculatorâ€</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/08.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/08.png" alt="" width="477" height="328" /></span></a></p>
<p>13)Â Â Â  Go to Project and click on Add Referenceâ€¦</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/09.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/09.png" alt="" width="266" height="289" /></span></a></p>
<p>14)Â Â Â  In the Add Reference window click on the Browse tab, locate MiniCalc.dll file and click OK button:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/10.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/10.png" alt="" width="342" height="274" /></span></a></p>
<p>15)Â Â Â  In the Toolbox pane right-click somewhere in the empty space and select Add Tab:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/11.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/11.png" alt="" width="203" height="248" /></span></a></p>
<p>16)Â Â Â  Name it My Controls and click Enter</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/12.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/12.png" alt="" width="218" height="124" /></span></a></p>
<p>17)Â Â Â  Right-click under My Controls and select Choose Itemsâ€¦ from the context menus:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/13.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/13.png" alt="" /></span></a></p>
<p>18)Â Â Â  In the .NET Framework Components tab click Browse.. and locate MiniCalc.dll file.</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/14.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/14.png" alt="" width="399" height="282" /></span></a></p>
<p>19)Â Â Â  miniCalc.dll will be added to your components collection. Click OK button to close the dialog window.</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/15.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/15.png" alt="" width="399" height="283" /></span></a></p>
<p>20)Â Â Â  MiniCalc control is now added to My Controls.</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/16.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/16.png" alt="" /></span></a></p>
<p>21)Â Â Â  Drag it to your form:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/17.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/17.png" alt="" /></span></a></p>
<p>22)Â Â Â  Click F5 to start debugging:</p>
<p><a href="http://www.bahramov.com/images/articles/vb/1/18.png#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed"><span style="font-size: 12pt; font-family: Verdana;" lang="EN-GB"><img src="../../images/articles/vb/1/18.png" alt="" /></span></a></p>
<p>In this tutorial we have learned how to create DLL files and use them across other applications.</p>
<p>Download the sample project <a href="http://www.bahramov.com/images/articles/vb/1/MiniCalcSource.zip#utm_source=feed&amp;utm_medium=feed&amp;utm_campaign=feed">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.bahramov.com/2009/03/18/vbnet-tutorial-creating-a-custom-dll-dynamic-link-library-in-visual-basic-net/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

