Strange CLR error

Started by stanl, May 07, 2014, 05:28:01 AM

Previous topic - Next topic

stanl

This line of code, produced the attached error.

Code (WINBATCH) Select

oWin = ObjectClrNew('System.Windows.Markup.XamlReader')

Deana

Please post the ObjectClrOption ("use"..) statement that you are calling to load the assembly.

WinBatch relies on the CLR hosting's reflection functionality to instantiate classes.  There are a  few classes attributed to block instantiation via reflection.




Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

sorry about that:  I coded (and tested by commenting one or more ClrOptions out). At this point oReader and creader have no issues).

Code (WINBATCH) Select

ObjectClrOption("use","System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
ObjectClrOption("use","System.Windows, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
ObjectClrOption("use","System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
ObjectClrOption("use","System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
ObjectClrOption("use","System.Xml.ReaderWriter, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
ObjectClrOption("use","System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

oReader = ObjectClrNew('System.Xml.XmlReader')
xaml=getxaml()
cReader = ObjectClrNew('System.IO.StringReader',xaml)
oReader.Create(cReader)

oWin = ObjectClrNew('System.Windows.Markup.XamlReader')


stanl

Oh, and if it helps, I am trying to redo a .ps1 in WB


Ã,¿$xaml = @"
<Window
xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>

<Border BorderThickness="20" BorderBrush="Yellow" CornerRadius="9" Background='Red'>
  <StackPanel>
   <Label FontSize="50" FontFamily='Stencil' Background='Red' Foreground='White' BorderThickness='0'>
    System will be rebooted in 15 minutes!
   </Label>

   <Label HorizontalAlignment="Center" FontSize="15" FontFamily='Consolas' Background='Red' Foreground='White' BorderThickness='0'>
    Worried about losing data? Talk to your friendly help desk representative and freely share your concerns!
   </Label>
  </StackPanel>
</Border>
</Window>
"@

$reader = [System.XML.XMLReader]::Create([System.IO.StringReader] $xaml)
$window = [System.Windows.Markup.XAMLReader]::Load($reader)
$Window.AllowsTransparency = $True
$window.SizeToContent = 'WidthAndHeight'
$window.ResizeMode = 'NoResize'
$Window.Opacity = .7
$window.Topmost = $true
$window.WindowStartupLocation = 'CenterScreen'
$window.WindowStyle = 'None'
# show message for 5 seconds:
$null = $window.Show()
Start-Sleep -Seconds 5
$window.Close()


Deana

The System.Windows.Markup.XamlReader Documention (http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader(v=vs.110).aspx) states that the required assembly to load is PresentationFramework.dll.

The converted code would look something like this in WinBatch:

Code (winbatch) Select
;***************************************************************************
;**   Reads XAML input and creates a Window, using the WPF default XAML reader
;**
;** Purpose: Display a Window using the Windows Presentation Foundation
;** Inputs:
;** Outputs: Results in WPF Window
;** Reference:
;**       REQUIRES WinBatch 2013A or newer
;**
;** Developer: Deana Falk 2014.05.07
;***************************************************************************
If Version( )< '2013A'
   Pause('Notice', 'Need 2013A or Newer Version of WinBatch')
   Exit
EndIf

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;  Define non WIL Types
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_True = ObjectType( 'BOOL', 1 )
_False = ObjectType( 'BOOL', 0 )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Load assemblies into the WinBatch process.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;The System.Windows.Markup.XamlReader documention states that the required assembly to load is PresentationFramework.dll.(http://msdn.microsoft.com/en-us/library/system.windows.markup.xamlreader(v=vs.110).aspx)
ObjectClrOption('appbase','C:\Windows\Microsoft.NET\Framework\v4.0.30319\WPF\') ; 'PresentationFramework.dll'
ObjectClrOption("use","PresentationFramework")
ObjectClrOption("use","PresentationCore")

ObjectClrOption("use","System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;  Enumerations
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
enumSizeToContent = ObjectClrNew( 'System.Windows.SizeToContent' )
enumResizeMode = ObjectClrNew( 'System.Windows.ResizeMode' )
enumWindowStartupLocation = ObjectClrNew( 'System.Windows.WindowStartupLocation' )
enumWindowStyle = ObjectClrNew( 'System.Windows.WindowStyle' )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Create classes implemented by a managed assembly.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

;Read Xaml
oReader = ObjectClrNew('System.Xml.XmlReader')
xaml=`<Window xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Border BorderThickness="20" BorderBrush="LightBlue" CornerRadius="9" Background='Blue'><StackPanel><Label FontSize="50" FontFamily='Stencil' Background='Blue' Foreground='White' BorderThickness='0'>WinBatch and dotNet Rocks!</Label><Label HorizontalAlignment="Center" FontSize="15" FontFamily='Consolas' Background='Red' Foreground='White' BorderThickness='0'>Visit us a http://www.winbatch.com!</Label></StackPanel></Border></Window>`
cReader = ObjectClrNew('System.IO.StringReader',xaml)
oXmlReader = oReader.Create(cReader)

;Load Xaml
oXamlReader = ObjectClrNew('System.Windows.Markup.XamlReader')
oWin = oXamlReader.Load(oXmlReader)
oWin = ObjectClrType( 'System.Windows.Window', oWin )  ;Associate a Framework based type name with a value
oWin.AllowsTransparency = _True
oWin.SizeToContent = ObjectClrType('System.Windows.SizeToContent',enumSizeToContent.WidthAndHeight)
oWin.ResizeMode = ObjectClrType('System.Windows.ResizeMode', enumResizeMode.NoResize )
oWin.Opacity = .7
oWin.Topmost = _True
oWin.WindowStartupLocation = ObjectClrType('System.Windows.WindowStartupLocation',enumWindowStartupLocation.CenterScreen) 
oWin.WindowStyle = ObjectClrType('System.Windows.WindowStyle',enumWindowStyle.None) 

;Show message for 5 seconds:
oWin.Show()
TimeDelay(5)
oWin.Close()
Exit



Deana F.
Technical Support
Wilson WindowWare Inc.

stanl

I guess the operative expression is "I feel a little less dumb". I like the Xaml and WPF stuff. In fact, I read it has a Grid Class that might be useful to WB.

Thanks, as usual. One final question. This stuff does seem to require .NET 4.0/4.5. So, in addition to the check in the script for WB 2013A>,  might make sense to put in a check4netversion() udf? What do you think?

Deana

Here is some introductory UDFs to determine dotNet versions installed.

Code (winbatch) Select
;***************************************************************************
;**            Installed dotNet Version Details
;**
;** Queries the information from the registry
;**
;** Currently supported version values = 4.5, 4.0, 3.5, 3.0, 2.0.50727, 1.1.4322 
;**
;** Developer: Deana Falk 2014.05.09
;***************************************************************************

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;        udfdotNetGetVersionInfo( nVersion, sOther )
;        Gets the installed versions, service packs and other from registry
;
; Parameters:      nVersion: Version number to check
;                  sOther: Specifies registry subkey: i.e. "client" of "full".
; Return Value:    delimited list containing (Version|Service Pack|Other) or "" if not installed.
;
; Note: Does not currently handle 1.0 versions, because that can get a bit complicated.
; http://blogs.msdn.com/b/deva/archive/2010/12/10/how-to-determine-which-microsoft-net-framework-version-and-service-pack-installed.aspx

; Currently supported version values = 4.5, 4.0, 3.5, 3.0, 2.0.50727, 1.1.4322   
;                     
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udfdotNetGetVersionInfo(sVersion, sOther)

   If sVersion > 4.5
       Pause('udfdotNetVersionsExist','This function does not currently support checking this dotNet Version.')
       Return 0
   Endif
   sSubkey = ""
   sSPDataItem = ""
   sSp = ""
   keyhandle = @REGMACHINE
   If WinMetrics(-7)|WinMetrics(-2)==3
      RegOpenFlags( 64 )
Else
      RegOpenFlags( 32 )
   Endif
   
   ;https://its-knowledge01.campus.ad.csulb.edu/display/help/How+to+check+the+version+of+.NET+that's+Installed
Switch @TRUE
Case sVersion == "4.5"
      Case sVersion == "4.0"
sSubkey =  "SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\" : sOther
         sSPDataItem = "Servicing"
         break
Case sVersion == "3.5"
sSubkey =  "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.5"
         sSPDataItem = "SP"
         break
Case sVersion == "3.0"
sSubkey =  "SOFTWARE\Microsoft\NET Framework Setup\NDP\v3.0"
         sSPDataItem = "SP"
         break
Case sVersion == "2.0.50727"
sSubkey =  "SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727"
         sSPDataItem = "SP"
         break
Case sVersion == "1.1.4322"
sSubkey = "SOFTWARE\Microsoft\NET Framework Setup\NDP\v1.1.4322"
         sSPDataItem = "SP"
         break
EndSwitch
   
   If RegExistValue( keyhandle, sSubkey:'[Version]' )
      sVersion = RegQueryValue( keyhandle, sSubkey:'[Version]' )
   Endif

   If RegExistValue( keyhandle, sSubkey:'[':sSPDataItem:']')
   sSP = RegQueryDWord( keyhandle, sSubkey:'[':sSPDataItem:']' )   
   Endif

   sResult = sVersion:"|":sSP:"|":sOther
   If sResult == "||" then sResult = ""
Return sResult
#EndFunction

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;        udfdotNetVersionsList()
;        Returns a list of all the installed versions, service packs, and other
;
; Parameter:       None.
; Return Value:    Tab delimited list containing (Version|Service Pack|Other) or "" if not installed.
;
;                   
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udfdotNetVersionsList()
   sList = ''
   sDelim = '|'
   
sValue = udfdotNetGetVersionInfo("4.5", "Full")
sList = sList : sValue :  @TAB

sValue = udfdotNetGetVersionInfo("4.5", "Client")
sList = sList : sValue :  @TAB

sValue = udfdotNetGetVersionInfo("4.0", "Full")
   sList = sList : sValue :  @TAB

sValue = udfdotNetGetVersionInfo("4.0", "Client")
sList = sList : sValue :  @TAB

sValue = udfdotNetGetVersionInfo("3.5", "")
sList = sList : sValue :  @TAB

sValue = udfdotNetGetVersionInfo("3.0", "")
sList = sList : sValue :  @TAB

sValue = udfdotNetGetVersionInfo("2.0.50727", "")
sList = sList : sValue :  @TAB

sValue = udfdotNetGetVersionInfo("1.1.4322", "")
sList = sList : sValue

Return sList
#EndFunction 


;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;        udfdotNetVersionsExist(sVersion)
;        Determines if a specific version of dotNet is installed.
;
; Parameter:       sVersion. Supported values = 4.5, 4.0, 3.5, 3.0, 2.0.50727, 1.1.4322
; Return Value:    @True if installed; @False otherwise.
;                       
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udfdotNetVersionsExist(sVersion)
   If sVersion > 4.5
       Pause('udfdotNetVersionsExist','This function does not currently support checking this dotNet Version.')
       Return 0
   Endif
   If sVersion == "4.0" || sVersion == "4.5"
      sValue = udfdotNetGetVersionInfo(sVersion, "Full")
   Else
   sValue = udfdotNetGetVersionInfo(sVersion, "")
   Endif
   If sValue == "" then bResult = 0
   Else bResult = 1
Return bResult
#EndFunction 

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;
;        udfdotNetLastestVersion()
;        Gets the latest version of dotNet that is installed.
;
; Parameter:       None
; Return Value:    delimited list containing (Version|Service Pack|Other) of the latest version of dotNet installed.
;                       
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#DefineFunction udfdotNetLastestVersion()
   lastversion = ItemExtract( 1, udfdotNetVersionsList(), @tab )
Return lastversion
#EndFunction 

;***************************************************************************
;***************************************************************************
;***************************************************************************

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Check a specific dotNet version exists
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
sVer = "4.0"
sRet = udfdotNetVersionsExist(sVer)
If sRet
   Pause('dotNet Version ':sVer,'Is Installed.')
Else
   Pause('dotNet Version ':sVer,'Not Installed.')
Endif

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; List all installed dotNet Versions
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
strList = udfdotNetVersionsList()
AskItemList( 'dotNet Installed Version Details', strList, @tab, @unsorted, @single )

;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Determine Latest dotNet Version installed
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
lastversion = udfdotNetLastestVersion()
Pause('Latest installed dotNet version details',lastversion)
Exit

Deana F.
Technical Support
Wilson WindowWare Inc.

kdmoyers

Quote from: Deana on May 09, 2014, 11:40:45 AM
Here is some introductory UDFs to determine dotNet versions installed.
awesome. instantly useful.
-Kirby
The mind is everything; What you think, you become.