tablacus / TablacusScriptControl

Script Control for 64-bit platforms
https://tablacus.github.io/scriptcontrol_en.html
MIT License
62 stars 17 forks source link

Newly-instantiated object causes PowerShell to crash #19

Closed TarquinQ closed 4 years ago

TarquinQ commented 4 years ago

Hi Tab,

The ScriptControl object causeses PowerShell to crash (due to a memory read/write error) if the object is accessed directly after being first instantiated. It works fine when actually using the object, but there is clearly an initialisation problem of some form. This can be easily replicated as per below.

Details:

Component Version
OS: Windows 10 x64 v1909 (and 1809)
PowerShell: v5.1 x64
TSC: v1.2.2.0

Steps to Replicate:

First, the working case, x86:

cmd /c C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\User>$sc = New-Object -ComObject 'ScriptControl'; $sc
Language      :
State         :
SitehWnd      : 0
Timeout       : 10000
AllowUI       : True
UseSafeSubset : False
Modules       :
Error         : System.__ComObject
CodeObject    :
Procedures    :
PS C:\Users\User>

Then the x64 version:

cmd /c C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

PS C:\Users\User>$sc = New-Object -ComObject 'ScriptControl'; $sc
Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.Management.Automation.IDispatch.Invoke(Int32 dispIdMember, Guid iid, Int32 lcid, INVOKEKIND wFlags, DISPPARAMS[] paramArray, Object& pVarResult, EXCEPINFO& pExcepInfo, UInt32& puArgErr)
   at System.Management.Automation.ComInvoker.Invoke(IDispatch target, Int32 dispId, Object[] args, Boolean[] byRef, INVOKEKIND invokeKind)
   at System.Management.Automation.ComProperty.GetValue(Object target)
   at System.Management.Automation.Adapter.BasePropertyGet(PSProperty property)
   at System.Management.Automation.PSProperty.get_Value()
   at Microsoft.PowerShell.Commands.Internal.Format.MshExpression.GetValue(PSObject target, Boolean eatExceptions)
<... snip: StackTrace continues for dozens of lines ...>
# PowerShell now est plutôt mort,  X-(

Interestingly, Getting or Setting any of the Properties on the object then allows it to be accessed correctly. See x64 again (restarted after the above crash, obviously ;) :

PS C:\Users\User>$sc = New-Object -ComObject 'ScriptControl'; $sc.Language = "food"; $sc
<snip: Successful empty Object output here>
PS C:\Users\User>$sc = New-Object -ComObject 'ScriptControl'; $sc.AllowUI = $False; $sc
<snip: Successful empty Object output here>

# And this one really amazes me:
PS C:\Users\User>$sc = New-Object -ComObject 'ScriptControl'; $Null -eq $sc.Language; $sc
True
<snip: Successful empty Object output here>

Can you please have a look.

Kindest thanks, T

tablacus commented 4 years ago

I have adjusted it on Tablacus Script Control 64 1.2.3.0. Give it a try.

Regards,

TarquinQ commented 4 years ago

Excellent, that fixed it, thanks. (That was also a super-quick response from you; sorry I didn't see it until today!).