#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=VHD Auto Attach 1.1\Icons\Yes.ico
#AutoIt3Wrapper_Outfile=VHD Auto Attach 1.1\VHD Auto Attach.exe
#AutoIt3Wrapper_Outfile_x64=VHDAttachx64.exe
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_Res_Comment=By StevenX Media
#AutoIt3Wrapper_Res_Description=Attach your VHD!
#AutoIt3Wrapper_Res_Fileversion=1.1.0.0
#AutoIt3Wrapper_Res_LegalCopyright=(C) StevenX Media 2011
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

;---------------------------------------------------------------------
;VHD Auto Attach
;---------------------------------------------------------------------
;Author: Stefan Blagojevic alias StevenX
;.....................................................................
;Version: 1.1
;Open Source
;Please donīt change and save this file under the same name!
;stefan.blagojevic@live.at
;....................................................................
;--------------------------------------------------------------------
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Region ### START Koda GUI section ### Form=
$GUI = GUICreate("VHD Auto Attach by StevenX Version 1.0 BETA", 461, 252, 193, 122)
$File_Select = GUICtrlCreateButton("Select VHD File", 112, 40, 233, 73)
$OK = GUICtrlCreateButton("OK", 80 , 136, 289, 65)
$Reg = GUICtrlCreateButton( "Speed Attach Activating", -1, 200)
$Attachbutton = GUICtrlCreateButton( "Attach VHD File", 285, 200)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
_Copy()
While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit
		Case $File_Select
			;----------------------Selection for AutoStart VHD File
	$Datei = FileOpenDialog( "Select VHD", @ScriptDir, "VHD File (*.*)", 1)
	if @error Then
		MsgBox( 0+16, "ERROR!", "No File selected!")
	EndIf
Case $Attachbutton
	;-----------------------------Selection for Attaching
	$Datei = FileOpenDialog( "Select VHD", @ScriptDir, "VHD File (*.*)", 1)
	_Attach()
Case $OK
	$Ini = IniWrite( @MyDocumentsDir & '\StevenX Media\VHDAUTOATTACH\Settings.ini', 'Settings', 'VHDDIR', $Datei)
	if $Ini = 1 Then
		_Fragen()
	EndIf
Case $Reg
	;------------------------------For Speed Attaching
FileCopy( @ScriptDir & '\AutoStartFileDoNotOPEN.exe', 'C:\VHDAutoattach\Attach.exe',1)
RegWrite("HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\VHD Speed Attach\","","REG_SZ","VHD Speed Attach");-----------Attaching
RegWrite("HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\VHD Speed Attach\Command","","REG_SZ", @ScriptDir & '\AttachRun.exe UNC %1')
RegWrite("HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\VHD Speed Dettach\","","REG_SZ","VHD Speed Detach");--------------------Attaching
RegWrite("HKEY_CLASSES_ROOT\AllFilesystemObjects\shell\VHD Speed Dettach\Command","","REG_SZ", @ScriptDir & '\DetachRun.exe UNC %1');-----Detaching
EndSwitch
WEnd
Func _Copy()
	;----------------------Copying the AutoStart file
	$Copy = FileCopy( @ScriptDir & '\Startup.exe', @StartMenuDir & '\Programs\Startup\VHDAUTOATTACH.EXE',1)

	If $Copy = 0 Then
		MsgBox(0,"ERROR!","Could not copy Autostart file!")
	EndIf
EndFunc


Func _Fragen()
			$Frag			= MsgBox(4+48, "Successful!", "Managed your VHD File! Do you want to attach it now?")
			If $Frag = 6 Then
				$Attach = IniRead(@MyDocumentsDir & '\StevenX Media\VHDAUTOATTACH\Settings.ini', 'Settings', 'VHDDIR', 'ERROR!!')
				_Attach()
EndIf
EndFunc

Func _Attach()
	;---------------Attaching function. You will also find this in the file 'Attachrun.au3'
	TrayTip( "Starting Attaching...", "VHDAutoAttach starts. Please standby and let the focus on the windows!", 30, 2)
	Sleep(2000)
Run( "Diskpart.exe")
Sleep(1000)
Send('list disk {ENTER}')
Send('list volume {ENTER}')
Send('select vdisk file= ' & $Datei & '{ENTER}')
Send('Attach vdisk {ENTER}')
Sleep(1000)
WinClose( "[ACTIVE]","")

EndFunc


