Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
BashOnWindows issue 861 - workaround
extern crate kernel32;
extern crate winapi;
use kernel32::{CreateProcessA, WaitForSingleObject, CloseHandle};
use winapi::{LPCSTR, LPSTR, LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPSTARTUPINFOA,
LPPROCESS_INFORMATION, PROCESS_INFORMATION, STARTUPINFOA};
use winapi::winbase::INFINITE;
use std::ptr::null_mut;
use std::mem::size_of;
fn main() {
let application_name: LPCSTR = b"c:\\Windows\\System32\\bash.exe\0".as_ptr() as LPCSTR;
let command_line: LPSTR = b"bash --noprofile --norc -c 'echo foo bar'\0".as_ptr() as LPSTR;
let process_atributes: LPSECURITY_ATTRIBUTES = null_mut();
let thread_attributes: LPSECURITY_ATTRIBUTES = null_mut();
let inherit_handles: BOOL = 0;
let creation_flags: DWORD = 0;
let environment: LPVOID = null_mut();
let current_directory: LPCSTR = null_mut();
let mut startup_info = STARTUPINFOA {
cb: size_of::<STARTUPINFOA>() as u32,
lpReserved: null_mut(),
lpDesktop: null_mut(),
lpTitle: null_mut(),
dwX: 0,
dwY: 0,
dwXSize: 0,
dwYSize: 0,
dwXCountChars: 0,
dwYCountChars: 0,
dwFillAttribute: 0,
dwFlags: 0,
wShowWindow: 0,
cbReserved2: 0,
lpReserved2: null_mut(),
hStdInput: null_mut(),
hStdOutput: null_mut(),
hStdError: null_mut(),
};
let startup_info_p: LPSTARTUPINFOA = &mut startup_info;
let mut process_information = PROCESS_INFORMATION {
hProcess: null_mut(),
hThread: null_mut(),
dwProcessId: 0,
dwThreadId: 0,
};
let process_information_p: LPPROCESS_INFORMATION = &mut process_information;
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396
let k = unsafe {
CreateProcessA(application_name,
command_line,
process_atributes,
thread_attributes,
inherit_handles,
creation_flags,
environment,
current_directory,
startup_info_p,
process_information_p)
};
println!("okay? {}", k);
if k != 0 {
unsafe {
WaitForSingleObject(process_information.hProcess, INFINITE);
CloseHandle(process_information.hProcess);
CloseHandle(process_information.hThread);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.