Last active
March 6, 2024 09:09
-
-
Save Midoliy/283b84c377ec9af225f81ba48c775b9a to your computer and use it in GitHub Desktop.
代替データストリームのサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#load "NativeMethod.fsx" | |
open System | |
open System.IO | |
open System.Text | |
open NativeMethod | |
let nullptr = IntPtr.Zero | |
let write (str:string) handle = | |
let bytes = (Encoding.GetEncoding("Shift_JIS")).GetBytes(str.ToCharArray()) | |
let length = Array.length >> uint32 | |
let mutable size = 0u | |
NativeMethod.writeFile(handle, bytes, length bytes, &size, nullptr) | |
let read handle = | |
let mutable size = 0 | |
NativeMethod.getFileSize(handle, &size) |> ignore | |
let mutable buf = Array.zeroCreate (int size) | |
let mutable readsize = 0u | |
NativeMethod.readFile(handle, buf, uint32 size, &readsize, nullptr) |> ignore | |
(Encoding.GetEncoding("Shift_JIS")).GetString(buf) | |
// 書き込みサンプル | |
let h = NativeMethod.createFile("sample.txt:foo", FileAccess.ReadWrite, FileShare.Read, nullptr, FileMode.OpenOrCreate, FileAttributes.Normal, nullptr) | |
if h <> nullptr then | |
printfn "=== Write ===" | |
write "ほげほげ" h |> ignore | |
NativeMethod.closeHandle(h) |> ignore | |
else | |
printfn "Failed" | |
// 読み込みサンプル | |
let h' = NativeMethod.createFile("sample.txt:foo", FileAccess.ReadWrite, FileShare.Read, nullptr, FileMode.OpenOrCreate, FileAttributes.Normal, nullptr) | |
if h' <> nullptr then | |
printfn "=== Read ===" | |
read h' |> printfn "read value= %s" | |
NativeMethod.closeHandle(h') |> ignore | |
else | |
printfn "Failed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module NativeMethod = | |
open System | |
open System.IO | |
open System.Runtime.ConstrainedExecution | |
open System.Runtime.InteropServices | |
open System.Security | |
open System.Text | |
[<DllImport("kernel32.dll", CharSet=CharSet.Unicode, SetLastError=true, EntryPoint="CreateFileW");CompiledName("CreateFileW")>] | |
extern nativeint createFile ( | |
[<MarshalAs(UnmanagedType.LPWStr); In>] string filename, | |
[<MarshalAs(UnmanagedType.U4)>] FileAccess access, | |
[<MarshalAs(UnmanagedType.U4)>] FileShare share, | |
nativeint securityAttributes, | |
[<MarshalAs(UnmanagedType.U4)>] FileMode creationDsiposition, | |
[<MarshalAs(UnmanagedType.U4)>] FileAttributes flagsAndAttributes, | |
nativeint templateFile ) | |
[<DllImport("kernel32.dll", SetLastError=true, EntryPoint="CloseHandle");CompiledName("CloseHandle")>] | |
[<ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success);SuppressUnmanagedCodeSecurity>] | |
extern [<MarshalAs(UnmanagedType.Bool)>] bool closeHandle(nativeint handle) | |
[<DllImport("kernel32.dll", BestFitMapping=true, CharSet=CharSet.Ansi);CompiledName("WriteFile")>] | |
extern [<MarshalAs(UnmanagedType.Bool)>] bool writeFile( | |
nativeint filehandle, | |
byte[] buffer, | |
uint32 numberOfBytesToWrite, | |
uint32& numberOfBytesWritten, | |
nativeint overlapped ) | |
[<DllImport("kernel32.dll", SetLastError=true, EntryPoint="ReadFile");CompiledName("ReadFile")>] | |
extern [<MarshalAs(UnmanagedType.Bool)>] bool readFile( | |
nativeint filehandle, | |
[<Out>] byte[] buffer, | |
uint32 numberOfBytesToRead, | |
uint32& numberOfBytesRead, | |
nativeint overlapped ) | |
[<DllImport("kernel32.dll", EntryPoint="GetFileSizeEx");CompiledName("GetFileSize")>] | |
extern [<MarshalAs(UnmanagedType.Bool)>] bool getFileSize( | |
nativeint filehandle, | |
[<Out>] int32& filesize); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment