Skip to content

Instantly share code, notes, and snippets.

@khchen
Last active November 9, 2021 18:28
Show Gist options
  • Select an option

  • Save khchen/0345b9928aea314c38fe21fe893ef2f8 to your computer and use it in GitHub Desktop.

Select an option

Save khchen/0345b9928aea314c38fe21fe893ef2f8 to your computer and use it in GitHub Desktop.
#[
Author: Ward
Example of CreateFileMapping
References:
https://docs.microsoft.com/en-us/windows/win32/memory/creating-named-shared-memory
]#
# This program may need to run as admin.
import winim/lean
import strformat, terminal # for getch
const BUF_SIZE = 256
block:
let hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
BUF_SIZE, "Global\\MyFileMappingObject")
if hMapFile == 0:
echo fmt"Could not create file mapping object ({GetLastError()})."
quit()
defer:
CloseHandle(hMapFile)
let pBuf = cast[LPSTR](MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, BUF_SIZE))
if pBuf.isNil:
echo fmt"Could not map view of file ({GetLastError()})."
quit()
defer:
UnmapViewOfFile(pBuf)
# https://khchen.github.io/winim/winstr.html#%3C%3C.t%2CA%2CB
# Fill operator for SomeBuffer and SomeString.
# Please make sure both side have the same character size.
# If destination don't have the length information (e.g. pointer or UncheckedArray),
# please make sure the buffer size is large enough.
pBuf << "Message from first process."
discard getch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment