Skip to content

Commit 6246f1b

Browse files
mrochraphael-proust
authored andcommitted
fix setting Lwt_process env on Windows
1 parent 4627e3c commit 6246f1b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/unix/lwt_process_stubs.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
#include "lwt_unix.h"
1111

12-
#if OCAML_VERSION < 41300
12+
/* needed for caml_stat_strdup_to_os before ocaml 4.13, and for
13+
caml_win32_multi_byte_to_wide_char, at least as of ocaml 5.0 */
1314
#define CAML_INTERNALS
15+
#if OCAML_VERSION < 50000
16+
#define caml_win32_multi_byte_to_wide_char win_multi_byte_to_wide_char
1417
#endif
1518

1619
#include <caml/alloc.h>
@@ -68,6 +71,7 @@ CAMLprim value lwt_process_create_process(value prog, value cmdline, value env,
6871
HANDLE hp, fd0, fd1, fd2;
6972
HANDLE to_close0 = INVALID_HANDLE_VALUE, to_close1 = INVALID_HANDLE_VALUE,
7073
to_close2 = INVALID_HANDLE_VALUE;
74+
int size;
7175

7276
fd0 = get_handle(Field(fds, 0));
7377
fd1 = get_handle(Field(fds, 1));
@@ -94,11 +98,24 @@ CAMLprim value lwt_process_create_process(value prog, value cmdline, value env,
9498
char_os
9599
*progs = string_option(prog),
96100
*cmdlines = caml_stat_strdup_to_os(String_val(cmdline)),
97-
*envs = string_option(env),
98101
*cwds = string_option(cwd);
99102

100103
#undef string_option
101104

105+
char_os *envs;
106+
if (Is_some(env)) {
107+
env = Some_val(env);
108+
size =
109+
caml_win32_multi_byte_to_wide_char(String_val(env),
110+
caml_string_length(env), NULL, 0);
111+
envs = caml_stat_alloc((size + 1)*sizeof(char_os));
112+
caml_win32_multi_byte_to_wide_char(String_val(env),
113+
caml_string_length(env), envs, size);
114+
envs[size] = 0;
115+
} else {
116+
envs = NULL;
117+
}
118+
102119
flags |= CREATE_UNICODE_ENVIRONMENT;
103120
if (! CreateProcess(progs, cmdlines, NULL, NULL, TRUE, flags,
104121
envs, cwds, &si, &pi)) {

0 commit comments

Comments
 (0)