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; }
char *gpr_format_message(DWORD messageid) { LPTSTR tmessage; char *message; DWORD status = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, messageid, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)(&tmessage), 0, NULL); if (status == 0) return gpr_strdup("Unable to retrieve error string"); message = gpr_tchar_to_char(tmessage); LocalFree(tmessage); return message; }
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; }