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;
|