Thanks to visit codestin.com
Credit goes to github.com

Skip to content

System.IntPtr F# snippets #7754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions snippets/fsharp/System/IntPtr/Add/add1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// <Snippet1>
#nowarn "9"
open System
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
let mutable arr =
[| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]

use parr = fixed arr

let ptr = NativePtr.toNativeInt parr

// Get the size of an array element.
let size = sizeof<int>
for i = 0 to arr.Length - 1 do
let newPtr = IntPtr.Add(ptr, i * size)
printf $"{Marshal.ReadInt32 newPtr} "
0

// The example displays the following output:
// 2 4 6 8 10 12 14 16 18 20
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IntPtr/Add/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="add1.fs" />
</ItemGroup>
</Project>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IntPtr/Overview/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="topointer.fs" />
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions snippets/fsharp/System/IntPtr/Overview/topointer.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//<snippet1>
#nowarn "9"
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
let stringA = "I seem to be turned around!"
let mutable copylen = stringA.Length

// Allocate HGlobal memory for source and destination strings
let sptr = Marshal.StringToHGlobalAnsi stringA
let dptr = Marshal.AllocHGlobal(copylen + 1)

let mutable src: byte nativeptr = sptr.ToPointer() |> NativePtr.ofVoidPtr
let mutable dst: byte nativeptr = dptr.ToPointer() |> NativePtr.ofVoidPtr

if copylen > 0 then
// set the source pointer to the end of the string
// to do a reverse copy.
src <-
NativePtr.toNativeInt src + nativeint (copylen - 1)
|> NativePtr.ofNativeInt

while copylen > 0 do
copylen <- copylen - 1
NativePtr.read src |> NativePtr.write dst
dst <- NativePtr.toNativeInt dst + 1n |> NativePtr.ofNativeInt
src <- NativePtr.toNativeInt src - 1n |> NativePtr.ofNativeInt
NativePtr.write dst 0uy

let stringB = Marshal.PtrToStringAnsi dptr

printfn $"Original:\n{stringA}\n"
printfn $"Reversed:\n{stringB}"

// Free HGlobal memory
Marshal.FreeHGlobal dptr
Marshal.FreeHGlobal sptr
0

// The progam has the following output:
//
// Original:
// I seem to be turned around!
//
// Reversed:
// !dnuora denrut eb ot mees I
//</snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IntPtr/Subtract/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="subtract1.fs" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions snippets/fsharp/System/IntPtr/Subtract/subtract1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// <Snippet1>
#nowarn "9"
open System
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
let arr =
[| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]

// Get the size of a single array element.
let size = sizeof<int>

use pend = fixed &arr[arr.GetUpperBound 0]
let ptr = NativePtr.toNativeInt pend
for i = 0 to arr.Length - 1 do
let newPtr = IntPtr.Subtract(ptr, i * size)
printf $"{Marshal.ReadInt32 newPtr} "
0

// The example displays the following output:
// 20 18 16 14 12 10 8 6 4 2
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IntPtr/ToPointer/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="topointer.fs" />
</ItemGroup>
</Project>
49 changes: 49 additions & 0 deletions snippets/fsharp/System/IntPtr/ToPointer/topointer.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//<snippet1>
#nowarn "9"
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
let stringA = "I seem to be turned around!"
let mutable copylen = stringA.Length

// Allocate HGlobal memory for source and destination strings
let sptr = Marshal.StringToHGlobalAnsi stringA
let dptr = Marshal.AllocHGlobal(copylen + 1)

let mutable src: byte nativeptr = sptr.ToPointer() |> NativePtr.ofVoidPtr
let mutable dst: byte nativeptr = dptr.ToPointer() |> NativePtr.ofVoidPtr

if copylen > 0 then
// set the source pointer to the end of the string
// to do a reverse copy.
src <-
NativePtr.toNativeInt src + nativeint (copylen - 1)
|> NativePtr.ofNativeInt

while copylen > 0 do
copylen <- copylen - 1
NativePtr.read src |> NativePtr.write dst
dst <- NativePtr.toNativeInt dst + 1n |> NativePtr.ofNativeInt
src <- NativePtr.toNativeInt src - 1n |> NativePtr.ofNativeInt
NativePtr.write dst 0uy

let stringB = Marshal.PtrToStringAnsi dptr

printfn $"Original:\n{stringA}\n"
printfn $"Reversed:\n{stringB}"

// Free HGlobal memory
Marshal.FreeHGlobal dptr
Marshal.FreeHGlobal sptr
0

// The progam has the following output:
//
// Original:
// I seem to be turned around!
//
// Reversed:
// !dnuora denrut eb ot mees I
//</snippet1>
11 changes: 11 additions & 0 deletions snippets/fsharp/System/IntPtr/Zero/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="zero2.fs" />
<Compile Include="zero4.fs" />
</ItemGroup>
</Project>
19 changes: 19 additions & 0 deletions snippets/fsharp/System/IntPtr/Zero/zero2.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module zero2

// <Snippet1>
open System
open System.Runtime.InteropServices

let GW_OWNER = 4

[<DllImport("user32", CharSet=CharSet.Auto, SetLastError=true, ExactSpelling=true)>]
extern IntPtr GetWindow(nativeint hwnd, int wFlag)

let hwnd = IntPtr 3
let hOwner = GetWindow(hwnd, GW_OWNER)
if hOwner = IntPtr.Zero then
printfn "Window not found."

// The example displays the following output:
// Window not found.
// </Snippet1>
33 changes: 33 additions & 0 deletions snippets/fsharp/System/IntPtr/Zero/zero4.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// <Snippet2>
open Microsoft.Win32.SafeHandles
open System
open System.Runtime.InteropServices

let GENERIC_READ = 0x80000000u
let OPEN_EXISTING = 3u
let FILE_ATTRIBUTE_NORMAL = 128u
let FILE_FLAG_OVERLAPPED = 0x40000000u

[<DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)>]
extern SafeFileHandle CreateFile(
string lpFileName, uint dwDesiredAccess, uint dwShareMode,
nativeint pSecurityAttributes, uint dwCreationDisposition,
uint dwFlagsAndAttributes, nativeint hTemplateFile)

let hnd =
CreateFile("CallOfTheWild.txt", GENERIC_READ, 0u,
IntPtr.Zero, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL ||| FILE_FLAG_OVERLAPPED,
IntPtr.Zero)

if hnd.IsInvalid then
let ex = Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error())
printfn $"Attempt to open file failed:\n {ex.Message}"
else
printfn "File successfully opened."
hnd.Close()

// If the file cannot be found, the example displays the following output:
// Attempt to open file failed:
// The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
// </Snippet2>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/IntPtr/op_Addition/addition1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <Snippet1>
#nowarn "9"
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
let arr =
[| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]

use parr = fixed arr

let ptr = NativePtr.toNativeInt parr

for i = 0 to arr.Length - 1 do
let newPtr = ptr + nativeint i * nativeint sizeof<int>
printf $"{Marshal.ReadInt32 newPtr} "
0

// The example displays the following output:
// 2 4 6 8 10 12 14 16 18 20
// </Snippet1>
10 changes: 10 additions & 0 deletions snippets/fsharp/System/IntPtr/op_Addition/fs.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="addition1.fs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="..\op_subtraction1.fs" />
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions snippets/fsharp/System/IntPtr/op_Addition/op_subtraction1.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <Snippet2>
#nowarn "9"
open System.Runtime.InteropServices
open FSharp.NativeInterop

[<EntryPoint>]
let main _ =
let arr =
[| 2; 4; 6; 8; 10; 12; 14; 16; 18; 20 |]

use parr = fixed &arr[arr.GetUpperBound 0]

let ptr = NativePtr.toNativeInt parr

for i = 0 to arr.GetUpperBound 0 do
let newPtr = ptr - nativeint i * nativeint sizeof<int>
printf $"{Marshal.ReadInt32 newPtr} "
0

// The example displays the following output:
// 20 18 16 14 12 10 8 6 4 2
// </Snippet2>
Loading