ShowDialog()出现奇怪的PowerShell + WPF错误

时间:2015-10-27 21:42:36

标签: wpf xaml powershell dialog runtime-error

所以我正在开发一个基于Powershell的WPF应用程序,我收到以下错误:

Exception calling "ShowDialog" with "0" argument(s): "Unidentified: USB_Disable="
At C:\Users\miste\OneDrive\Documents\Visual Studio 2015\Projects\Vaccinate-USB\Vaccinate-USB\Vaccinate-USB.ps1:344 char:5
+     $Form.ShowDialog() | out-null
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : RuntimeException

现在相当奇怪的是,"USB_Disable="字符串只能在一个永远不会与$Form.ShowDialog()联系的hereString中找到。下面是初始化函数的摘录,其中包含hereString,然后是表单加载器函数。如果您需要完整的代码,请告诉我(它大约有800行)

初始化程序:

function Initialize-ARD
{
    if (-not (Test-Admin))
    {
        $wshpop = New-Object -ComObject Wscript.Shell
        $wshpop.Popup('Please run as an administrator.',0,'Error!',0x0)
        exit
    }
    if(-not (Test-Path -Path $ARDHome))
    {
        if(-not (Test-IsNetConnected))
        {
            $wshpop = New-Object -ComObject Wscript.Shell
            $wshpop.Popup('Please make sure you have an internet connection for first run. Connect to the internet and the re-run the program.',0,'Error!',0x0)
            exit
        }
        New-Item -Path $ARDHome -ItemType 'Directory' -Force
        New-Item -Path "$ARDHome\config.ini" -ItemType 'File' -Force
        #HERESTRING STARTS HERE!
        Set-Content -Path "$ARDHome\config.ini" -Force -Value @'
[USB]
USB_Block=False
USB_Lock=False
USB_Delete=False
USB_Disable=False
[CD]
CD_Block=False
CD_Lock=False
CD_Delete=False
CD_Disable=False
[Firewall]
Firewall_Enabled=False
Firewall_Disabled=True
Firewall_UpdateFreq=0.5
'@
        #New-Download -URL 'http://ha.x10.bz/package.zip/' -Destination "$ARDHome\package.zip"
        #Expand-ZIPFile -File "$ARDHome\package.zip" -Destination $ARDHome
        #Import-Module "$ARDHome\DeviceManagment\DeviceManagement.psd1" -Force
    }
    else{}
    $script:ini = Get-IniFile -FilePath "$ARDHome\config.ini"
    $USBBlock = $ini.USB.USB_Block
    $USBLock = $ini.USB.USB_Lock
    $USBDelete = $ini.USB.USB_Delete
    $USBDisable = $ini.USB.USB_Disable
    $CDBlock = $ini.CD.CD_Block
    $CDLock = $ini.CD.CD_Lock
    $CDDelete = $ini.CD.CD_Delete
    $CDEject = $ini.CD.CD_Eject
    $FirewallEnabled = $ini.Firewall.Firewall_Enabled
    $FirewallDisabled = $ini.Firewall.Firewall_Disabled
    $FirewallValue = $ini.Firewall.Firewall_UpdateFreq
    if($USBBlock -eq "True")
    {
        $USBBlock = $true
    }
    else
    {
        $USBBlock = $false
    }
    if($USBLock -eq "True")
    {
        $USBLock = $true
    }
    else
    {
        $USBLock = $false
    }
    if($USBDelete -eq "True")
    {
        $USBDelete = $true
    }
    else
    {
        $USBDelete = $false
    }
    if($USBDisable -eq "True")
    {
        $USBDisable = $true
    }
    else
    {
        $USBDisable = $false
    }
    if($CDBlock -eq "True")
    {
        $CDBlock = $true
    }
    else
    {
        $CDBlock = $false
    }
    if($CDDelete -eq "True")
    {
        $CDDelete = $true
    }
    else
    {
        $CDDelete = $false
    }
    if($CDEject -eq "True")
    {
        $CDEject = $true
    }
    else
    {
        $CDEject = $false
    }
    if($CDLock -eq "True")
    {
        $CDLock = $true
    }
    else
    {
        $CDLock = $false
    }
    if($FirewallEnabled -eq "True")
    {
        $FirewallEnabled = $true
    }
    else
    {
        $FirewallEnabled = $false
    }
    if($FirewallDisabled -eq "True")
    {
        $FirewallDisabled = $true
    }
    else
    {
        $FirewallDisabled = $false
    }
    $USB_Block.IsChecked = $USBBlock
    $USB_Lock.IsChecked = $USBLock
    $USB_Delete.IsChecked = $USBDelete
    $USB_Disable.IsChecked = $USDisable
    $CD_Block.IsChecked = $CDBlock
    $CD_Lock.IsChecked = $CDLock
    $CD_Delete.IsChecked = $CDDelete
    $CD_Eject.IsChecked = $CDEject
    $Firewall_Enabled.IsChecked = $FirewallEnabled
    $Firewall_Disabled.IsChecked = $FirewallDisabled
    $Firewall_UpdateFreq.Value = $FirewallValue
}

表单及其加载器:

function Initialize-Window
{
  [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
  [xml]$XAML = @"
<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Autorun Defender" Height="376" Width="528" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" Background="Black" Opacity="1">
  <Grid Margin="0,10,4,1">
    <TabControl Name="tabControl" HorizontalAlignment="Left" Height="285.5" VerticalAlignment="Top" Width="493" Margin="10,0,0,0" Background="#FF2E2E2E">
      <TabItem Header="Welcome" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
        <Grid Background="#FF2E2E2E">
          <Viewbox HorizontalAlignment="Left" Margin="10,0,0,87.5" Width="116">
            <Image Name="image" Height="100" Width="100" Source="$(Get-ScriptDirectory)\Logo.png"/>
          </Viewbox>
          <Label Name="title1" Content="Autorun" HorizontalAlignment="Left" Margin="126,0,0,0" VerticalAlignment="Top" Height="67" Width="191" FontSize="48" FontStyle="Italic" FontWeight="Bold" FontFamily="Harlow Solid Italic"/>
          <Label Name="title2" Content="Defender" HorizontalAlignment="Left" Margin="206,67,0,87.5" Width="205" FontSize="48" FontStyle="Italic" FontWeight="Bold" FontFamily="Harlow Solid Italic"/>
          <Label Name="copyright" Content="© 2015 LogoiLab" VerticalAlignment="Bottom" FontSize="12" Height="26.5" HorizontalAlignment="Left" Width="101" Foreground="#FFD4D4D4"/>
        </Grid>
      </TabItem>
      <TabItem Header="USB" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
        <Grid Background="#FF2E2E2E">
          <GroupBox Name="USB_Passive" Header="Passive" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
            <Grid>
              <CheckBox Name="USB_Block" Content="Block Autorun" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
              <CheckBox Name="USB_Lock" Content="Lock Autorun Edits" Margin="10,30,89.656,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
            </Grid>
          </GroupBox>
          <GroupBox Name="USB_Active" Header="Active" HorizontalAlignment="Left" Margin="249,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
            <Grid>
              <CheckBox Name="USB_Delete" Content="Delete Autorun.inf Files" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
              <CheckBox Name="USB_Disable" Content="Disable USBs With Autorun" HorizontalAlignment="Left" Margin="10,30,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
            </Grid>
          </GroupBox>
        </Grid>
      </TabItem>
      <TabItem Header="CD/DVD" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
        <Grid Background="#FF2E2E2E">
          <GroupBox Name="CD_Passive" Header="Passive" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
            <Grid>
              <CheckBox Name="CD_Block" Content="Block Autorun" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
              <CheckBox Name="CD_Lock" Content="Lock Autorun Edits" Margin="10,30,89.656,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
            </Grid>
          </GroupBox>
          <GroupBox Name="CD_Active" Header="Active" HorizontalAlignment="Left" Margin="249,10,0,0" VerticalAlignment="Top" Height="224" Width="230" Foreground="#FFD4D4D4">
            <Grid>
              <CheckBox Name="CD_Delete" Content="Delete Autorun.inf Files" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
              <CheckBox Name="CD_Eject" Content="Eject CDs With Autorun" HorizontalAlignment="Left" Margin="10,30,0,0" Width="227" Height="26" VerticalAlignment="Top" Foreground="#FFD4D4D4" Background="black"/>
            </Grid>
          </GroupBox>
        </Grid>
      </TabItem>
      <TabItem Header="Firewall" Background="#FF2E2E2E" Margin="-2,0,2,0" Width="80">
        <Grid Background="#FF2E2E2E">
          <TextBlock TextWrapping="WrapWithOverflow" Foreground="#FFD4D4D4">
            This is a device firewall. It will block new devices from being &quot;Pluged and Played&quot;. This type of firewall is meant to prevent such attacks as "BadUSB" or other hardware attacks. Please note that this may cause system errors(especially if your system has a lot of software defined devices).
          </TextBlock>
          <RadioButton Background="black" Name="Firewall_Enabled" Content="Enable Device Firewall" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="30,83.04,0,0" Foreground="#FFD4D4D4"/>
          <RadioButton Background="black" Name="Firewall_Disabled" Content="Disable Device Firewall" HorizontalAlignment="Left" Margin="30,103,0,111.54" Foreground="#FFD4D4D4"/>
          <TextBlock TextWrapping="WrapWithOverflow" Height="87.12" Margin="10,0" VerticalAlignment="Bottom" Foreground="#FFD4D4D4">
            Check Frequency:
            <LineBreak/>
            This is how often the firewall checks for new devices, set this very low if you're afraid of BadUSB or its variants.

          </TextBlock>
          <Slider Minimum=".50" Maximum="300" TickPlacement="BottomRight" TickFrequency="5" IsSnapToTickEnabled="True" Name="Firewall_UpdateFreq" Margin="10,0,75,10" Height="29.96" VerticalAlignment="Bottom" Value="{Binding Count}"/>
          <Label Name="SliderValue_Label" Content="" Height="29.96" Margin="0,0,10,10" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="60" Foreground="#FFD4D4D4"/>
        </Grid>
      </TabItem>
      <TabItem Header="Info" Background="#FF2E2E2E" Margin="-2,0,2,-1" RenderTransformOrigin="0.137,0.501" Height="46" VerticalAlignment="Top" Width="80" >
        <Grid Background="#FF2E2E2E">
          <Viewbox HorizontalAlignment="Left" Height="97.389" Margin="10,10,0,0" VerticalAlignment="Top" Width="100">
            <Image Name="Avatar" Height="100" Width="100" Source="$(Get-ScriptDirectory)\LogoiLab.png"/>
          </Viewbox>
          <Label Name="username" Content="&#169; 2015 LogoiLab" Margin="115,10,10,0" VerticalAlignment="Top" FontSize="24" FontWeight="Bold" FontStyle="Italic" Foreground="#FFD4D4D4"/>
          <Label Name="quote" Content="~ The logos behind the pathos." Margin="115,51.923,10,0" VerticalAlignment="Top" FontWeight="Bold" FontStyle="Italic" Foreground="#FFD4D4D4"/>
          <Button Name="link" Content="http://www.github.com/LogoiLab" VerticalAlignment="Top" Margin="115,79.656,10,0" Cursor="Hand" FontStyle="Oblique"/>
          <WebBrowser Margin="10,115,10,10" Source="http://ha.x10.bz/AutorunDefender/changelog.html"/>
        </Grid>
      </TabItem>
      <TabItem Header="Feedback" Background="#FF2E2E2E" Margin="-2,0,2,-1" RenderTransformOrigin="0.137,0.501" Height="46" VerticalAlignment="Top" Width="80" >
        <Grid Background="#FF2E2E2E">
          <WebBrowser Margin="10,10,10,10" Source="http://goo.gl/forms/B77vvnhLPV"/>
        </Grid>
      </TabItem>
    </TabControl>
    <Button Name="Apply" Content="Apply" HorizontalAlignment="Left" Margin="353,290.5,0,0" VerticalAlignment="Top" Width="150" Height="35.5"/>
    <Label Name="Status_Label" VerticalContentAlignment="Center" Content="" Margin="10,0,170,10" VerticalAlignment="Bottom" Height="35.5" Foreground="White"/>
  </Grid>
</Window>
"@
    $reader=(New-Object System.Xml.XmlNodeReader $xaml) 
    $form=[Windows.Markup.XamlReader]::Load($reader)
    $xaml.SelectNodes("//*[@Name]") | %{Set-Variable -Name ($_.Name) -Value $Form.FindName($_.Name) -Scope Script }
    $Firewall_Enabled.Add_Click({
        $Firewall_Enabled.IsChecked = $True
        $FireWall_Disabled.IsChecked = $False
    })
    $Firewall_Disabled.Add_Click({
        $Firewall_Enabled.IsChecked = $False
        $FireWall_Disabled.IsChecked = $True
    })
    $link.Add_Click({
        Start-Process 'http://www.github.com/LogoiLab'
    })
    $Firewall_UpdateFreq.Add_ValueChanged({
        $SliderValue_Label.Content = ($Firewall_UpdateFreq.value).ToString() + " Sec"
    })
    $Apply.Add_Click({
        $Status_Label.ForeGround = 'yellow'
        $Status_Label.Content = 'Applying Changes...'
        Start-Handler
    })
    $Status_Label.Foreground = 'green'
    $Status_Label.Content = 'Ready...'
    $Form.Add_ContentRendered({
        Initialize-ARD
    })
    #$Form.Add_Loaded({
    #    Initialize-ARD
    #})
    $Form.Add_Closing({
        Deinitialize-Window
    })
    $Form.ShowDialog() | out-null
}

这是一个非常奇怪的错误,我以前从未遇到过,不知怎的,ShowDialog方法正在获取一个永远不应该被解析的字符串(更不用说甚至不允许访问)。< / p>

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

问题在于.NET内存管理错误,运行CCleaner并重启系统。