void gpr_setenv(const char *name, const char *value) { LPTSTR tname = gpr_char_to_tchar(name); LPTSTR tvalue = gpr_char_to_tchar(value); BOOL res = SetEnvironmentVariable(tname, tvalue); gpr_free(tname); gpr_free(tvalue); GPR_ASSERT(res); }
FILE *gpr_tmpfile(const char *prefix, char **tmp_filename_out) { FILE *result = NULL; LPTSTR template_string = NULL; TCHAR tmp_path[MAX_PATH]; TCHAR tmp_filename[MAX_PATH]; DWORD status; UINT success; if (tmp_filename_out != NULL) *tmp_filename_out = NULL; /* Convert our prefix to TCHAR. */ template_string = gpr_char_to_tchar(prefix); GPR_ASSERT(template_string); /* Get the path to the best temporary folder available. */ status = GetTempPath(MAX_PATH, tmp_path); if (status == 0 || status > MAX_PATH) goto end; /* Generate a unique filename with our template + temporary path. */ success = GetTempFileName(tmp_path, template_string, 0, tmp_filename); if (!success) goto end; /* Open a file there. */ if (_tfopen_s(&result, tmp_filename, TEXT("wb+")) != 0) goto end; end: if (result && tmp_filename) { *tmp_filename_out = gpr_tchar_to_char(tmp_filename); } gpr_free(template_string); return result; }
gpr_subprocess *gpr_subprocess_create(int argc, const char **argv) { gpr_subprocess *r; STARTUPINFO si; PROCESS_INFORMATION pi; char *args = gpr_strjoin_sep(argv, (size_t)argc, " ", NULL); TCHAR *args_tchar; args_tchar = gpr_char_to_tchar(args); gpr_free(args); memset(&si, 0, sizeof(si)); si.cb = sizeof(si); memset(&pi, 0, sizeof(pi)); if (!CreateProcess(NULL, args_tchar, NULL, NULL, FALSE, CREATE_NEW_PROCESS_GROUP, NULL, NULL, &si, &pi)) { gpr_free(args_tchar); return NULL; } gpr_free(args_tchar); r = gpr_malloc(sizeof(gpr_subprocess)); memset(r, 0, sizeof(*r)); r->pi = pi; return r; }
char *gpr_getenv(const char *name) { char *result = NULL; DWORD size; LPTSTR tresult = NULL; LPTSTR tname = gpr_char_to_tchar(name); DWORD ret; ret = GetEnvironmentVariable(tname, NULL, 0); if (ret == 0) return NULL; size = ret * (DWORD)sizeof(TCHAR); tresult = gpr_malloc(size); ret = GetEnvironmentVariable(tname, tresult, size); gpr_free(tname); if (ret == 0) { gpr_free(tresult); return NULL; } result = gpr_tchar_to_char(tresult); gpr_free(tresult); return result; }