How to use AppDomainInitializer in Powershell

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Don Jones Don Jones 5 months, 2 weeks ago.

  • Author
    Posts
  • #43858
    Profile photo of René Vajkay
    René Vajkay
    Participant

    Hi guys,

    I am trying to create an AppDomain in a Powershell script. I implemented the exact same in C# and it seems to be working fine. However, the Powershell version always fails with an exception. The script code is:

    function Execute($arguments)
    {
        Write-Host "Arguments: " $arguments[0] $arguments[1] $arguments[2]
    }
    
    function Main
    {
        $appDomain = $null
        Try
        {
            $appDomainSetup = New-Object -TypeName System.AppDomainSetup
            $appDomainSetup.AppDomainInitializer = ${function:Execute}
            $appDomainSetup.AppDomainInitializerArguments = @("Test1", "Test2", "Test3")
    
            $appDomain = [AppDomain]::CreateDomain("TestDomain", $null, $appDomainSetup)
        }
        Finally
        {
            If ($appDomain -ne $null)
            {
                [AppDomain]::Unload($appDomain)
            }
        }
    }
    
    Main
    

    And the exception is: Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at System.AppDomain.nCreateDomain(String friendlyName, AppDomainSetup setup, Evidence providedSecurityInfo, Evidence creatorsSecurityInfo, IntPtr parentSecur ityDescriptor) ...

    We figured out that Powershell is creating a dynamic method from the delegate in AppDomainSetup. But I don't know to change this behavior or any other way how I can use AppDomainInitializer in Powershell?

    This question has been originally asked on http://stackoverflow.com/questions/37896034/how-to-use-appdomaininitializer-in-powershell.

  • #44694
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Could you just use embedded C#?

You must be logged in to reply to this topic.