fixes for win32

This commit is contained in:
Andrea Fioraldi 2021-03-18 16:38:04 +01:00
parent acdc70b0a6
commit c0b3d25ae2
3 changed files with 21 additions and 13 deletions

View File

@ -1,7 +1,7 @@
fn main() { fn main() {
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
windows::build!( windows::build!(
windows::win32::system_services::{HANDLE, BOOL, PAGE_TYPE}, windows::win32::system_services::{HANDLE, BOOL, PAGE_TYPE, PSTR},
windows::win32::windows_programming::CloseHandle, windows::win32::windows_programming::CloseHandle,
// API needed for the shared memory // API needed for the shared memory
windows::win32::system_services::{CreateFileMappingA, OpenFileMappingA, MapViewOfFile, UnmapViewOfFile}, windows::win32::system_services::{CreateFileMappingA, OpenFileMappingA, MapViewOfFile, UnmapViewOfFile},

View File

@ -217,7 +217,9 @@ pub unsafe fn setup_exception_handler<T: 'static + Handler>(handler: &mut T) ->
compiler_fence(Ordering::SeqCst); compiler_fence(Ordering::SeqCst);
unsafe { unsafe {
if let Some(prev) = SetUnhandledExceptionFilter(Some(core::mem::transmute(handle_exception as *const c_void))) { if let Some(prev) = SetUnhandledExceptionFilter(Some(core::mem::transmute(
handle_exception as *const c_void,
))) {
PREVIOUS_HANDLER = Some(core::mem::transmute(prev as *const c_void)); PREVIOUS_HANDLER = Some(core::mem::transmute(prev as *const c_void));
} }
} }

View File

@ -446,16 +446,16 @@ pub mod shmem {
use super::ShMem; use super::ShMem;
use crate::{ use crate::{
bolts::bindings::{ bolts::bindings::{
windows::win32::system_services::{HANDLE, BOOL, PAGE_TYPE},
windows::win32::system_services::{ windows::win32::system_services::{
CreateFileMappingA, MapViewOfFile, OpenFileMappingA, UnmapViewOfFile, CreateFileMappingA, MapViewOfFile, OpenFileMappingA, UnmapViewOfFile,
}, },
windows::win32::system_services::{BOOL, HANDLE, PAGE_TYPE, PSTR},
windows::win32::windows_programming::CloseHandle, windows::win32::windows_programming::CloseHandle,
}, },
Error, Error,
}; };
use core::{ptr, slice, ffi::c_void}; use core::ffi::c_void;
use uuid::Uuid; use uuid::Uuid;
const INVALID_HANDLE_VALUE: isize = -1; const INVALID_HANDLE_VALUE: isize = -1;
@ -508,15 +508,19 @@ pub mod shmem {
impl Win32ShMem { impl Win32ShMem {
pub fn from_str(map_str_bytes: &[u8; 20], map_size: usize) -> Result<Self, Error> { pub fn from_str(map_str_bytes: &[u8; 20], map_size: usize) -> Result<Self, Error> {
unsafe { unsafe {
let handle = OpenFileMappingA(FILE_MAP_ALL_ACCESS, BOOL(0), map_str_bytes as *const u8 as *const i8); let handle = OpenFileMappingA(
FILE_MAP_ALL_ACCESS,
BOOL(0),
PSTR(map_str_bytes as *const u8),
);
if handle == HANDLE(0) { if handle == HANDLE(0) {
return Err(Error::Unknown(format!( return Err(Error::Unknown(format!(
"Cannot open shared memory {}", "Cannot open shared memory {}",
String::from_utf8_lossy(map_str_bytes) String::from_utf8_lossy(map_str_bytes)
))); )));
} }
let map = MapViewOfFile(handle.clone(), FILE_MAP_ALL_ACCESS, 0, 0, map_size) let map =
as *mut u8; MapViewOfFile(handle.clone(), FILE_MAP_ALL_ACCESS, 0, 0, map_size) as *mut u8;
if map == ptr::null_mut() { if map == ptr::null_mut() {
return Err(Error::Unknown(format!( return Err(Error::Unknown(format!(
"Cannot map shared memory {}", "Cannot map shared memory {}",
@ -545,7 +549,7 @@ pub mod shmem {
PAGE_TYPE::PAGE_READWRITE, PAGE_TYPE::PAGE_READWRITE,
0, 0,
map_size as u32, map_size as u32,
map_str_bytes.as_ptr() as *const i8, PSTR(map_str_bytes.as_ptr()),
); );
if handle == HANDLE(0) { if handle == HANDLE(0) {
return Err(Error::Unknown(format!( return Err(Error::Unknown(format!(
@ -553,20 +557,22 @@ pub mod shmem {
String::from_utf8_lossy(map_str_bytes) String::from_utf8_lossy(map_str_bytes)
))); )));
} }
let map = MapViewOfFile(handle.clone(), FILE_MAP_ALL_ACCESS, 0, 0, map_size) let map =
as *mut u8; MapViewOfFile(handle.clone(), FILE_MAP_ALL_ACCESS, 0, 0, map_size) as *mut u8;
if map == ptr::null_mut() { if map == ptr::null_mut() {
return Err(Error::Unknown(format!( return Err(Error::Unknown(format!(
"Cannot map shared memory {}", "Cannot map shared memory {}",
String::from_utf8_lossy(map_str_bytes) String::from_utf8_lossy(map_str_bytes)
))); )));
} }
Ok(Self { let mut ret = Self {
shm_str: map_str_bytes[0..20], shm_str: [0; 20],
handle: handle, handle: handle,
map: map, map: map,
map_size: map_size, map_size: map_size,
}) };
ret.shm_str.clone_from_slice(&map_str_bytes[0..20]);
Ok(ret)
} }
} }
} }