topten

require admin and error report, how?

7 posts in this topic

I have a script. If I run it without require admin option it wont execute some of its functions. If I execute it with require admin it would execute one of its function, but then later it fails somewhere- but I dont know the reason- because the reason of error is not displayed on the console already. How can I deal with this? Run with admin privileges and know what is the error?

 

Thanx in advance!

Share this post


Link to post
Share on other sites



Any chance we can have a look-see at the script? :)


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

Ah, may be this will help:

;#Include "APIConstants.au3"
#Include <GUIConstantsEx.au3>
#Include <EditConstants.au3>
#Include "WinAPIEx.au3"
#Include "APIVhdConstants.au3"
#Include "WinAPIVhd.au3"
#RequireAdmin
Opt('MustDeclareVars', 1)
Opt('TrayAutoPause', 0)

Global $hForm, $hVHD = 0, $Edit, $Button[3], $Path, $Size = 128 * 1024 * 1024

If _WinAPI_GetVersion() < '6.1' Then
    MsgBox(16, 'Error', 'Require Windows 7 or later.')
    Exit
EndIf

$hForm = GUICreate('VHD API Example', 460, 302)
$Edit = GUICtrlCreateEdit('', 13, 13, 434, 238, $ES_READONLY)
GUICtrlSetFont(-1, 9, 400, 0, 'Courier New')
GUICtrlSetBkColor(-1, 0xFFFFFF)
GUICtrlSetColor(-1, 0x404040)
$Button[0] = GUICtrlCreateButton('Create VHD', 12, 263, 140, 27)
$Button[1] = GUICtrlCreateButton('Open VHD', 160, 263, 140, 27)
$Button[2] = GUICtrlCreateButton('Close VHD', 308, 263, 140, 27)
GUICtrlSetState(-1, $GUI_DISABLE)
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            If $hVHD Then
                _VHD_Close()
            EndIf
            Exit
        Case $Button[0]
            $Path = FileSaveDialog('Create VHD', @ScriptDir, 'Virtual Hard Disk (*.vhd)|All Files (*.*)', 2 + 16, 'Test.vhd', $hForm)
            If $Path Then
                If FileExists($Path) Then
                    FileDelete($Path)
                EndIf
            Else
                ContinueLoop
            EndIf
            GUICtrlSetData($Edit, '')
            If Not _VHD_Create($Path, $Size) Then
                If FileExists($Path) Then
                    FileDelete($Path)
                EndIf
                ContinueLoop
            EndIf
            For $i = 0 To 1
                GUiCtrlSetState($Button[$i], $GUI_DISABLE)
            Next
            GUiCtrlSetState($Button[2], $GUI_ENABLE)
        Case $Button[1]
            $Path = FileOpenDialog('Open VHD', @ScriptDir, 'Virtual Hard Disk (*.vhd)|All Files (*.*)', 1 + 2, '', $hForm)
            If $Path Then

            Else
                ContinueLoop
            EndIf
            GUICtrlSetData($Edit, '')
            If Not _VHD_Open($Path) Then
                ContinueLoop
            EndIf
            For $i = 0 To 1
                GUiCtrlSetState($Button[$i], $GUI_DISABLE)
            Next
            GUiCtrlSetState($Button[2], $GUI_ENABLE)
        Case $Button[2]
            _VHD_Close()
            GUiCtrlSetState($Button[2], $GUI_DISABLE)
            For $i = 0 To 1
                GUiCtrlSetState($Button[$i], $GUI_ENABLE)
            Next
    EndSwitch
WEnd

Func _P2L($sDisk)

    Local $aData, $aList, $iDisk, $iTime, $sMask = ''

    $iDisk = StringReplace($sDisk, '\\.\PHYSICALDRIVE', '')
    If Not StringIsDigit($iDisk) Then
        Return $sDisk
    EndIf
    $iDisk = Number($iDisk)
    $iTime = TimerInit()
    While TimerDiff($iTime) < 5000
        $aList = DriveGetDrive('FIXED')
        If IsArray($aList) Then
            For $i = 1 To $aList[0]
                If Not StringInStr($sMask, $aList[$i]) Then
                    $aData =_WinAPI_GetDriveNumber($aList[$i])
                    If (IsArray($aData)) And ($aData[1] = $iDisk) Then
                        Return StringUpper($aList[$i])
                    EndIf
                EndIf
                $sMask&= $aList[$i]
            Next
        EndIf
        Sleep(10)
    WEnd
    Return $sDisk
EndFunc   ;==>_P2L

Func _Log($sEvent, $iError = -1)
    Switch $iError
        Case -1
            $sEvent &= @CRLF
        Case  0
            $sEvent &= ': OK' & @CRLF
        Case Else
            $sEvent &= ': FAIL (' &  $iError & ')' & @CRLF
    EndSwitch
    GUICtrlSendMsg($Edit, $EM_SETSEL, 32767, 32767)
    GUICtrlSetData($Edit, $sEvent, 1)
EndFunc   ;==>_Log

Func _VHD_Close()

    Local $sDisk = _P2L(_WinAPI_GetVirtualDiskPhysicalPath($hVHD))

    _WinAPI_DetachVirtualDisk($hVHD)
    If @Error Then
        _Log('Detach', @Extended)
    Else
        _Log('Detach', 0)
    EndIf
    _WinAPI_CloseHandle($hVHD)
    $hVHD = 0
    If $sDisk Then
        _Log('VHD (' & $sDisk & ') has been closed!')
    EndIf
EndFunc   ;==>_VHD_Close

Func _VHD_Create($sPath, $iSize)

    Local $tVST, $tCVDPV1, $tGVDI, $sDisk, $Error = True

    _Log('Path: ' & _WinAPI_PathCompactPathEx($sPath, 60))
    Do
        $tVST = _WinAPI_CreateVirtualStorageType($VIRTUAL_STORAGE_TYPE_DEVICE_VHD, $VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT)
        $tCVDPV1 = _WinAPI_CreateCreateVirtualDiskParametersV1($iSize)
        $hVHD = _WinAPI_CreateVirtualDisk($sPath, $tVST, $tCVDPV1, $VIRTUAL_DISK_ACCESS_ALL, $CREATE_VIRTUAL_DISK_FLAG_FULL_PHYSICAL_ALLOCATION)
        If @Error Then
            _Log('Create', @Extended)
            ExitLoop
        Else
            _Log('Create', 0)
        EndIf
        _WinAPI_AttachVirtualDisk($hVHD)
        If @Error Then
            _Log('Attach', @Extended)
            ExitLoop
        Else
            _Log('Attach', 0)
        EndIf
        $sDisk = _WinAPI_GetVirtualDiskPhysicalPath($hVHD)
        If @Error Then
            ; Nothing
        EndIf
        _VHD_Initialize($sDisk)
        If @Error Then
            _Log('Initialize', @Extended)
            ExitLoop
        Else
            _Log('Initialize', 0)
        EndIf
        $Error = False
    Until 1
    If $Error Then
        If $hVHD Then
            _WinAPI_DetachVirtualDisk($hVHD)
            _WinAPI_CloseHandle($hVHD)
        EndIf
        $hVHD = 0
        Return 0
    EndIf
    $tGVDI = _WinAPI_GetVirtualDiskInformation($hVHD, $GET_VIRTUAL_DISK_INFO_IDENTIFIER)
    If Not @Error Then
        _Log('GUID: ' & _WinAPI_StringFromGUID(DllStructGetPtr($tGVDI, 'Identifier')))
    EndIf
    $tGVDI = _WinAPI_GetVirtualDiskInformation($hVHD, $GET_VIRTUAL_DISK_INFO_SIZE)
    If Not @Error Then
        _Log('Size: ' & _WinAPI_StrFormatByteSize(DllStructGetData($tGVDI, 'VirtualSize')))
    EndIf
    _Log('VHD (' & _P2L($sDisk) & ') has been successfully created and ready for use!')
    Return 1
EndFunc   ;==>_VHD_Create

Func _VHD_Initialize($sDisk)

;~  Local Const $PARTITION_ENTRY_UNUSED = 0x00
;~  Local Const $PARTITION_EXTENDED = 0x05
;~  Local Const $PARTITION_FAT_12 = 0x01
;~  Local Const $PARTITION_FAT_16 = 0x04
;~  Local Const $PARTITION_FAT_32 = 0x0B
    Local Const $PARTITION_IFS = 0x07
;~  Local Const $PARTITION_LDM = 0x42
;~  Local Const $PARTITION_NTFT = 0x80
;~  Local Const $PARTITION_VALID_NTFT = 0xC0

    Local Const $PARTITION_STYLE_MBR = 0
;~  Local Const $PARTITION_STYLE_GPT = 1
;~  Local Const $PARTITION_STYLE_RAW = 2

    Local Const $tagDISK_GEOMETRY = 'int64 Cylinders;dword MediaType;dword TracksPerCylinder;dword SectorsPerTrack;dword BytesPerSector;'
    Local Const $tagCREATE_DISK_MBR = 'dword Signature;byte GPT[16];'
;~  Local Const $tagCREATE_DISK_GPT = 'byte DiskId[16];dword MaxPartitionCount'
    Local Const $tagCREATE_DISK = 'dword PartitionStyle;' & $tagCREATE_DISK_MBR
    Local Const $tagPARTITION_INFORMATION_MBR = 'byte PartitionType;boolean BootIndicator;boolean RecognizedPartition;dword HiddenSectors;byte GPT[104];'
;~  Local Const $tagPARTITION_INFORMATION_GPT = 'byte PartitionType[16];byte PartitionId[16];uint64 Attributes;wchar Name[36];'
    Local Const $tagPARTITION_INFORMATION_EX = 'dword PartitionStyle;int64 StartingOffset;int64 PartitionLength;dword PartitionNumber;boolean RewritePartition;byte Alignment[3];' & $tagPARTITION_INFORMATION_MBR
    Local Const $tagDRIVE_LAYOUT_INFORMATION_MBR = 'ulong Signature;byte GPT[36];'
;~  Local Const $tagDRIVE_LAYOUT_INFORMATION_GPT = 'byte DiskId[16];int64 StartingUsableOffset;int64 UsableLength;ulong MaxPartitionCount;'
    Local Const $tagDRIVE_LAYOUT_INFORMATION_EX = 'dword PartitionStyle;dword PartitionCount;' & $tagDRIVE_LAYOUT_INFORMATION_MBR & 'byte PartitionEntry[144];'
    Local Const $tagVERIFY_INFORMATION = 'int64 StartingOffset;dword Length'

    Local $hFile, $tDG, $tCD, $tDLIEX, $tPIEX, $tVI, $Error, $Extended = 0

    Do
        $Error = 1
        $hFile = _WinAPI_CreateFileEx($sDisk, $OPEN_EXISTING, BitOR($GENERIC_READ, $GENERIC_WRITE), BitOR($FILE_SHARE_READ, $FILE_SHARE_WRITE), $FILE_ATTRIBUTE_NORMAL)
        If @Error Then
            ExitLoop
        EndIf
        ; IOCTL_DISK_GET_DRIVE_GEOMETRY
        $Error = 2
        $tDG = DllStructCreate($tagDISK_GEOMETRY)
        If Not _WinAPI_DeviceIoControl($hFile, $IOCTL_DISK_GET_DRIVE_GEOMETRY, 0, 0, DllStructGetPtr($tDG), DllStructGetSize($tDG)) Then
            ExitLoop
        EndIf
        ; IOCTL_DISK_CREATE_DISK
        $Error = 3
        $tCD = DllStructCreate($tagCREATE_DISK)
        DllStructSetData($tCD, 'PartitionStyle', $PARTITION_STYLE_MBR)
        DllStructSetData($tCD, 'Signature', 0xA4B57300)
        If Not _WinAPI_DeviceIoControl($hFile, $IOCTL_DISK_CREATE_DISK, DllStructGetPtr($tCD), DllStructGetSize($tCD)) Then
            ExitLoop
        EndIf
        ; IOCTL_DISK_SET_DRIVE_LAYOUT_EX
        $Error = 4
        $tDLIEX = DllStructCreate($tagDRIVE_LAYOUT_INFORMATION_EX)
        DllStructSetData($tDLIEX, 'PartitionStyle', $PARTITION_STYLE_MBR)
        DllStructSetData($tDLIEX, 'PartitionCount', 1)
        DllStructSetData($tDLIEX, 'Signature', 0xA4B57300)
        $tPIEX = DllStructCreate($tagPARTITION_INFORMATION_EX, DllStructGetPtr($tDLIEX, 'PartitionEntry'))
        DllStructSetData($tPIEX, 'PartitionStyle', $PARTITION_STYLE_MBR)
        DllStructSetData($tPIEX, 'StartingOffset', 0)
        DllStructSetData($tPIEX, 'PartitionLength', DllStructGetData($tDG, 'Cylinders') * DllStructGetData($tDG, 'TracksPerCylinder') * DllStructGetData($tDG, 'SectorsPerTrack') * DllStructGetData($tDG, 'BytesPerSector'))
        DllStructSetData($tPIEX, 'PartitionNumber', 1)
        DllStructSetData($tPIEX, 'RewritePartition', 1)
        DllStructSetData($tPIEX, 'PartitionType', $PARTITION_IFS)
        DllStructSetData($tPIEX, 'BootIndicator', 0)
        DllStructSetData($tPIEX, 'RecognizedPartition', 1)
        DllStructSetData($tPIEX, 'HiddenSectors', 1)
        If Not _WinAPI_DeviceIoControl($hFile, $IOCTL_DISK_SET_DRIVE_LAYOUT_EX, DllStructGetPtr($tDLIEX), DllStructGetSize($tDLIEX)) Then
            ExitLoop
        EndIf
        ; IOCTL_DISK_VERIFY
        $Error = 5
        $tVI = DllStructCreate($tagVERIFY_INFORMATION)
        DllStructSetData($tVI, 'StartingOffset', 0)
        DllStructSetData($tVI, 'Length', DllStructGetData($tPIEX, 'PartitionLength'))
        If Not _WinAPI_DeviceIoControl($hFile, $IOCTL_DISK_VERIFY, DllStructGetPtr($tVI), DllStructGetSize($tVI)) Then
            ExitLoop
        EndIf
        ; IOCTL_DISK_UPDATE_PROPERTIES
        $Error = 6
        If Not _WinAPI_DeviceIoControl($hFile, $IOCTL_DISK_UPDATE_PROPERTIES) Then
            ExitLoop
        EndIf
        $Error = 0
    Until 1
    If $Error Then
        $Extended = _WinAPI_GetLastError()
    EndIf
    If $hFile Then
        _WinAPI_CloseHandle($hFile)
    EndIf
    Return SetError($Error, $Extended, Not $Error)
EndFunc   ;==>_VHD_Initialize

Func _VHD_Open($sPath)

    Local $tVST, $tGVDI, $sDisk, $Error = True

    _Log('Path: ' & _WinAPI_PathCompactPathEx($sPath, 50))
    Do
        $tVST = _WinAPI_CreateVirtualStorageType($VIRTUAL_STORAGE_TYPE_DEVICE_VHD, $VIRTUAL_STORAGE_TYPE_VENDOR_MICROSOFT)
        $hVHD = _WinAPI_OpenVirtualDisk($sPath, $tVST, 0, $VIRTUAL_DISK_ACCESS_ALL)
        If @Error Then
            _Log('Open', @Extended)
            ExitLoop
        Else
            _Log('Open', 0)
        EndIf
        _WinAPI_AttachVirtualDisk($hVHD)
        If @Error Then
            _Log('Attach', @Extended)
            ExitLoop
        Else
            _Log('Attach', 0)
        EndIf
        $Error = False
    Until 1
    If $Error Then
        If $hVHD Then
            _WinAPI_DetachVirtualDisk($hVHD)
            _WinAPI_CloseHandle($hVHD)
        EndIf
        $hVHD = 0
        Return 0
    EndIf
    $tGVDI = _WinAPI_GetVirtualDiskInformation($hVHD, $GET_VIRTUAL_DISK_INFO_IDENTIFIER)
    If Not @Error Then
        _Log('GUID: ' & _WinAPI_StringFromGUID(DllStructGetPtr($tGVDI, 'Identifier')))
    EndIf
    $tGVDI = _WinAPI_GetVirtualDiskInformation($hVHD, $GET_VIRTUAL_DISK_INFO_SIZE)
    If Not @Error Then
        _Log('Size: ' & _WinAPI_StrFormatByteSize(DllStructGetData($tGVDI, 'VirtualSize')))
    EndIf
    $sDisk = _WinAPI_GetVirtualDiskPhysicalPath($hVHD)
    If Not @Error Then
        _Log('VHD (' & _P2L($sDisk) & ') has been successfully opened and ready for use!')
    EndIf
    Return 1
EndFunc   ;==>_VHD_Open

I am testing this thread:

'?do=embed' frameborder='0' data-embedContent>>

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Parts like this...

$Path = FileSaveDialog('Create VHD', @ScriptDir, 'Virtual Hard Disk (*.vhd)|All Files (*.*)', 2 + 16, 'Test.vhd', $hForm)
            If $Path Then
                If FileExists($Path) Then
                    FileDelete($Path)
                EndIf
            Else
                ContinueLoop
            EndIf

FileDelete may need admin..

Go through it all and add checks, and deal with the results.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Can you tell us some of the functions that do not happen to run when admin & vice versa? 

Many of these might be due to policy etc.

EDIT: As J1 stated, error checking all will help out greatly.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites

When I run it without #requireadmin a gui appears, then I try to create a virtual disk

The virtual disk is created, but it fails to attach it

Then I run with require admin- the virtual disk is created and it attaches it and after that the script fails, gui disappears. I guess it fails further to arrange the disk. Because when I am trying to access it via windows explorer- popup appears that the disk is not arranged properly

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now