Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to use mmap function in windows, here is the code
use strict; use warnings; use Fcntl; use Inline C => Config => LIBS => '-lkernel32', BUILD_NOISY => 1, type +maps => 'typemap'; use Inline C => <<'END_OF_C_CODE'; #include <windows.h> #include <winbase.h> void* map_region(int filehandle, DWORD size, DWORD prot){ HANDLE hMap = CreateFileMapping((HANDLE)filehandle, NULL, prot, 0, + size, NULL); LPVOID addr = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, size) +; CloseHandle(hMap); return addr; } int unmap_region(void* addr, DWORD size){ return UnmapViewOfFile(addr); } END_OF_C_CODE my $test_file = 'test_mmap.bin'; sysopen(my $fh, $test_file, O_RDWR|O_CREAT|O_TRUNC) or die "can't open: $!"; binmode $fh; my $size = 4096; my $addr = map_region(fileno($fh), $size, 0x04) or die "mmap failed!!: $^E "; my $test_str = "Hello Windows mmap!"; syswrite($fh, $test_str); unmap_region($addr, $size) or die "fail to unmap";
the code could pass the check, but when I run it, it throws: "mmap failed!!: The handle is invalid at ccc.pl line 29." It seems that the handle I passed from $fh is not correct. My question is what the windows Handle is, and how I get in perl?

Beside: here is the typemap

BOOL T_IV LONG T_IV HANDLE T_IV HWND T_IV HMENU T_IV HKEY T_IV SECURITY_INFORMATION T_UV DWORD T_UV UINT T_UV REGSAM T_UV WPARAM T_UV LPARAM T_IV LPVOID T_UV HANDLE T_UV SIZE_T T_UV DibSect * O_OBJECT ###################################################################### +####### INPUT T_UV $var = ($type)SvUV($arg) ###################################################################### +####### OUTPUT T_UV sv_setuv($arg, (UV)$var);

Replies are listed 'Best First'.
Re: what is exactly HANDL is
by ikegami (Patriarch) on Apr 27, 2025 at 17:01 UTC

    In this case, it's what's returned by CreateFile.

    With MS's C libary, you can use _get_osfhandle to return what you need from a C/unix file descriptor (returned by fileno). I don't know what other C libraries provide, if anything.

      Thanks ikegami, after add _get_osfhandle function, I can get the addr mapping. Before I copy the contents of the address point to to a new SV, Is there a easy way to print the content of the address by perl directly?
      use strict; use warnings; use Fcntl; use Inline C => Config => LIBS => '-lkernel32', BUILD_NOISY => 1, type +maps => 'typemap'; use Inline C => <<'END_OF_C_CODE'; #include <windows.h> #include <winbase.h> #include <io.h> HANDLE get_handle(int fileno){ return _get_osfhandle(fileno); } void* map_region(int filehandle, DWORD size, DWORD prot){ HANDLE hMap = CreateFileMapping((HANDLE)filehandle, NULL, prot, 0, + size, NULL); LPVOID addr = MapViewOfFile(hMap, FILE_MAP_ALL_ACCESS, 0, 0, size) +; CloseHandle(hMap); return addr; } int unmap_region(void* addr, DWORD size){ return UnmapViewOfFile(addr); } END_OF_C_CODE my $test_file = 'test_mmap.bin'; sysopen(my $fh, $test_file, O_RDWR|O_CREAT|O_TRUNC) or die "can't open: $!"; binmode $fh; my $size = 4096; my $addr = map_region(get_handle(fileno($fh)), $size, 0x04) or die "mmap failed!!: $^E "; my $test_str = "Hello Windows mmap!"; syswrite($fh, $test_str); print $addr; #How print the contents without XS? unmap_region($addr, $size) or die "fail to unmap"; sysseek($fh, 0, 0); my $buf; sysread($fh, $buf, length($test_str)); print "validate: ", ($buf eq $test_str ? "success" : "failed"), "\n"; close $fh; unlink $test_file;
Re: what is exactly HANDL is
by pfaut (Priest) on Apr 27, 2025 at 17:11 UTC

    The fileno() return value is a C file descriptor value and the HANDLE is, as ikegami stated, what CreateFile() returns. CreateFile() is a Windows system function. You're mixing two different APIs. Instead of using CreateFileMapping() maybe you could try using mmap().

    90% of every Perl application is already written.
    dragonchild
Re: what is exactly HANDL is
by Fletch (Bishop) on Apr 28, 2025 at 15:47 UTC

    There's already a File::Map on CPAN which says that it works on both Windows and *NIX which may be of use unless you're reimplementing for pedagogical intent.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Linux example of mmap
by bliako (Abbot) on Apr 28, 2025 at 12:16 UTC

    Sorry for OT'ing your post, but I thought an example of mmap on *nix, tested in Linux, could be useful for those lucky linux users:

    use strict; use warnings; use Fcntl; use Inline C => Config => LIBS => '', BUILD_NOISY => 1; use Inline C => <<'END_OF_C_CODE'; #include <sys/mman.h> #include <stdio.h> #include <errno.h> int unmap_region(void *m, int sz){ if( munmap(m, sz) != 0 ){ fprintf(stderr, "unmap_region() : error, call to munmap() has +failed for memory address %p and size %ld : %s\n", m, sz, strerror(er +rno)); return 0; // failed } return 1; // success } void *map_region(int fd, int sz){ char *map = (char *)mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_F +ILE|MAP_SHARED, fd, 0); if (map == MAP_FAILED){ fprintf(stderr, "map has failed for size %ld : %s\n", sz, stre +rror(errno)); return (void *)NULL; // failed } return map; // success } END_OF_C_CODE my $test_file = 'test_mmap.bin'; sysopen(my $fh, $test_file, O_RDWR|O_CREAT|O_TRUNC) or die "can't open file '$test_file' : $!"; binmode $fh; my $size = 4096; my $addr = map_region(fileno($fh), $size) or die "mmap failed!!: $^E "; my $test_str = "Hello Linux mmap!"; syswrite($fh, $test_str); unmap_region($addr, $size) or die "fail to unmap"; close($fh); open($fh, '<', $test_file) or die "failed to open file '$test_file'/2, + $!"; my $contents; { local $/ = undef; $contents = <$fh> } close $fh; print "Sucess, contents of the mmap'ed file '$test_file':\n${contents} +\n--end contents.\n";