Skip to content

Instantly share code, notes, and snippets.

@khchen
Last active November 9, 2021 18:28
Show Gist options
  • Save khchen/8414329fed2d0c2d32176247a2819e28 to your computer and use it in GitHub Desktop.
Save khchen/8414329fed2d0c2d32176247a2819e28 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
const BUF_SIZE = 256
block:
let hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE,
"Global\\MyFileMappingObject")
if hMapFile == 0:
echo fmt"Could not open 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()
# pBuf is LPSTR (pointer to multibyte character string) here,
# convert it to nim string before pass into MessageBox.
MessageBox(0, $pBuf, "Process2", MB_OK)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment