Skip to content

Commit

Permalink
try to fix Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Be-ing committed Aug 26, 2022
1 parent 8d189d9 commit b23abe6
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions examples/cargo_without_cmake/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,21 @@ use std::{
ptr,
};

#[cfg(unix)]
use std::os::unix::ffi::OsStrExt;

extern "C" {
fn run_cpp(argc: c_int, argv: *const *const c_char) -> c_int;
}

fn main() {
// https://stackoverflow.com/questions/34379641/how-do-i-convert-rust-args-into-the-argc-and-argv-c-equivalents
let args: Vec<CString> = std::env::args_os().map(|osstring| {
let args: Vec<CString> = std::env::args_os().map(|string| {
// https://stackoverflow.com/questions/54374381/how-can-i-convert-a-windows-osstring-to-a-cstring

#[cfg(unix)]
let intermediate = osstring.as_bytes();
use std::os::unix::ffi::OsStrExt;

#[cfg(windows)]
let intermediate = osstring.to_string_lossy();
let string = string.to_string_lossy();

CString::new(intermediate).unwrap()
CString::new(string.as_bytes()).unwrap()
}).collect();

let mut c_args: Vec<*const c_char> = args.iter().map(|arg| arg.as_ptr()).collect();
Expand Down

0 comments on commit b23abe6

Please sign in to comment.