0

I would like to use [System.IO.MemoryMappedFiles] on PowerShell-7 with .NetFramework 4.8 (Release 528372) running on Windows 10 20H2;

Name                           Value
----                           -----
PSVersion                      7.1.0
PSEdition                      Core
GitCommitId                    7.1.0
OS                             Microsoft Windows 10.0.19042
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

and trying to load the DLL into PowerShell-7 console. My goal is to create a view of a large binary file using [System.IO.MemoryMappedFiles]::CreateFromFile().

Below is based on the "Example 3" and "Example 2" at Add-Type and seeing answers to similar questions at SO like Unable to use System.IO.Compression.FileSystem.dll

xSet-Location $PSHOME
$AccType = Add-Type -AssemblyName "System.IO.MemoryMappedFiles" -PassThru
[System.IO.MemoryMappedFiles] | Get-Member

However, on a PowerShell 7 (x64) console, the Get-Member returns "Unable to find type" error as below.

InvalidOperation: C:\.......\PowerShell\MyTest-MemoryMappedFiles.ps1:3
Line |
   3 |  [System.IO.MemoryMappedFiles] | Get-Member
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Unable to find type [System.IO.MemoryMappedFiles].

When I run $AccType = Add-Type -AssemblyName "System.IO.MemoryMappedFiles" -PassThru, the $AccType contains below,

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
False    False    EmbeddedAttribute                        System.Attribute
False    False    NullableAttribute                        System.Attribute
False    False    NullableContextAttribute                 System.Attribute
False    False    NullablePublicOnlyAttribute              System.Attribute
False    False    Interop                                  System.Object
False    False    Kernel32                                 System.Object
False    False    SECURITY_ATTRIBUTES                      System.ValueType
False    False    SYSTEM_INFO                              System.ValueType
False    False    MEMORY_BASIC_INFORMATION                 System.ValueType
False    False    MEMORYSTATUSEX                           System.ValueType
False    True     BOOL                                     System.Enum
False    False    SR                                       System.Object
False    False    SR                                       System.Object
False    False    Win32Marshal                             System.Object
True     True     MemoryMappedFileOptions                  System.Enum
True     True     MemoryMappedFileAccess                   System.Enum
True     False    MemoryMappedFile                         System.Object
False    False    MemoryMappedView                         System.Object
True     False    MemoryMappedViewAccessor                 System.IO.UnmanagedMemoryAccessor
True     False    MemoryMappedViewStream                   System.IO.UnmanagedMemoryStream
True     True     MemoryMappedFileRights                   System.Enum
True     False    SafeMemoryMappedViewHandle               System.Runtime.InteropServices.SafeBuffer
True     False    SafeMemoryMappedFileHandle               Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid

After running the Add-Type command, when I enter

[System.IO.Memory

then enter TAB key in the PS7 console,

[System.IO.MemoryMappedFiles

is automatically completed, But when I enter (Please ignore the heading double quote below.)

" [System.IO.MemoryMappedFiles]::

and enter TAB key, nothing is completed. The pre-loaded standard library like

"[System.IO.File]::

completes the methods

[System.IO.File]::AppendAllLine(

with entering TAB key.

kaz
  • 1
  • 1
  • my understanding is that ps7 requires dotnet core ... not the dotnet framework. i seriously doubt that you can force ps7 to load that dll. – Lee_Dailey Dec 08 '20 at 04:29
  • Thank you @Lee_Dailey for your comment. I've now installed "dotnet-sdk-5.0.100-win-x64.exe" and tried to load the DLL directly by"Add-Type -Path "C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0\ref\net5.0\System.IO.MemoryMappedFiles.dll", and "[System.IO.MemoryMappedFiles] | Get-Member" returns the same result as "InvalidOperation: Unable to find type [System.IO.MemoryMappedFiles]. – kaz Dec 08 '20 at 05:24
  • have you checked to see if that class exists in the new dotnetcore? not all classes are supported yet. you may not be able to use ps7 to access that class at this time. ///// it may be worth your while to check the github site for dotnetcore to see when or if that class will be added. – Lee_Dailey Dec 08 '20 at 06:30
  • @Lee_Dailey. I suppose MemoryMappedFiles is already supported in .Net3.1 and .Net5.0 because when I create a new C# Class Library project (.Net Core) in VS2019, I can see the path of "C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\3.1.0\ref\netcoreapp3.1\System.IO.MemoryMappedFiles.dll" as default at (ProjectName) - Dependencies - Frameworks - Microsoft.NETCore.App, and also find "C:\Program Files\dotnet\packs\Microsoft.NETCore.App.Ref\5.0.0\ref\net5.0\System.IO.MemoryMappedFiles.dll" when changing the Target framework to ".Net 5.0". – kaz Dec 08 '20 at 08:18
  • i'm well beyond my skill level at this point. you may want to think about other tags for this Question. perhaps `DotNetCore` or some variant? – Lee_Dailey Dec 08 '20 at 08:34
  • 1
    `MemoryMappedFiles` is a _namespace_, not a _type_ - it won't have any static members you can autocomplete or invoke with `::` - did you mean `[System.IO.MemoryMappedFiles.MemoryMappedFile]::`? – Mathias R. Jessen Dec 08 '20 at 12:09
  • Thank you @Mathias R. Jessen for your reply. That looks correct. Now "[System.IO.MemoryMappedFiles.MemoryMappedFile] | gm" returns the members. Thank you so much! – kaz Dec 08 '20 at 15:22

0 Answers0