Skip to content

Commit e12aeb3

Browse files
committed
Use CreateProcessW instead of CreateProcessA
Changed libnative to use CreateProcessW instead of CreateProcessA. In addition, the lpEnvironment parameter now uses Unicode. Added a helper function os::win32::as_mut_utf16_p, which does basically the same thing as os::win32::as_utf16_p except the pointer is mutable.
1 parent 4537f13 commit e12aeb3

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

src/liblibc/lib.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub use funcs::bsd43::{shutdown};
208208
#[cfg(windows)] pub use consts::os::extra::{TRUE, FALSE, INFINITE};
209209
#[cfg(windows)] pub use consts::os::extra::{PROCESS_TERMINATE, PROCESS_QUERY_INFORMATION};
210210
#[cfg(windows)] pub use consts::os::extra::{STILL_ACTIVE, DETACHED_PROCESS};
211-
#[cfg(windows)] pub use consts::os::extra::{CREATE_NEW_PROCESS_GROUP};
211+
#[cfg(windows)] pub use consts::os::extra::{CREATE_NEW_PROCESS_GROUP, CREATE_UNICODE_ENVIRONMENT};
212212
#[cfg(windows)] pub use consts::os::extra::{FILE_BEGIN, FILE_END, FILE_CURRENT};
213213
#[cfg(windows)] pub use consts::os::extra::{FILE_GENERIC_READ, FILE_GENERIC_WRITE};
214214
#[cfg(windows)] pub use consts::os::extra::{FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_SHARE_DELETE};
@@ -1937,6 +1937,7 @@ pub mod consts {
19371937

19381938
pub static DETACHED_PROCESS: DWORD = 0x00000008;
19391939
pub static CREATE_NEW_PROCESS_GROUP: DWORD = 0x00000200;
1940+
pub static CREATE_UNICODE_ENVIRONMENT: DWORD = 0x00000400;
19401941

19411942
pub static PIPE_ACCESS_DUPLEX: DWORD = 0x00000003;
19421943
pub static PIPE_ACCESS_INBOUND: DWORD = 0x00000001;
@@ -4193,7 +4194,7 @@ pub mod funcs {
41934194
pub mod kernel32 {
41944195
use types::os::arch::c95::{c_uint};
41954196
use types::os::arch::extra::{BOOL, DWORD, SIZE_T, HMODULE,
4196-
LPCWSTR, LPWSTR, LPCSTR, LPSTR,
4197+
LPCWSTR, LPWSTR,
41974198
LPCH, LPDWORD, LPVOID,
41984199
LPCVOID, LPOVERLAPPED,
41994200
LPSECURITY_ATTRIBUTES,
@@ -4251,16 +4252,16 @@ pub mod funcs {
42514252
dwProcessId: DWORD)
42524253
-> HANDLE;
42534254
pub fn GetCurrentProcess() -> HANDLE;
4254-
pub fn CreateProcessA(lpApplicationName: LPCSTR,
4255-
lpCommandLine: LPSTR,
4255+
pub fn CreateProcessW(lpApplicationName: LPCWSTR,
4256+
lpCommandLine: LPWSTR,
42564257
lpProcessAttributes:
42574258
LPSECURITY_ATTRIBUTES,
42584259
lpThreadAttributes:
42594260
LPSECURITY_ATTRIBUTES,
42604261
bInheritHandles: BOOL,
42614262
dwCreationFlags: DWORD,
42624263
lpEnvironment: LPVOID,
4263-
lpCurrentDirectory: LPCSTR,
4264+
lpCurrentDirectory: LPCWSTR,
42644265
lpStartupInfo: LPSTARTUPINFO,
42654266
lpProcessInformation:
42664267
LPPROCESS_INFORMATION)

src/libnative/io/process.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ fn spawn_process_os(config: p::ProcessConfig,
258258
GetCurrentProcess,
259259
DuplicateHandle,
260260
CloseHandle,
261-
CreateProcessA
261+
CreateProcessW
262262
};
263263
use libc::funcs::extra::msvcrt::get_osfhandle;
264264

@@ -318,15 +318,15 @@ fn spawn_process_os(config: p::ProcessConfig,
318318
let mut create_err = None;
319319

320320
// stolen from the libuv code.
321-
let mut flags = 0;
321+
let mut flags = libc::CREATE_UNICODE_ENVIRONMENT;
322322
if config.detach {
323323
flags |= libc::DETACHED_PROCESS | libc::CREATE_NEW_PROCESS_GROUP;
324324
}
325325

326326
with_envp(env, |envp| {
327327
with_dirp(dir, |dirp| {
328-
cmd.with_c_str(|cmdp| {
329-
let created = CreateProcessA(ptr::null(), mem::transmute(cmdp),
328+
os::win32::as_mut_utf16_p(cmd, |cmdp| {
329+
let created = CreateProcessW(ptr::null(), cmdp,
330330
ptr::mut_null(), ptr::mut_null(), TRUE,
331331
flags, envp, dirp, &mut si,
332332
&mut pi);
@@ -691,7 +691,7 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*mut c_void| -> T) -> T {
691691

692692
for pair in env.iter() {
693693
let kv = format!("{}={}", *pair.ref0(), *pair.ref1());
694-
blk.push_all(kv.as_bytes());
694+
blk.push_all(kv.to_utf16().as_slice());
695695
blk.push(0);
696696
}
697697

@@ -704,9 +704,12 @@ fn with_envp<T>(env: Option<~[(~str, ~str)]>, cb: |*mut c_void| -> T) -> T {
704704
}
705705

706706
#[cfg(windows)]
707-
fn with_dirp<T>(d: Option<&Path>, cb: |*libc::c_char| -> T) -> T {
707+
fn with_dirp<T>(d: Option<&Path>, cb: |*u16| -> T) -> T {
708708
match d {
709-
Some(dir) => dir.with_c_str(|buf| cb(buf)),
709+
Some(dir) => match dir.as_str() {
710+
Some(dir_str) => os::win32::as_utf16_p(dir_str, cb),
711+
None => cb(ptr::null())
712+
},
710713
None => cb(ptr::null())
711714
}
712715
}

src/libstd/os.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,14 @@ pub mod win32 {
141141
}
142142

143143
pub fn as_utf16_p<T>(s: &str, f: |*u16| -> T) -> T {
144+
as_mut_utf16_p(s, |t| { f(t as *u16) })
145+
}
146+
147+
pub fn as_mut_utf16_p<T>(s: &str, f: |*mut u16| -> T) -> T {
144148
let mut t = s.to_utf16();
145149
// Null terminate before passing on.
146150
t.push(0u16);
147-
f(t.as_ptr())
151+
f(t.as_mut_ptr())
148152
}
149153
}
150154

0 commit comments

Comments
 (0)