maandag 29 september 2008

Vista boot disc / recovery disc

When you need to start Windows Vista from a CD/DVD because the version on the harddisk does not work properly anymore, the question rises how to make a boot disc? I had this problem recently on a laptop where there where disc errors on the C partition. Normally you could just schedule a disc check, unfortunately this did not work (chkdisc did not start). The truth is that I couldn't find out how to make a boot disc, maybe it is not even possible...

I found a solution though, you can download a recovery disc from this website. With this disc I was able to boot from CD and run chkdisc from the CD. The size of the download is approximatly 120 MB and best of all completly free.

vrijdag 26 september 2008

Show temporary file in an external application VB.NET

In this example I will show you how to start an external application and how to generate a temporary file in the temp directory configured in Windows. The source code below create a text file in the temp directory and shows this file Notepad. The filename for the file is automatically generated.

Dim sw As IO.StreamWriter
Dim sTmpFile As String

'Write the temp file
sTmpFile = System.IO.Path.GetTempFileName()
sw = New IO.StreamWriter(sTmpFile, False)
sw.Write(Me.BookingRow.sCRSRecord)
sw.Close()

'Launch the external application
Dim myProcess As System.Diagnostics.Process = New System.Diagnostics.Process()
myProcess.StartInfo.FileName = "notepad.exe"
myProcess.StartInfo.Arguments = sTmpFile
myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal myProcess.Start()

Encryption in VB.NET

Encryption in Visual Basic .NET has become quite simple, as the .NET Framework already includes some popular encryption providers. The following class makes it easier to use encryption in your application.

Public Class CryptTools
Private Shared HashKey As Byte() = {12, 45, 7, 123, 6, 34, 56, 12, 245, 34}

Public Shared Function GetHexHash(ByVal ba As Byte()) As String
Dim hb As String
Dim ret As String = ""

For i As Integer = 0 To ba.Length - 1
hb = Hex(ba(i))
If hb.Length = 0 Then
hb = "00"
ElseIf hb.Length = 1 Then
hb = "0" & hb
End If

ret &= hb
Next

Return ret
End Function

Public Shared Function Encrypt(ByVal data As String, ByVal key As String) As Byte()
Return Encrypt(System.Text.ASCIIEncoding.ASCII.GetBytes(data), key)
End Function

Public Shared Function Encrypt(ByRef data As Byte(), ByVal key As String) As Byte()
Dim RMCrypto As New System.Security.Cryptography.RijndaelManaged
Dim mStream As System.IO.MemoryStream
Dim CryptStream As System.Security.Cryptography.CryptoStream
Dim bytIV() As Byte = {115, 21, 106, 15, 12, 78, 19, 30, 200, 90, 40, 75, 13, 212, 21, 67}
Dim bytKEY() As Byte
Dim sha As New System.Security.Cryptography.HMACSHA256(HashKey)

'Create key from the hash of the string
bytKEY = sha.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))

'Encrypt the data
mStream = New IO.MemoryStream()
RMCrypto.KeySize = 256
CryptStream = New System.Security.Cryptography.CryptoStream(mStream, RMCrypto.CreateEncryptor(bytKEY, bytIV), System.Security.Cryptography.CryptoStreamMode.Write)
CryptStream.Write(data, 0, data.Length)
CryptStream.FlushFinalBlock()

'Cean up
CryptStream.Close()
mStream.Close()

Return mStream.ToArray()
End Function

Public Shared Function Decrypt(ByVal data As String, ByVal key As String) As Byte()
Return Decrypt(System.Text.ASCIIEncoding.ASCII.GetBytes(data), key)
End Function

Public Shared Function Decrypt(ByRef data As Byte(), ByVal key As String) As Byte()
Dim RMCrypto As New System.Security.Cryptography.RijndaelManaged
Dim mStream As System.IO.MemoryStream
Dim CryptStream As System.Security.Cryptography.CryptoStream
Dim cryptBuffer As Byte()
Dim bytIV() As Byte = {115, 21, 106, 15, 12, 78, 19, 30, 200, 90, 40, 75, 13, 212, 21, 67}
Dim bytKEY() As Byte
Dim sha As New System.Security.Cryptography.HMACSHA256(HashKey)

'Create key from the hash of the string
bytKEY = sha.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes(key))

'Encrypt the data
mStream = New IO.MemoryStream(data)
RMCrypto.KeySize = 256
CryptStream = New System.Security.Cryptography.CryptoStream(mStream, RMCrypto.CreateDecryptor(bytKEY, bytIV), System.Security.Cryptography.CryptoStreamMode.Read)
ReDim cryptBuffer(data.Length - 1)
CryptStream.Read(cryptBuffer, 0, data.Length)

'Cean up
CryptStream.Close()
mStream.Close()

Return cryptBuffer
End Function

End Class

Simple encryption in VB

This is an simple introduction on write your own encryption functions, but as stated these function are very simple, so do NOT use this function for encrypting any important data.

The first and very simple encryption:
The following function adds the code number to the ascii value of the text, which all characters change to something else.
An output of this function could be:
Rovvy8*^ro*}zkwwo|*lovy�*s}*os~ro|*}sxq*ƒy|*|o}y|mo}*~y*}oxn*y~*lvu
x}yvsms~on*mywwo|mskv*o7wksv*2,}zkw,3*y|*s}*nomoz~s€ovƒ*~|ƒsxq*~y*wkuo*s~
vyyu*vsuo*ro*s}8*Sx*os~ro|*mk}o6*k*voqs~swk~o*mywzkxƒ*vsuo*ƒy|}
*z|ylklvƒ�yvn*xy~*kzz|y€
o8*^ro*sxpy|wk~syx*lovy�*}ryvn*lo*kvv*ƒy*xoon8
Quite hard to read, isn't it?

Public Function Encrypt1(Txt As String, CodeNr As Integer) As String
Dim i As Long
Dim Out As String

'Inital output value is empty string
Out = ""

'Change ASCII codes
For i = 1 To Len(Txt)
Out = Out & Chr(Asc(Mid$(Txt, i, 1)) + CodeNr)
Next i

Encrypt1 = Out
End Function

As encryption en decryption are not that different we just call Encrypt1 from the decryption function:

Public Function Decrypt1(Txt As String, CodeNr As Integer) As String
'Decryption is the same as encryption with CodeNr*-1
Decrypt1 = Encrypt1(Txt, CodeNr * -1)

End Function

Now you may be wondering why this code is so extremely weak. But it's actually quite simple, as the most commonly used character would normally be the space. Below the code to break this code easily:

Function BreakCode(CText as String) as String
Dim a(0 To 255) As Long
Dim Txt As String
Dim ascii As Byte
Dim l As Byte
Dim q As Long
Dim SugCodeNr As Integer

'Count how often characters occur
Txt = CText
l = 0
For q = 1 To Len(Txt)
ascii = Asc(Mid$(Txt, q, 1))
a(ascii) = a(ascii) + 1
If a(ascii) > a(l) Then l = ascii
Next q

Debug.Print "Most used character: '" & Chr(l) & "'"

'Space (ascii 32) is most often used in texts
'As we now this we can calculate the CodeNr...
SugCodeNr = l - 32
BreakCode = Decrypt1(Txt, SugCodeNr)

Debug.Print "Code number = " & CStr(SugCodeNr)
End function

Open default browser

This source code will open a site/html page in the clients default browser (for example Internet Explorer or Firefox).


Add the following source code to a module:

'***************************************************************

'Windows APIl Declarations for :Open Default Web Browser '***************************************************************

Public Declare Function ShellExecute Lib "shell32.dll" Alias _
"ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As _
String, ByVal lpFile As String, ByVal lpParameters As String, _
ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long


Public Sub GotoWebsite(window As Variant, url As String)
'Opens the default webbrowser with url url,
'window is a reference to a form/window
Dim ret&
ret& = ShellExecute(window.hwnd, "Open", url, "", App.Path, 1)
End Sub

If you want to open a browser window from a form, use the following VB code:

GotoWebsite(Me, "http://www.compuonline.info/")

Get HTML from Webbrowser Control

The following function will get the complete html source code from a webpage shown in the Webbrowser Control:

Public Function GetHTML(Web As WebBrowser) As String
  'declare variables
  Dim doc As HTMLDocument
  
  'Make sure the page has been completely loaded
  'if not wait
  Do
    DoEvents
  Loop Until Not Web.Busy
  
  Set doc = Web.Document
  
  'return the html
  GetHTML = doc.All(0).outerHTML
End Function

Filling a form in the WebBrowser Control (automatically)

In this sample I will fill the altavista search box, with the WebBrowser control. Below I will list some subs and functions which are used in this sample.

Open a new project (standard exe) and place a WebBrowser control, a textbox, and a command button on form1. Make sure you also have an reference to mshtml.tlb.


Add the following code to form1:

Private Sub Command1_Click()
Dim doc As HTMLDocument

'go to the altavista (text) search page
WebBrowser1.Navigate "http://www.altavista.com/cgi-bin/query?text"

'Wait until page is loaded
Do
DoEvents
Loop Until Not WebBrowser1.Busy

'Make doc reference to the document inside the webbrowser control
Set doc = WebBrowser1.Document

'Set field q with the value of Text1
SetInputField doc, 0, "q", Text1

'Submit the form (same result as click the search button)
doc.Forms(0).submit

'Wait until result are loaded
Do
DoEvents
Loop Until Not WebBrowser1.Busy

MsgBox "Altavista search result loaded"
End Sub

Add the following code to a module:

Public Sub SetInputField(doc As HTMLDocument, Form As Integer, Name As String, Value As String)
'doc = HTMLDocument, can be retrieved from webbrowser --> webbrowser.document
'Form = number of the form (if only one form in the doc --> Form = 0)
'Name = Name of the field you would like to fill
'Value = The new value for the input field called name
'PRE: Legal parameters entered
'POST: Input field with name Name on form Form in document doc will be filled with Value

For q = 0 To doc.Forms(Form).length - 1
If doc.Forms(Form)(q).Name = Name Then
doc.Forms(Form)(q).Value = Value
Exit For
End If
Next q
End Sub


Additional useful subs:

Sub to get the contents from a textbox:

Public Function GetInputField(doc As HTMLDocument, Form As Integer, Name As String) As String
For q = 0 To doc.Forms(Form).Length - 1
If doc.Forms(Form)(q).Name = Name Then
GetInputField = doc.Forms(From)(q).Value
Exit For
End If
Next q
End Function



Sub to set a Checkbox:

Public Sub SetCheckBox(doc As HTMLDocument, Form As Integer, Name As String, Value As Boolean)
For q = 0 To doc.Forms(Form).Length - 1
If doc.Forms(Form)(q).Name = Name Then
doc.Forms(From)(q).Checked = Value
Exit For
End If
Next q
End Sub



Sub set a radio button:

Public Sub SetRadioButton(doc As HTMLDocument, Form As Integer, Name As String, Name2 As String)
For q = 0 To doc.Forms(Form).Length - 1
If (doc.Forms(Form)(q).Name = Name) And (doc.Forms(Form)(q).Value = Name2) Then
doc.Forms(From)(q).Checked = True
Exit For
End If
Next q
End Sub


Sub set a combo box:

Public Function SetComboBoxValue(ByVal doc As IHTMLDocument3, Form As
Integer, Name As String, Name2 As String)
'**** This one bases it's selection on the Value of the - - Tag.
Dim q, i

For q = 0 To doc.Forms(Form).length - 1
If (doc.Forms(Form)(q).Name = Name) Then
For i = 0 To doc.Forms(Form)(q).length - 1
If doc.Forms(Form)(q).Options(i).Value = Name2 Then
doc.Forms(Form)(q).Options(i).Selected = True
Exit For
End If
Next i
End If
Next q
End Function


Public Function SetComboValue(ByVal doc As IHTMLDocument3, Form As Integer,
Name As String, Name2 As String)
'
**** This one bases it's selection on the Value of the Text after the -
'Text - Tag.
Dim q, i

For q = 0 To doc.Forms(Form).length - 1
If (doc.Forms(Form)(q).Name = Name) Then
For i = 0 To doc.Forms(Form)(q).length - 1
If doc.Forms(Form)(q).Options(i).Text = Name2 Then
doc.Forms(Form)(q).Options(i).Selected = True
Exit For
End If
Next
End If
Next q

Order the WebBrowser control to click a link

The following sub will click a link with the text LinkText in a document, given that you referenced mshtml.tlb and pass a HTMLDocument as the doc parameter (for example WebBrowser.Document):

Public Sub ClickLink(doc, LinkText As String)
For i = 0 To doc.links.length - 1
If LTrim(RTrim(doc.links(i).outerText)) = LinkText Then
doc.links(i).Click
Exit For
End If
Next i
End Sub

The following sub will click a link which refers to LinkUrl in a document:


Public Sub ClickLinkUrl(doc, LinkUrl As String)
For i = 0 To doc.links.length - 1
If LTrim(RTrim(doc.links(i).href)) = LinkUrl Then
doc.links(i).Click
Exit For
End If
Next i
End Sub