Dangling COM objects
No Excel processes are currently running;
get-process -name excel -ea ignore
Create an Excel process using COM:
$xls = new-object -com excel.application
$xls.visible = $true
get-process
reports the Excel process:
get-process -name excel -ea ignore
Quit Excel:
$xls.Quit()
Even though Excel has become invisible, the process is still running:
get-process -name excel -ea ignore
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($xls)
Verify process is gone:
get-process -name excel -ea ignore
Returned object type
Typically,
new-object -com …
returns a
System.__ComObject
object. In special cases however, it returns an «interop» object that wraps the COM Object.
This behaviour is demonstrated in the following simple example.
First, we create two COM objects …
PS C:\> $fso = new-object -com scripting.filesystemobject
PS C:\> $xls = new-object -com excel.application
… and then determine their types:
PS C:\> $fso.GetType().FullName
System.__ComObject
PS C:\> $xls.GetType().FullName
Microsoft.Office.Interop.Excel.ApplicationClass
$null = new-psDrive -name HKCR -psProvider registry -root HKEY_CLASSES_ROOT
$clsid_xls = get-itemPropertyValue -name '(default)' HKCR:\excel.application\CLSID
$clsid_fso = get-itemPropertyValue -name '(default)' HKCR:\scripting.filesystemobject\CLSID
write-host "CLSIDs: xls: $clsid_xls, fso: $clsid_fso"
CLSIDs: xls: {00024500-0000-0000-C000-000000000046}, fso: {0D43FE01-F093-11CF-8940-00A0C9054228}
If we look up these keys in
regedit.exe
, we find some differences.
In-proc server for file system object
In the case of the file system object, «only» a
DLL is registred:
In-proc server for Excel
However, for the
Excel object, an
assembly and a (default?) class is registered: